forked from nehirozdemir/wise2324
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_sheet_09.py
90 lines (62 loc) · 1.99 KB
/
exercise_sheet_09.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Vorlage zu Übungsblatt 09
Dieses Dokument dient Ihnen als Vorlage für Ihre Abgabe zum aktuellen
Übungsblatt. Sie finden weiter unten mehrere Abschnitte, passend zu
den einzelnen Aufgaben des Übungsblatts.
"""
KNIGHTS = [("Arthur", 34), ("Bedevere", 33), ("Galahad", 32), ("Lancelot", 36)]
# Aufgabe 1
def sort_by_age(knights):
"""
Sort knights by age in ascending order
Ref: https://stackoverflow.com/questions/10695139/sort-a-list-
of-tuples-by-2nd-item-integer-value
"""
return sorted(knights, key=lambda x: x[1])
# Aufgabe 2
def young_knights(knights):
"""
Filter knights younger than 34
Ref: https://ipcisco.com/lesson/python-lambda-function/
"""
return list(filter(lambda x: (x[1] < 34), knights))
# Aufgabe 3
def get_knight_names(knights):
"""
Returns only the knight name
Ref: https://www.tutorialspoint.com/how-to-get-the-first-element-in-list-of-tuples-in-python
"""
return [knight[0] for knight in knights]
# Aufgabe 4
def convert_to_dict(knights):
"""
Gives list's tuples in the form of dict elements
Ref:https://www.tutorialspoint.com/python-program-to-convert-dictionary-values-to-strings
"""
return {key: str(value) for key, value in dict(knights).items()}
# Aufgabe 5
def prime_numbers():
"""
A prime number is a whole number greater than 1
whose only factors are 1 and itself.
"""
num = 2
while True:
prime = 0
# i is always x minus 1
# thus, we never divide x to itself
for i in range(2, num):
if num > 1 and num % i == 0:
prime += 1
if prime == 0:
yield num
num += 1
# Testaufrufe
if __name__ == "__main__":
print(sort_by_age(KNIGHTS))
print(young_knights(KNIGHTS))
print(get_knight_names(KNIGHTS))
print(convert_to_dict(KNIGHTS))
NUMBERS = prime_numbers()
print([next(NUMBERS) for _ in range(20) if NUMBERS is not None])