Skip to content
Snippets Groups Projects
Commit 2f161179 authored by Markus Opolka's avatar Markus Opolka
Browse files

Add sorted

parent 2d973246
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3 #!/usr/bin/env python3
import collections
def create_freq_dict(tokens):
freq_dict = {}
for token in tokens:
if not token in freq_dict.keys():
freq_dict[token] = 1
else:
freq_dict[token] += 1
return freq_dict
def sort_freq(fd, verbose=True, descending=True):
sorted_keys = sorted(fd, key=fd.get, reverse=descending)
sorted_list = list()
for k in sorted_keys:
sorted_list.append((k, fd[k]))
if verbose:
print(sorted_list)
def can_write_string(want, have):
fd_want = collections.Counter(want)
fd_have = collections.Counter(have)
for k, v in fd_want.items():
if v > fd_have[k]:
return False
return True
def sanitize(string): def sanitize(string):
...@@ -17,7 +51,9 @@ def main(): ...@@ -17,7 +51,9 @@ def main():
tokens = sanitize(content).split() tokens = sanitize(content).split()
try: try:
create_freq_dict(tokens) a = create_freq_dict(tokens)
sort_freq(a, verbose=False)
#print(can_write_string('foo', 'oh california'))
except Exception as e: except Exception as e:
print("Error while calling function:") print("Error while calling function:")
print(e) print(e)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment