python-kurs-softed/loesungen/dictionaries.py

152 lines
3.3 KiB
Python
Raw Normal View History

# Ausgangslage
salaries_1 = {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}
salaries_2 = {'Barbara': 3000, 'Elke': 3300, 'Michael': 2800, 'Johann': 2000}
# Aufgabe 1:
salaries_all = salaries_1.copy()
salaries_all.update(salaries_2)
print(salaries_all)
print("-" * 50)
# Aufgabe 2:
employee = ""
max_salary = 0
sum_salary = 0
for name, salary in salaries_all.items():
if salary > max_salary:
max_salary = salary
employee = name
sum_salary += salary
print("Topverdiener:", employee, max_salary)
print("-" * 50)
# Aufgabe 3:
print("Summe:", sum_salary)
# Aufgabe 3 alternativ:
print("Summe:", sum(salaries_all.values()))
print("-" * 50)
# Aufgabe 4:
salaries_new = {}
for key in salaries_all:
salaries_new[key] = salaries_all[key] * 1.1
# Aufgabe 5:
heading = "Name Gehalt"
print(heading)
print("-" * len(heading))
for name, salary in salaries_new.items():
print(name.ljust(8), int(salary))
print("-" * 50)
# Aufgabe 6:
heading = "Name Gehalt"
print(heading)
print("-" * len(heading))
for name, salary in salaries_all.items():
print(name.ljust(8), int(salary))
print("-" * 50)
# Aufgabe 5 & 6 mit Funktion
def print_salary_list(data, col_width=8):
"""Print a formatted list of employees and their salary.
Expects a a dictionary as the first argument:
{
"name": salary,
...
}
"""
heading = "Name".ljust(col_width) + " Gehalt"
print(heading)
print("-" * len(heading))
for name, salary in data.items():
print(name.ljust(col_width), int(salary))
print_salary_list(salaries_new, 10)
print("-" * 50)
print_salary_list(salaries_all, col_width=10)
print("-" * 50)
# Bonusaufgabe 1:
def print_ordered_salary_list(data, col_width=8):
heading = "Name".ljust(col_width) + "Gehalt"
print(heading)
print("-" * len(heading))
salary_first = []
for name, salary in data.items():
salary_first.append((salary, name))
for salary, name in sorted(salary_first, reverse=True):
print(name.ljust(col_width), int(salary))
print_ordered_salary_list(salaries_new)
print("-" * 50)
# Bonusaufgabe 2:
def print_enumerated_salary_list(data, col_width=8):
heading = "Nr. " + "Name".ljust(col_width) + "Gehalt"
print(heading)
print("-" * len(heading))
salary_first = []
for name, salary in data.items():
salary_first.append((salary, name))
for i, (salary, name) in enumerate(sorted(salary_first, reverse=True)):
print(str(i+1).rjust(3), name.ljust(col_width), int(salary))
print_enumerated_salary_list(salaries_new)
print("-" * 50)
# Bonusaufgabe 3:
print("Gehaltsdurchschnitt:", sum(salaries_new.values()) / len(salaries_new))
print("-" * 50)
# Bonusaufgabe 4:
def print_extended_salary_list(data, col_width=8):
mean_salary = sum(data.values()) / len(data)
heading = "Nr. " + "Name".ljust(col_width + 1) + "Gehalt".ljust(col_width +1) + "% d. ø"
print(heading)
print("-" * len(heading))
salary_first = []
for name, salary in data.items():
salary_first.append((salary, name))
for i, (salary, name) in enumerate(sorted(salary_first, reverse=True)):
print(str(i+1).rjust(3), name.ljust(col_width), str(int(salary)).ljust(col_width), round(salary / (mean_salary / 100), 2))
print_extended_salary_list(salaries_new)