diff --git a/.gitignore b/.gitignore index 68b819a..f2adf56 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.test.py \ No newline at end of file +*.test.py +*.pyc \ No newline at end of file diff --git a/u2/list_ops.py b/u2/list_ops.py new file mode 100644 index 0000000..533adbc --- /dev/null +++ b/u2/list_ops.py @@ -0,0 +1,32 @@ +def append(list1: list, list2: list) -> list: + return list1 + list2 +def concat(lists: list[list]) -> list: + fin = [] + for l in lists: + fin += l + return fin +def filter(function, list: list) -> list: + fin = [] + for x in list: + if(function(x)): + fin.append(x) + return fin +def length(list: list): + return len(list) +def map(function, list: list) -> list: + fin = [] + for x in list: + fin.append(function(x)) + return fin +def foldl(function, list: list, initial: any) -> any: + acc = initial + for x in list: + acc = function(acc, x) + return acc +def foldr(function, list: list, initial: any) -> any: + acc = initial + for x in list[::-1]: + acc = function(acc, x) + return acc +def reverse(list: list) -> list: + return list[::-1] diff --git a/u3/flatten_array.py b/u3/flatten_array.py new file mode 100644 index 0000000..aca61ff --- /dev/null +++ b/u3/flatten_array.py @@ -0,0 +1,8 @@ +def flatten(l) -> list: + acc = [] + for x in l: + if(isinstance(x, list)): + acc += flatten(x) + elif(x is not None): + acc.append(x) + return acc \ No newline at end of file diff --git a/u4/word_count.py b/u4/word_count.py index bb887a6..89e8316 100644 --- a/u4/word_count.py +++ b/u4/word_count.py @@ -1,30 +1,23 @@ from collections import defaultdict +def isalnum_without_apostroph(ch: str): + return ch.isalnum() and ch != "'" def count_words(sentance: str) -> dict: sent_list = list(sentance) words = defaultdict(int) - SPECIAL_CHAR = oct(7) - - delims = ":?!\n\t,._&@$^&*()%" - del_chars = '"' - - forbidden = list(delims) + list(del_chars) + [" ", "'", ""] - for i, ch in enumerate(sent_list): - if(ch in delims): - sent_list[i] = " " - elif(ch in del_chars): - sent_list[i] = "" if(ch == "'"): - if((sent_list[max(i-1, 0)] not in forbidden) and (sent_list[min(i+1, len(sent_list)-1)] not in forbidden)): - sent_list[i] = SPECIAL_CHAR + if((isalnum_without_apostroph(sent_list[max(i-1, 0)])) and (isalnum_without_apostroph(sent_list[min(i+1, len(sent_list)-1)]))): + pass else: - sent_list[i] = "" + sent_list[i] = " " + elif(not ch.isalnum()): + sent_list[i] = " " for w in ''.join(sent_list).lower().split(" "): - if w == "": + if not len(w): continue - words[w.replace(SPECIAL_CHAR, "'")] += 1 + words[w] += 1 return dict(words)