-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (50 loc) · 2.7 KB
/
main.py
File metadata and controls
76 lines (50 loc) · 2.7 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
76
import random
class zipfAlgorithm():
def __init__(self, loop):
letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
] # 50% chance letters, 50% chance spaces
word, words, self.words_lenghts, self.decimals_percentage = list(), list(), list(), 4 # just initializing variables
self.lenghts_frequency, self.percentual_per_frequency, self.organized_percentuals, self.total_frequency = dict(), dict(), dict(), int()
while len(words) < loop: # arg from a class call
word.append(random.choice(letters))
if word[-1] == letters[26]:
# if last item from word is ' ' (space), then remove ' ' it and append the list as a str to words
word.remove(letters[26])
if word == []:
# if word ends being a empty list, then continue the loop
continue
words.append(str().join(word))
word = list() # reinitializing word variable
self.words = words
#return self.words
#
#
#
def statistics(self):
for item in self.words:
self.words_lenghts.append(len(item))
for item in self.words_lenghts:
if item not in self.lenghts_frequency:
self.lenghts_frequency[item] = 1
else:
self.lenghts_frequency[item] += 1
for item in self.lenghts_frequency:
self.total_frequency += self.lenghts_frequency.get(item)
percentual_per_singular_item = round(100 / self.total_frequency, self.decimals_percentage)
for item in self.lenghts_frequency:
self.percentual_per_frequency[item] = f'{round(self.lenghts_frequency[item]*percentual_per_singular_item, self.decimals_percentage)}%'
for iterator in range(1, len(self.percentual_per_frequency)*2):
try:
if self.percentual_per_frequency.get(iterator) is not None:
self.organized_percentuals[iterator] = self.percentual_per_frequency.get(iterator)
except KeyError:
del self.organized_percentuals[iterator]
continue
return self.organized_percentuals
if __name__ == '__main__':
probabilistic_shit = zipfAlgorithm(1000000)
print(probabilistic_shit.statistics())