-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmonty_b.py
More file actions
executable file
·64 lines (51 loc) · 1.39 KB
/
monty_b.py
File metadata and controls
executable file
·64 lines (51 loc) · 1.39 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
#!/usr/bin/env python3
# Dies ist eine Beispiellösung für Aufgabe 2b aus Level 4
# * einen Integer n einliest,
# * die Häufigkeitstabelle der Buchstaben aus der, zuvor erstellten, Datei
# "chars.txt" einliest und
# * die n häufigsten und die n seltenen Buchstaben ausgibt.
from pathlib import Path
path = Path("chars.txt")
# Eingabe Integer n:
n = input("Bitte eine Zahl eingeben: ")
if n.isdigit():
n = int(n)
else:
print("Es wurde keine Zahl eingegeben.")
exit()
# Einlesen der Datei:
if not path.exists():
print(f"Die Datei {path} wurde nicht gefunden.")
exit()
table = {}
file_obj = path.open("r")
for line in file_obj:
key = line.split(";")[0]
value = int(line.split(";")[1])
table[key] = value
file_obj.close()
# Sortieren
tmp = list(table.items())
table = []
for i in range(len(tmp)):
max_key = 0
max_value = 0
for index in range(len(tmp)):
entry = tmp[index]
if entry[1] > max_value:
max_key = index
max_value = entry[1]
max_entry = tmp[max_key]
table.append(max_entry)
tmp.remove(max_entry)
# Ausgeben der n häufigsten und n seltenen Buchstaben:
common = []
rare = []
for entry in table[0:n]:
common.append(entry[0])
for entry in table[-(n+1):-1]:
rare.append(entry[0])
print(f"Die {n} häufigsten Buchstaben sind: ")
print(common)
print(f"Die {n} seltenen Buchstaben sind: ")
print(rare)