-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_18.py
More file actions
144 lines (121 loc) · 4.43 KB
/
module_18.py
File metadata and controls
144 lines (121 loc) · 4.43 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 18. Итоговая работа на файлы
"""
Этот модуль содержит дополнительные задачи по изученным темам.
Они помогут еще раз закрепить ваши знания, углубить понимание
ключевых концепций и убедиться, что вы освоили материал.
"""
# 18.1 Итоговая работа
def m_18_1_1(): # Количество строк в файле
file_name = input()
with open(file_name, encoding="utf-8") as file:
print(len(file.readlines()))
def m_18_1_2(): # Суммарная стоимость
file_name = "ledger.txt" # <= "data/ledger.txt" для тестов
with open(file_name, encoding="utf-8") as file:
print(f'${sum(int(row.strip("$")) for row in file)}')
def m_18_1_3(): # Goooood students
file_name = "grades.txt" # <= "data/grades.txt" для тестов
with open(file_name, encoding="utf-8") as file:
print(
sum(
1
for row in file
if all(int(x) >= 65 for x in filter(lambda x: x.isdigit(), row.split()))
)
)
def m_18_1_4(): # Самое длинное слово в файле
file_name = "words.txt" # <= "data/words.txt" для тестов
with open(file_name, encoding="utf-8") as file:
max_len = max(len(max(line.strip().split(), key=len)) for line in file)
file.seek(0)
print(
*(
word
for line in file
for word in (filter(lambda w: len(w) == max_len, line.strip().split()))
),
sep="\n",
)
def m_18_1_5(): # Tail of a File
file_name = input() # <= "data/test_file.txt" для тестов
with open(file_name, encoding="utf-8") as file:
lines = file.readlines()
print(*lines[-10:], sep="")
def m_18_1_6(): # Forbidden words
def cheked(word):
result = word.lower()
for bad in forbidden:
while bad in result:
result = result.replace(bad, "*" * len(bad))
return "".join(w if r != "*" else r for w, r in zip(word, result))
# "data/data-2.txt", "data/beegeek.txt", "data/stepik.txt" для тестов
forbidden_words = "forbidden_words.txt" # <= "data/forbidden_words.txt" для тестов
with open(forbidden_words, encoding="utf-8") as file:
forbidden = file.read().split()
file_name = "data/beegeek.txt" # input()
with open(file_name, encoding="utf-8") as file:
for line in file:
print(cheked(line.strip("\n")))
def m_18_1_7(): #
def translit(text):
translit_text = ""
for ch in text:
if ch.lower() in translit_dict:
translit_ch = translit_dict[ch.lower()]
translit_text += (
translit_ch.capitalize() if ch.isupper() else translit_ch
)
else:
translit_text += ch
return translit_text
translit_dict = {
"а": "a",
"к": "k",
"х": "h",
"б": "b",
"л": "l",
"ц": "c",
"в": "v",
"м": "m",
"ч": "ch",
"г": "g",
"н": "n",
"ш": "sh",
"д": "d",
"о": "o",
"щ": "shh",
"е": "e",
"п": "p",
"ъ": "*",
"ё": "jo",
"р": "r",
"ы": "y",
"ж": "zh",
"с": "s",
"ь": "'",
"з": "z",
"т": "t",
"э": "je",
"и": "i",
"у": "u",
"ю": "ju",
"й": "j",
"ф": "f",
"я": "ya",
}
file_name = "cyrillic.txt" # "data/cyrillic.txt" для тестов
with open(file_name, encoding="utf-8") as input_file, open(
"transliteration.txt", "w", encoding="utf-8"
) as output_file:
for line in input_file:
output_file.write(translit(line))
def m_18_1_8(): # Пропущенные комменты
pass
file_name = input() # "data/test_file_18_1_8.txt" для тестов
with open(file_name, encoding="utf-8") as file:
lines = file.readlines()
result = []
for n, line in enumerate(lines):
if line.startswith("def ") and (n == 0 or not lines[n - 1].startswith("#")):
result.append(line.strip()[4 : line.index("(")])
print("\n".join(result) if result else "Best Programming Team")