Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework lesson 3 Fadeev #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions homework_3_Fadeev/task_3_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def num_translate(num_dict: dict, num: str) -> str:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не нужно менять аргументы функции в исходном задании value: str

"""Переводит числительное с английского на русский"""

# str_out = num_dict[num] # В случае если искомого слова нет в словаре возвращает ошибку
str_out = num_dict.get(num)
return str_out


# Словарь размещаем в теле программы, чтобы к нему могли обращаться другие функции
user_dict = {
'one': 'один',
'two': 'два',
'three': 'три',
'four': 'четыре',
'five': 'пять',
'six': 'шесть',
'seven': 'семь',
'eight': 'восемь',
'nine': 'девять',
'ten': 'десять',
'zero': 'нуль',
}

print(f'Перевод слова "two": {num_translate(user_dict, "two")}')
print(f'Перевод слова "eight": {num_translate(user_dict, "eight")}')
print(f'Перевод слова "eleven": {num_translate(user_dict, "eleven")}')
30 changes: 30 additions & 0 deletions homework_3_Fadeev/task_3_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def thesaurus(*names: str) -> dict:

key_list = []
aux_list = []
names_list = []
dictionary = {}

# из входных аргументов получаем лист, состоящий из заглавных букв - key_list
for i in range(0, len(names), 1):
key_list.append(names[i][0])
Comment on lines +9 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если уж завязались на такие манипуляции, то можно было посмотреть в сторону оптимизации через lambda и map

# удаляем повторы из листа key_list
i = 0
while i < len(key_list) - 1:
if key_list[i] == key_list[i + 1]:
key_list.remove(key_list[i + 1])
Comment on lines +14 to +15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

забыли метод .setdefault() с вебинара?

else:
i = i + 1
# по каждой букве из key_list формируем список из фамилий и добавляем его в список names_list, формируем словарь dictionary
for i in range(0, len(key_list), 1):
for k in range(0, len(names), 1):
if key_list[i] == names[k][0]:
aux_list.append(names[k])
names_list.append(aux_list)
aux_list = []
dictionary.update({key_list[i]: names_list[i]})

return dictionary


print(thesaurus("Виктор", "Владимир", "Борис", "Братислав", "Марина"))
30 changes: 30 additions & 0 deletions homework_3_Fadeev/task_3_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def get_jokes(count: int, flag: int) -> list:

nouns = ["автомобиль", "лес", "огонь", "город", "дом"]
adverbs = ["сегодня", "вчера", "завтра", "позавчера", "ночью"]
adjectives = ["веселый", "яркий", "зеленый", "утопичный", "мягкий"]

from random import randint

joke_list = []

if flag == 0:
for i in range(count):
noun = nouns[randint(0, len(nouns) - 1)]
adverb = adverbs[randint(0, len(adverbs) - 1)]
adjective = adjectives[randint(0, len(adjectives) - 1)]
joke_list.append(' '.join([noun, adverb, adjective]))
Comment on lines +13 to +16
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно было сделать в одну строку, используя choice из random

i += 1
else:
# для flag == 1 используется метод .pop()
for i in range(0, len(nouns), 1):
noun = nouns.pop(randint(0, len(nouns) - 1))
adverb = adverbs.pop(randint(0, len(adverbs) - 1))
adjective = adjectives.pop(randint(0, len(adjectives) - 1))
joke_list.append(' '.join([noun, adverb, adjective]))
i += 1

return joke_list


print(get_jokes(6, 1))