From 594e5b58c86301cb50da886a126106906ad5ebb2 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Thu, 27 Aug 2020 23:49:09 -0400 Subject: [PATCH 1/7] Day1 MVP --- hashtable/hashtable.py | 66 +++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..e2b8a25d4 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -1,6 +1,9 @@ class HashTableEntry: """ Linked List hash table key/value pair + Node class to store key value pair of the link list + each node has string as key value pair and pointer + to the the next node in case there is collision """ def __init__(self, key, value): self.key = key @@ -22,7 +25,9 @@ class HashTable: def __init__(self, capacity): # Your code here - + self.capacity = capacity + #assiging list for the hash table + self.list_hashtable= [None for i in range(self.capacity)] def get_num_slots(self): """ @@ -34,7 +39,8 @@ def get_num_slots(self): Implement this. """ - # Your code here + + return len(self.list_hashtable) def get_load_factor(self): @@ -43,7 +49,7 @@ def get_load_factor(self): Implement this. """ - # Your code here + # def fnv1(self, key): @@ -59,11 +65,15 @@ def fnv1(self, key): def djb2(self, key): """ DJB2 hash, 32-bit - - Implement this, and/or FNV-1. + It uses bit manipulation and prime numbers + to create a hash index from a string """ - # Your code here - + hash = 5381 + byte_array = key.encode('utf-8') + for byte in byte_array: + # the modulus keeps it 32-bit, python ints don't overflow + hash = ((hash * 33) ^ byte) % 0x100000000 + return hash def hash_index(self, key): """ @@ -81,7 +91,31 @@ def put(self, key, value): Implement this. """ - # Your code here + #calculate index to store string(key)in the list + list_index = self.hash_index(key) + #create a link list at that index to store bucket of key value pair + #create a node with given key value pair + new_node = HashTableEntry(key, value) + # check if there is a node at that index of the list + existing_node = self.list_hashtable[list_index] + if existing_node: + while existing_node: + #compare key and value at that index with the new_node + if existing_node.key == new_node.key: + #replace the value if it is already exist + existing_node.value = value + return + last_node = existing_node + existing_node = existing_node.next + #if we didn't find the value in the bucket + #put the value at the end of bucket + last_node.next = new_node + else: + #store new_node at that index of the list + self.list_hashtable[list_index] = new_node + + + def delete(self, key): @@ -102,8 +136,20 @@ def get(self, key): Returns None if the key is not found. Implement this. - """ - # Your code here + """ + #calculate index to find string(key)in the list + list_index = self.hash_index(key) + # check if there is a node at that index of the list + existing_node = self.list_hashtable[list_index] + if existing_node: + while existing_node: + #compare key and value at that index with the new_node + if existing_node.key == key: + return existing_node.value + existing_node = existing_node.next + else: + return None + def resize(self, new_capacity): From 97961418b84c07fb44dfe2cb4ddb7548228f3c11 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Fri, 28 Aug 2020 00:51:10 -0400 Subject: [PATCH 2/7] started working on day2 --- hashtable/hashtable.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index e2b8a25d4..75e7c5b2a 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -126,7 +126,25 @@ def delete(self, key): Implement this. """ - # Your code here + #calculate index to find string(key)in the list + list_index = self.hash_index(key) + # check if there is a node at that index of the list + existing_node = self.list_hashtable[list_index] + if existing_node: + #keep looking for the key in the bucket till the end + prev_node = None + while existing_node: + + if existing_node.key == key: + if prev_node: + prev_node.next = existing_node.next + else: + self.list_hashtable[list_index] = existing_node.next + prev_node = existing_node + existing_node=existing_node.next + else: + print(f'waring: {key} not found') + def get(self, key): From 5aeb522ad9818a0967943b0af3fdf6a569373d00 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Mon, 31 Aug 2020 03:01:50 -0400 Subject: [PATCH 3/7] Day2 MVP --- hashtable/hashtable.py | 47 +++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 75e7c5b2a..043b40fd8 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -23,11 +23,12 @@ class HashTable: Implement this. """ - def __init__(self, capacity): + def __init__(self, capacity=8): # Your code here self.capacity = capacity #assiging list for the hash table self.list_hashtable= [None for i in range(self.capacity)] + self.count =0 def get_num_slots(self): """ @@ -40,7 +41,7 @@ def get_num_slots(self): Implement this. """ - return len(self.list_hashtable) + return self.capacity #len(self.list_hashtable) def get_load_factor(self): @@ -49,7 +50,18 @@ def get_load_factor(self): Implement this. """ - # + #load factor = (No. of item in the hash table)/total no of slots + # items=0 + # for i in range(self.capacity): + # existing_node = self.list_hashtable[i] + # if existing_node: + # while existing_node: + # items +=1 + # existing_node=existing_node.next + + + load_factor = self.count/self.capacity + return load_factor def fnv1(self, key): @@ -110,10 +122,14 @@ def put(self, key, value): #if we didn't find the value in the bucket #put the value at the end of bucket last_node.next = new_node + self.count +=1 else: #store new_node at that index of the list self.list_hashtable[list_index] = new_node - + self.count +=1 + load_factor = self.get_load_factor() + if load_factor >0.7: + self.resize(int(self.capacity *2)) @@ -176,8 +192,20 @@ def resize(self, new_capacity): rehashes all key/value pairs. Implement this. - """ - # Your code here + """ + new_list = [None for i in range(new_capacity)] + old_capacity = self.capacity + self.capacity = new_capacity + old_list = self.list_hashtable + self.list_hashtable = new_list + for node in old_list: + if node: + self.put(node.key, node.value) + + + + + @@ -203,13 +231,16 @@ def resize(self, new_capacity): for i in range(1, 13): print(ht.get(f"line_{i}")) + load_factor = ht.get_load_factor() + print(load_factor) # Test resizing old_capacity = ht.get_num_slots() ht.resize(ht.capacity * 2) new_capacity = ht.get_num_slots() - + + load_factor = ht.get_load_factor() print(f"\nResized from {old_capacity} to {new_capacity}.\n") - + print(load_factor) # Test if data intact after resizing for i in range(1, 13): print(ht.get(f"line_{i}")) From 32479a86c9b702932dacbcb670f7400b8300c5a8 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Tue, 1 Sep 2020 02:33:09 -0400 Subject: [PATCH 4/7] Working on the Day2 --- applications/expensive_seq/expensive_seq.py | 12 +++++++++--- applications/lookup_table/lookup_table.py | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 5c82b8453..8768ec41b 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -1,8 +1,14 @@ # Your code here - - +cache ={} def expensive_seq(x, y, z): - # Your code here + if x <= 0: + result = y + z + return result + if (x, y, z) in cache: + return cache[(x,y,z)] + else: + cache[(x,y,z)] = expensive_seq(x-1,y+1,z) + expensive_seq(x-2,y+2,z*2) + expensive_seq(x-3,y+3,z*3) + return cache[(x, y, z)] diff --git a/applications/lookup_table/lookup_table.py b/applications/lookup_table/lookup_table.py index 05b7d37fa..3121d6bf1 100644 --- a/applications/lookup_table/lookup_table.py +++ b/applications/lookup_table/lookup_table.py @@ -1,5 +1,6 @@ # Your code here - +import random +import math def slowfun_too_slow(x, y): v = math.pow(x, y) @@ -8,13 +9,22 @@ def slowfun_too_slow(x, y): v %= 982451653 return v - +lookup_table = {} def slowfun(x, y): """ Rewrite slowfun_too_slow() in here so that the program produces the same output, but completes quickly instead of taking ages to run. """ - # Your code here + #create a dictionary(hash tale) for caching + if (x,y) in lookup_table: + return lookup_table[(x,y)] + else: + lookup_table[(x,y)]=slowfun_too_slow(x, y) + return lookup_table[(x,y)] + + + + From 79cffb36041b238b133652737f5433337589eeb4 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Wed, 2 Sep 2020 02:23:07 -0400 Subject: [PATCH 5/7] Finished Day3 --- applications/no_dups/no_dups.py | 15 ++++++++++++++- applications/word_count/README.md | 2 +- applications/word_count/word_count.py | 19 +++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/applications/no_dups/no_dups.py b/applications/no_dups/no_dups.py index caa162c8c..0d94bb951 100644 --- a/applications/no_dups/no_dups.py +++ b/applications/no_dups/no_dups.py @@ -1,5 +1,18 @@ def no_dups(s): - # Your code here + ''' + str -> str + + ''' + cache = '' + words = s.split() + for word in words: + if word not in cache: + cache = cache + ' ' +word + return cache.strip() + + + + diff --git a/applications/word_count/README.md b/applications/word_count/README.md index 99d43b158..b730be5d3 100644 --- a/applications/word_count/README.md +++ b/applications/word_count/README.md @@ -28,4 +28,4 @@ Ignore each of the following characters: " : ; , . - + = / \ | [ ] { } ( ) * ^ & ``` -If the input contains no ignored characters, return an empty dictionary. +If the input contains all ignored characters, return an empty dictionary. diff --git a/applications/word_count/word_count.py b/applications/word_count/word_count.py index a20546425..f452b6e75 100644 --- a/applications/word_count/word_count.py +++ b/applications/word_count/word_count.py @@ -1,10 +1,25 @@ def word_count(s): - # Your code here + word_dic = {} + ignored_char ='":;,.-+=/\\|[]{}()*^&' + #split the string into words + words= s.lower().split() + for word in words: + for char in word: + if char in ignored_char: + word = word.replace(char,'') + if word is '': + return {} + if word not in word_dic: + word_dic[word] = 1 + else: + word_dic[word] +=1 + return word_dic if __name__ == "__main__": + print(word_count('":;,.-+=/\\|[]{}()*^&')) print(word_count("")) - print(word_count("Hello")) + print(word_count("Hello hello")) print(word_count('Hello, my cat. And my cat doesn\'t say "hello" back.')) print(word_count('This is a test of the emergency broadcast network. This is only a test.')) \ No newline at end of file From 94d944e3dca59e319dcb58c2e28cf6ee51a45407 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Fri, 4 Sep 2020 02:47:52 -0400 Subject: [PATCH 6/7] working on Day4 --- applications/markov/markov.py | 39 +++++++++++++++++++++++++++++++-- applications/no_dups/no_dups.py | 1 - 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/applications/markov/markov.py b/applications/markov/markov.py index 1d138db10..8efc39f42 100644 --- a/applications/markov/markov.py +++ b/applications/markov/markov.py @@ -3,9 +3,44 @@ # Read in all the words in one go with open("input.txt") as f: words = f.read() - + +texts = words.split(' ') # TODO: analyze which words can follow other words -# Your code here +word_dic ={} +for i in range(len(texts)-1): + if texts[i] not in word_dic: + word_dic[texts[i]] = [texts[i+1]] + else: + word_dic[texts[i]].append(texts[i+1]) + +start_words =[] +stop_words =[] +for word in word_dic: + if word[0].isupper() or word[0] == '"': + start_words.append(word) + if word[-1] in '.?!': + stop_words.append(word) + if len(word)>2: + if (word[-2] in '.?!') and word[-1] =='"': + stop_words.append(word) + + + +# start with capital letter or " +# for key in word_dic: + +# # start_words +start = random.choice(list(word_dic)) + +for i in range(5): + if start in stop_words: + continue + else: + start = random.choice(word_dic[start]) + for value in word_dic[start]: + print(value, end = ' ') + + # TODO: construct 5 random sentences diff --git a/applications/no_dups/no_dups.py b/applications/no_dups/no_dups.py index 0d94bb951..ec40788e2 100644 --- a/applications/no_dups/no_dups.py +++ b/applications/no_dups/no_dups.py @@ -1,7 +1,6 @@ def no_dups(s): ''' str -> str - ''' cache = '' words = s.split() From 97cc1649b2f718f6e40c64ecc2b4f342ef52f594 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Wed, 11 Nov 2020 01:37:28 -0500 Subject: [PATCH 7/7] finished histogram --- applications/histo/README.md | 10 ++++------ applications/histo/histo.py | 22 +++++++++++++++++++++- applications/markov/markov.py | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/applications/histo/README.md b/applications/histo/README.md index 428a1b267..ea304bcc6 100644 --- a/applications/histo/README.md +++ b/applications/histo/README.md @@ -67,14 +67,12 @@ bow ###### ## Hints -Items: `.vgrzf()` zrgubq ba n qvpgvbanel zvtug or hfrshy. +Items: `.items()` method on a dictionary might be useful. -Sorting: vg'f cbffvoyr sbe `.fbeg()` gb fbeg ba zhygvcyr xrlf ng bapr. +Sorting: it's possible for `.sort()` to sort on multiple keys at once. -Sorting: artngvirf zvtug uryc jurer `erirefr` jba'g. - -Printing: lbh pna cevag n inevnoyr svryq jvqgu va na s-fgevat jvgu -arfgrq oenprf, yvxr fb `{k:{l}}` +Sorting: negatives might help where `reverse` won't. +Printing: you can print a variable field width in an f-string with nested braces, like so {x:{y}} (The hints are encrypted with ROT13. Google for `rot13 decoder` to see them.) \ No newline at end of file diff --git a/applications/histo/histo.py b/applications/histo/histo.py index 6014a8e13..9bd4b2c51 100644 --- a/applications/histo/histo.py +++ b/applications/histo/histo.py @@ -1,2 +1,22 @@ -# Your code here +# Open and the file +with open('robin.txt') as f: + texts= f.read() +#split the text +words = texts.lower().split() +#create a dictionary for the word count +histogram ={} +#ignore character +ignore_char = ' " : ; , . - + = / \ | [ ] { } ( ) * ^ & ' +for word in words: + for char in word: + if char in ignore_char: + word = word.replace(char, '') + if word not in histogram: + histogram[word] = '#' + else: + histogram[word] += '#' +for key, value in sorted(histogram.items(), key = lambda item: (item[1]), reverse =True): + print(f'{key} :{value}') + + diff --git a/applications/markov/markov.py b/applications/markov/markov.py index 8efc39f42..a5fb14e10 100644 --- a/applications/markov/markov.py +++ b/applications/markov/markov.py @@ -4,7 +4,7 @@ with open("input.txt") as f: words = f.read() -texts = words.split(' ') +texts = words.lower().split(' ') # TODO: analyze which words can follow other words word_dic ={} for i in range(len(texts)-1):