diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..26d33521a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Sprint-Challenge--Data-Structures-Python.iml b/.idea/Sprint-Challenge--Data-Structures-Python.iml new file mode 100644 index 000000000..8dc09e547 --- /dev/null +++ b/.idea/Sprint-Challenge--Data-Structures-Python.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 000000000..f2e239138 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,31 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 000000000..105ce2da2 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..a2e120dcc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..e549fe287 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/names/names.py b/names/names.py index ea158997f..784248643 100644 --- a/names/names.py +++ b/names/names.py @@ -13,10 +13,17 @@ duplicates = [] # Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements -for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) +# # runtime: 6.383997678756714 seconds + +with open('names_1.txt') as f1, open('names_2.txt') as f2: + for line in set(line.strip() for line in f1) & set(line.strip() for line in f2): + if line: + duplicates.append(line) + # runtime: 0.008002042770385742 seconds 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 6116252d1..c8168476a 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -38,5 +38,16 @@ def contains(self, value): return False - def reverse_list(self, node, prev): - pass + def reverse_list(self, node, prev=None): + while node is not None: + # we hold a reference for the node's next node + next = node.next_node + # in the beginning, set to None, like how the tail is set to None + # Then afterwards the next_node is set to the prev node of the current nod + node.next_node = prev + # prev set to node for later use in the while loop + prev = node + # We then go on to the node's next node to change its pointer + node = next + # we set a new head of the final node that we changed + self.head = prev diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..3be62254c 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,40 @@ +# https://www.youtube.com/watch?v=ia__kyuwGag&t=627s + class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.index = 0 + self.storage = [] def append(self, item): - pass + if len(self.storage) is self.capacity: + self.storage[self.index] = item + else: + self.storage.append(item) + self.index = (self.index + 1) % self.capacity def get(self): - pass \ No newline at end of file + return self.storage + + +if __name__ == '__main__': + buffer = RingBuffer(3) + + print(buffer.get()) # should return [] + + buffer.append('a') + buffer.append('b') + buffer.append('c') + print(len(buffer.get())) + print(buffer.get()) # should return ['a', 'b', 'c'] + + # 'd' overwrites the oldest value in the ring buffer, which is 'a' + buffer.append('d') + + print(buffer.get()) # should return ['d', 'b', 'c'] + + buffer.append('e') + print(buffer.get()) + buffer.append('f') + + print(buffer.get()) # should return ['d', 'e', 'f']