u2, u3, u4 - opraveny test
This commit is contained in:
parent
6dad46914b
commit
6bf02bf9e6
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
*.test.py
|
*.test.py
|
||||||
|
*.pyc
|
||||||
32
u2/list_ops.py
Normal file
32
u2/list_ops.py
Normal file
@ -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]
|
||||||
8
u3/flatten_array.py
Normal file
8
u3/flatten_array.py
Normal file
@ -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
|
||||||
@ -1,30 +1,23 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
def isalnum_without_apostroph(ch: str):
|
||||||
|
return ch.isalnum() and ch != "'"
|
||||||
def count_words(sentance: str) -> dict:
|
def count_words(sentance: str) -> dict:
|
||||||
sent_list = list(sentance)
|
sent_list = list(sentance)
|
||||||
words = defaultdict(int)
|
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):
|
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(ch == "'"):
|
||||||
if((sent_list[max(i-1, 0)] not in forbidden) and (sent_list[min(i+1, len(sent_list)-1)] not in forbidden)):
|
if((isalnum_without_apostroph(sent_list[max(i-1, 0)])) and (isalnum_without_apostroph(sent_list[min(i+1, len(sent_list)-1)]))):
|
||||||
sent_list[i] = SPECIAL_CHAR
|
pass
|
||||||
else:
|
else:
|
||||||
sent_list[i] = " "
|
sent_list[i] = " "
|
||||||
|
elif(not ch.isalnum()):
|
||||||
|
sent_list[i] = " "
|
||||||
|
|
||||||
for w in ''.join(sent_list).lower().split(" "):
|
for w in ''.join(sent_list).lower().split(" "):
|
||||||
if w == "":
|
if not len(w):
|
||||||
continue
|
continue
|
||||||
words[w.replace(SPECIAL_CHAR, "'")] += 1
|
words[w] += 1
|
||||||
|
|
||||||
return dict(words)
|
return dict(words)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user