-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson05 #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: master
Are you sure you want to change the base?
Lesson05 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Задание №1 | ||
| # Пользователь вводит данные о количестве предприятий, их наименования и прибыль за 4 квартал | ||
| # (т.е. 4 числа) для каждого предприятия. Программа должна определить среднюю прибыль (за год для | ||
| # всех предприятий) и отдельно вывести наименования предприятий, чья прибыль выше среднего и ниже | ||
| # среднего. | ||
|
|
||
| import collections | ||
|
|
||
| n = int(input("Введите количество предприятий")) | ||
| ent = collections.defaultdict(list) | ||
| for i in range(n): | ||
| name = input('Введите название предприятия №{0} '.format(i)) | ||
| for j in range(4): | ||
| prib = int(input('Введите прибыль за квартал №{0} '.format(j))) | ||
| ent[name].append(prib) | ||
| avg_arr = [sum(ent[x])/4 for x in ent] | ||
|
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. Но не экономьте на пробелах. |
||
| avg = sum(avg_arr)/n | ||
| print("Средняя прибыль предприятий за год:", avg) | ||
| print("Список предприятий прибыль выше среднего:") | ||
| print([x for x in ent if sum(ent[x])/4 > avg]) | ||
| print("Список предприятий прибыль меньше среднего:") | ||
| print([x for x in ent if sum(ent[x])/4 < avg]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Задание №2 | ||
| # Написать программу сложения и умножения двух шестнадцатеричных чисел. При этом каждое число | ||
| # представляется как массив, элементы которого — цифры числа. Например, пользователь | ||
| # ввёл A2 и C4F. Нужно сохранить их как [‘A’, ‘2’] и [‘C’, ‘4’, ‘F’] соответственно. Сумма чисел | ||
| # из примера: [‘C’, ‘F’, ‘1’], произведение - [‘7’, ‘C’, ‘9’, ‘F’, ‘E’]. | ||
|
|
||
| import collections | ||
|
|
||
|
|
||
| def get_dict(str_): | ||
| counter = collections.Counter() | ||
| len_str = len(str_) | ||
| for i in range(len_str): | ||
| counter[i] = int(str_[len(str_) - i - 1], 16) # int(str_[i]) | ||
| return counter | ||
|
|
||
| def get_hex_result(coll): | ||
| perenos = 0; res = "" | ||
|
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 i in coll: | ||
| coll[i] = coll[i] + perenos | ||
| if coll[i] > 16: | ||
| perenos = coll[i] // 16 | ||
| coll[i] = coll[i] % 16 | ||
| else: | ||
| perenos = 0 | ||
| res = hex(coll[i])[2:].upper() + res | ||
|
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. Если придираться, то можно тут и ниже вставить текст задания "Примечание: Если воспользоваться функциями hex() и/или int() для преобразования систем счисления, задача решается в несколько строк. Для прокачки алгоритмического мышления такой вариант не подходит. Поэтому использование встроенных функций для перевода из одной системы счисления в другую в данной задаче под запретом." |
||
| else: | ||
| if perenos > 0: | ||
| res = hex(perenos)[2:].upper() + res | ||
| return res | ||
|
|
||
| def addition(x1,x2): | ||
| sum_coll = get_dict(x1) + get_dict(x2) | ||
| return get_hex_result(sum_coll) | ||
|
|
||
| def mult(x1,x2): | ||
| count1 = get_dict(x1) | ||
| count2 = get_dict(x2) | ||
| counter = collections.Counter() | ||
| for i in count1: | ||
| for j in count2: | ||
| counter[j+i] = counter[j+i] + count1[i]*count2[j] | ||
| return get_hex_result(counter) | ||
|
|
||
| x1 = input("Введите первое шестнадцатиричное число") | ||
| x2 = input("Введите второе шестнадцатиричное число") | ||
|
|
||
| print("Сумма:", addition(x1, x2)) | ||
|
|
||
| print("Произведение:", mult(x1, x2)) | ||
|
|
||
This file was deleted.
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.
Отличный вариант решения