Repository navigation
Expand file tree
/
Copy pathcodeinplace_32.py
More file actions
75 lines (61 loc) · 1.98 KB
/
Copy pathcodeinplace_32.py
File metadata and controls
75 lines (61 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Curso:
Code in Place (Stanford)
Objetivo:
Praticar estrutura de dados de dicionário.
Notes:
Percebo 3 principais aprendizados do curso:
1. tópicos de boas práticas (arquitetura básica, top-down design)
2. debugging em loops while e for (fencepost bug)
3. interesse em entender a lógica no nível de memória e hardware
^ isso me fez começar e concluir um curso básico de C
Com este exercício, finalizo o curso :)
"""
def main():
"""
Read baby words in words.txt, count the
frequency of each word and display a histogram
of the counts.
"""
words = load_words_from_file("words.txt")
# TODO: your code here :)
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
for word, count in word_counts.items():
print_histogram_bar(word, count)
def print_histogram_bar(word, count):
"""
Prints one bar in the histogram.
Uses formatted strings to do so. The
{word : <8}
adds white space after a string to make
the string take up 8 total characters of space.
This makes all of our words on the left of the
histogram line up nicely. On the other end,
{'x' * count}
takes the 'x' string and duplicates it by 'count'
number of times. So 'x' * 5 would be 'xxxxx'.
Calling print_histogram_bar("mom", 7) would print:
mom : xxxxxxx
"""
print(f"{word : <8}: {'x' * count}")
def load_words_from_file(filepath):
"""
Loads words from a file into a list and returns it.
We assume the file to have one word per line.
Returns a list of strings. You should not modify this
function.
"""
words = []
with open(filepath, 'r') as file_reader:
for line in file_reader.readlines():
cleaned_line = line.strip()
if cleaned_line != '':
words.append(cleaned_line)
return words
if __name__ == '__main__':
main()