-
Notifications
You must be signed in to change notification settings - Fork 260
Badriev A. (Basic_exercises) #36
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
Open
badrievad
wants to merge
14
commits into
learnpythonru:main
Choose a base branch
from
badrievad:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7eabf14
Test commit
badrievad f3d43f8
for_challenges done
badrievad a3d77c9
string_challenges done
badrievad 19e2ef3
for_dict_challenges done
badrievad eb457db
for_dict_challenges_bonus done
badrievad 340e25e
Done
badrievad 24a75a7
Done
badrievad 29014c5
Done
badrievad 5e8c03b
Update for_dict_challenges.py
badrievad ec1b21b
Done
badrievad c115653
Done
badrievad 2d839db
Done
badrievad 84f10c7
Done
badrievad 3fcdf93
Done
badrievad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| import random | ||
| import uuid | ||
| import datetime | ||
| import lorem | ||
| from itertools import groupby | ||
| from collections import Counter | ||
|
|
||
| """ | ||
| Пожалуйста, приступайте к этой задаче после того, как вы сделали и получили ревью ко всем остальным задачам | ||
| в этом репозитории. Она значительно сложнее. | ||
|
|
@@ -30,11 +37,11 @@ | |
|
|
||
| Весь код стоит разбить на логические части с помощью функций. | ||
| """ | ||
| import random | ||
| import uuid | ||
| import datetime | ||
|
|
||
| import lorem | ||
|
|
||
| # Создадим функцию, которая принимает list для нахождения максимального повторяющегося элемента в данном списке | ||
| def maximum_frequency(lst): | ||
| return Counter(lst).most_common(1)[0][0] | ||
|
|
||
|
|
||
| def generate_chat_history(): | ||
|
|
@@ -66,5 +73,72 @@ def generate_chat_history(): | |
| return messages | ||
|
|
||
|
|
||
| # Создает список значений по ключу | ||
| def create_list_by_key(key_name: str, messages): | ||
| return [message[key_name] for message in messages if message[key_name] is not None] | ||
|
|
||
|
|
||
| def find_id_who_write_the_most_messages(messages): | ||
| return maximum_frequency(create_list_by_key('sent_by', messages)) | ||
|
|
||
|
|
||
| def find_id_who_got_the_most_replies(messages): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Классный нейминг! |
||
| for message in messages: | ||
| if message['id'] == maximum_frequency(create_list_by_key('reply_for', messages)): | ||
| return message["sent_by"] | ||
|
|
||
|
|
||
| def find_id_saw_unique_users(messages): | ||
| id_unique = {} | ||
| for message in messages: | ||
| id_unique.setdefault(message['sent_by'], set()).update(message['seen_by']) | ||
|
|
||
| # создаем список с отсортированными айди по убыванию кол-ва просмотров уникальными пользователями | ||
| sorted_id_user_and_count_saw_users = sorted(id_unique.items(), reverse=True, key=lambda x: len(x[1])) | ||
| return [id_user for id_user, count in sorted_id_user_and_count_saw_users] | ||
|
|
||
|
|
||
| def what_time_more_messages(messages): | ||
| morning, day, evening = [], [], [] | ||
|
|
||
| for message in messages: | ||
| hour_of_writing = message["sent_at"].hour | ||
| if hour_of_writing < 12: | ||
| morning.append(hour_of_writing) | ||
| elif 18 > hour_of_writing > 12: | ||
| day.append(hour_of_writing) | ||
| else: | ||
| evening.append(hour_of_writing) | ||
|
|
||
| max_messages = max([len(message) for message in [morning, day, evening]]) | ||
|
|
||
| if len(morning) == max_messages: | ||
| return 'morning' | ||
| elif len(day) == max_messages: | ||
| return 'day' | ||
| else: | ||
| return 'evening' | ||
|
|
||
|
|
||
| def maximum_thread_length(messages): | ||
| result = [] | ||
|
|
||
| for id_reply_for, thread_length in groupby( | ||
| [message['reply_for'] for message in messages if message['reply_for'] is not None]): | ||
| length = len(list(thread_length)) | ||
| if length > 1: | ||
| result.append(id_reply_for) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(generate_chat_history()) | ||
| print(find_id_who_write_the_most_messages(generate_chat_history())) | ||
| print('_' * 75) | ||
| print(find_id_who_got_the_most_replies(generate_chat_history())) | ||
| print('_' * 75) | ||
| print(find_id_saw_unique_users(generate_chat_history())) | ||
| print('_' * 75) | ||
| print(what_time_more_messages(generate_chat_history())) | ||
| print('_' * 75) | ||
| print(maximum_thread_length(generate_chat_history())) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,25 @@ | ||
| # Вывести последнюю букву в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
|
|
||
| print(word[-1]) | ||
|
|
||
| # Вывести количество букв "а" в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
|
|
||
| print(word.lower().count('а')) | ||
|
|
||
| # Вывести количество гласных букв в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
|
|
||
| vowels = 'аеиоуэюяыё' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| print(len([letter for letter in word.lower() if letter in vowels])) | ||
|
|
||
| # Вывести количество слов в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
|
|
||
| print(len(sentence.split())) | ||
|
|
||
| # Вывести первую букву каждого слова на отдельной строке | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
|
|
||
| for word in sentence.split(): | ||
| print(word[0]) | ||
|
|
||
| # Вывести усреднённую длину слова в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| print(int(sum([len(word) for word in sentence.split()]) / len(sentence.split()))) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тот редкий случай когда строчка комментария не помешала бы.