diff --git a/Pipfile b/Pipfile new file mode 100644 index 000000000..b723d0199 --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] + +[requires] +python_version = "3.7" diff --git a/names/names.py b/names/names.py index 586e8393e..70b21c0d2 100644 --- a/names/names.py +++ b/names/names.py @@ -11,10 +11,36 @@ f.close() duplicates = [] -for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) + +#O(n) solution +# names_cache = {} + +# for n in names_1: +# names_cache[n] = 1 + +# for n in names_2: +# if n in names_cache: +# duplicates.append(n) + +#Limited memory solution, only store in arrays +#Sort first array, binary search each name in second array +#Slower than my first solution, but still pretty dang fast +#O( n log n ) +names_1.sort() +for n in names_2: + first = 0 + last = len(names_2) - 1 + found = False + while (first <= last and not found): + mid = (first + last)//2 + if names_1[mid] == n: + duplicates.append(n) + found = True + else: + if n < names_1[mid]: + last = mid - 1 + else: + first = mid + 1 end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") diff --git a/reverse/reverse.py b/reverse/reverse.py index 95ed9d1d4..eaaf81341 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -1,47 +1,59 @@ class Node: - def __init__(self, value=None, next_node=None): - # the value at this linked list node - self.value = value - # reference to the next node in the list - self.next_node = next_node + def __init__(self, value=None, next_node=None): + # the value at this linked list node + self.value = value + # reference to the next node in the list + self.next_node = next_node - def get_value(self): - return self.value + def get_value(self): + return self.value - def get_next(self): - return self.next_node + def get_next(self): + return self.next_node - def set_next(self, new_next): - # set this node's next_node reference to the passed in node - self.next_node = new_next + def set_next(self, new_next): + # set this node's next_node reference to the passed in node + self.next_node = new_next class LinkedList: - def __init__(self): - # reference to the head of the list - self.head = None + def __init__(self): + # reference to the head of the list + self.head = None - def add_to_head(self, value): - node = Node(value) - if self.head is not None: - node.set_next(self.head) - - self.head = node + def add_to_head(self, value): + node = Node(value) + if self.head is not None: + node.set_next(self.head) + + self.head = node - def contains(self, value): - if not self.head: - return False - # get a reference to the node we're currently at; update this as we traverse the list - current = self.head - # check to see if we're at a valid node - while current: - # return True if the current value we're looking at matches our target value - if current.get_value() == value: - return True - # update our current node to the current node's next node - current = current.get_next() - # if we've gotten here, then the target node isn't in our list - return False + def contains(self, value): + if not self.head: + return False + # get a reference to the node we're currently at; update this as we traverse the list + current = self.head + # check to see if we're at a valid node + while current: + # return True if the current value we're looking at matches our target value + if current.get_value() == value: + return True + # update our current node to the current node's next node + current = current.get_next() + # if we've gotten here, then the target node isn't in our list + return False - def reverse_list(self): - # TO BE COMPLETED - pass \ No newline at end of file + def reverse_list(self): + if self.head: + previous_node = None + current_node = self.head + next_node = self.head.next_node + while next_node is not None: + #reverse current_node + current_node.next_node = previous_node + #increment current and next_node + previous_node = current_node + current_node = next_node + next_node = current_node.next_node + current_node.next_node = previous_node + #set current_node to head + self.head = current_node \ No newline at end of file diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 35fd33cac..abc68b20f 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,11 +1,21 @@ class RingBuffer: - def __init__(self, capacity): - self.capacity = capacity - self.current = 0 - self.storage = [None]*capacity + def __init__(self, capacity): + self.capacity = capacity + self.current = 0 + #I would have gone with a linked list here + self.storage = [None]*capacity - def append(self, item): - pass + def append(self, item): + self.storage[self.current] = item + if self.current < self.capacity-1: + self.current += 1 + else: + self.current = 0 - def get(self): - pass \ No newline at end of file + def get(self): + ret_arr = [] + i = 0 + while i < self.capacity and self.storage[i] is not None: + ret_arr.append(self.storage[i]) + i += 1 + return ret_arr \ No newline at end of file