-
Notifications
You must be signed in to change notification settings - Fork 0
first commit #5
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
base: main
Are you sure you want to change the base?
first commit #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """ | ||
| 1. Пользователь вводит данные о количестве предприятий, их наименования и прибыль | ||
| за 4 квартала (т.е. 4 отдельных числа) для каждого предприятия. | ||
| Программа должна определить среднюю прибыль (за год для всех предприятий) | ||
| и вывести наименования предприятий, чья прибыль выше среднего и отдельно | ||
| вывести наименования предприятий, чья прибыль ниже среднего. | ||
| Подсказка: | ||
| Для решения задачи обязательно примените какую-нибудь коллекцию из модуля collections. | ||
| Пример: | ||
| Введите количество предприятий для расчета прибыли: 2 | ||
| Введите название предприятия: Фирма_1 | ||
| через пробел введите прибыль данного предприятия | ||
| за каждый квартал(Всего 4 квартала): 235 345634 55 235 | ||
| Введите название предприятия: Фирма_2 | ||
| через пробел введите прибыль данного предприятия | ||
| за каждый квартал(Всего 4 квартала): 345 34 543 34 | ||
| Средняя годовая прибыль всех предприятий: 173557.5 | ||
| Предприятия, с прибылью выше среднего значения: Фирма_1 | ||
| Предприятия, с прибылью ниже среднего значения: Фирма_2 | ||
| """ | ||
|
|
||
| from collections import namedtuple | ||
|
|
||
|
|
||
| class Firm: | ||
|
|
||
| def __init__(self): | ||
| firm_data = namedtuple('Firm_Data', 'name income') | ||
| self.data = firm_data(name=input('Введите название предприятия: '), income=self.income_input()) | ||
|
|
||
| def income_input(self): | ||
| income = input('через пробел введите прибыль данного предприятия за каждый квартал(Всего 4 квартала): ').split() | ||
| if len(income) == 4: | ||
| try: | ||
| return sum(list(map(int, income))) | ||
| except ValueError: | ||
| pass | ||
| self.income_input() | ||
|
|
||
|
|
||
| while True: | ||
| try: | ||
| firms_number = int(input('Введите количество предприятий для расчета прибыли: ')) | ||
| break | ||
| except ValueError: | ||
| continue | ||
|
|
||
| firms = [] | ||
| average = 0 | ||
|
|
||
| for i in range(firms_number): | ||
| firm_i = Firm() | ||
| firms.append(firm_i) | ||
| average += firm_i.data.income | ||
|
|
||
| average = average / len(firms) | ||
| above_average = [] | ||
| below_average = [] | ||
|
|
||
| for i in firms: | ||
| if i.data.income > average: | ||
| above_average.append(i.data.name) | ||
| else: | ||
| below_average.append(i.data.name) | ||
|
|
||
| print(f'Средняя годовая прибыль всех предприятий: {average}') | ||
| print(f'Предприятия, с прибылью выше среднего значения: {above_average}') | ||
| print(f'Предприятия, с прибылью ниже среднего значения: {below_average}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """ | ||
| 2. Написать программу сложения и умножения двух шестнадцатиричных чисел. | ||
| При этом каждое число представляется как массив, элементы которого это цифры числа. | ||
| Например, пользователь ввёл A2 и C4F. Сохранить их как [‘A’, ‘2’] и [‘C’, ‘4’, ‘F’] соответственно. | ||
| Сумма чисел из примера: [‘C’, ‘F’, ‘1’], произведение - [‘7’, ‘C’, ‘9’, ‘F’, ‘E’]. | ||
| Подсказка: | ||
| Для решения задачи обязательно примените какую-нибудь коллекцию из модуля collections | ||
| Для лучшее освоения материала можете даже сделать несколько решений этого задания, | ||
| применив несколько коллекций из модуля collections | ||
| Также попробуйте решить задачу вообще без collections и применить только ваши знания по ООП | ||
| (в частности по перегрузке методов) | ||
| __mul__ | ||
| __add__ | ||
| Пример: | ||
| Например, пользователь ввёл A2 и C4F. | ||
| Сохранить их как [‘A’, ‘2’] и [‘C’, ‘4’, ‘F’] соответственно. | ||
| Сумма чисел из примера: [‘C’, ‘F’, ‘1’] | ||
| Произведение - [‘7’, ‘C’, ‘9’, ‘F’, ‘E’]. | ||
| 1. вариант | ||
| defaultdict(list) | ||
| int(, 16) | ||
| reduce | ||
| 2. вариант | ||
| class HexNumber: | ||
| __add__ | ||
| __mul__ | ||
| hx = HexNumber | ||
| hx + hx | ||
| hex() | ||
| """ | ||
|
|
||
| from collections import defaultdict | ||
|
|
||
| hex_dct = defaultdict(list, {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}) | ||
|
|
||
|
|
||
| def hex_check(lst): | ||
| answer = [] | ||
| for i in lst: | ||
| if hex_dct[i] != []: | ||
| answer.append(i) | ||
| return answer | ||
|
|
||
|
|
||
| def hex_number(lst): | ||
| if len(lst) == 1: | ||
| return hex_dct[lst[0]] | ||
| else: | ||
| return hex_dct[lst[0]] * (16 ** (len(lst) - 1)) + hex_number(lst[1:]) | ||
|
|
||
|
|
||
| def dec_number(num): | ||
| return hex(num)[2:].upper() | ||
|
|
||
|
|
||
| def hex_sum(hex1, hex2): | ||
| return dec_number(hex_number(hex_check(hex1)) + hex_number(hex_check(hex2))) | ||
|
|
||
|
|
||
| def hex_mult(hex1, hex2): | ||
| return dec_number(hex_number(hex_check(hex1)) * hex_number(hex_check(hex2))) | ||
|
|
||
|
|
||
| first = list(input('Введите первое шестнадцатиричное число: ')) | ||
| second = list(input('Введите второе шестнадцатиричное число: ')) | ||
| print(f'Сумма чисел равна: {list(hex_sum(first, second))}') | ||
| print(f'Произведение чисел равно: {list(hex_mult(first, second))}') | ||
|
|
||
|
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. реализовали через defaultdict |
||
|
|
||
|
|
||
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.
реализовали через namedtuple, как мы и обсуждали на уроке