added file fakultaet.py and lambda_lc_while.py
This commit is contained in:
parent
3626d4e68a
commit
18497573e2
@ -4,4 +4,11 @@ def fakultaet(n):
|
|||||||
else:
|
else:
|
||||||
return n * fakultaet(n - 1)
|
return n * fakultaet(n - 1)
|
||||||
|
|
||||||
|
def fakultaet_loop(n):
|
||||||
|
f = 1
|
||||||
|
|
||||||
|
for i in range(n, 1, -1):
|
||||||
|
f *= i
|
||||||
|
return f
|
||||||
print(fakultaet(5))
|
print(fakultaet(5))
|
||||||
|
print(fakultaet_loop(5))
|
||||||
75
lambda_lc_while.py
Normal file
75
lambda_lc_while.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
li = [1, 3, 2, 6, 7, 3, 4, 3, 2, 5]
|
||||||
|
|
||||||
|
# 1.) Gerade Zahlen aus einer Liste extrahieren
|
||||||
|
|
||||||
|
# Aufgabe: Eine Liste mit allen geraden Zahlen zurückgeben
|
||||||
|
|
||||||
|
# a) for loop
|
||||||
|
def even_num_for(li):
|
||||||
|
even_list = []
|
||||||
|
|
||||||
|
for i in li:
|
||||||
|
if i % 2 == 0:
|
||||||
|
even_list.append(i)
|
||||||
|
return even_list
|
||||||
|
|
||||||
|
# b) lambda function with filter
|
||||||
|
def even_num_lambda(li):
|
||||||
|
return list(filter(lambda i: i % 2 == 0, li))
|
||||||
|
|
||||||
|
# c) list comprehension
|
||||||
|
def even_num_list_comprehension(li):
|
||||||
|
return [i for i in li if i % 2 == 0]
|
||||||
|
|
||||||
|
# d) while loop
|
||||||
|
def even_num_while(li):
|
||||||
|
i = 0
|
||||||
|
even_list = []
|
||||||
|
while i < len(li):
|
||||||
|
if li[i] % 2 == 0:
|
||||||
|
even_list.append(li[i])
|
||||||
|
i += 1
|
||||||
|
return even_list
|
||||||
|
|
||||||
|
print(even_num_for(li)) # a)
|
||||||
|
print(even_num_lambda(li)) # b)
|
||||||
|
print(even_num_list_comprehension(li)) # c)
|
||||||
|
print(even_num_while(li)) # d)
|
||||||
|
|
||||||
|
# 2.) Elemente umwandeln
|
||||||
|
|
||||||
|
# Aufgabe: Alle Elemente der Liste li in den Typ str umwandeln
|
||||||
|
|
||||||
|
# a) for loop
|
||||||
|
def to_string_for(li):
|
||||||
|
string_list = []
|
||||||
|
for i in li:
|
||||||
|
string_list.append(str(i))
|
||||||
|
return string_list
|
||||||
|
|
||||||
|
# b) lambda function
|
||||||
|
def to_string_lambda(li):
|
||||||
|
return list(map(lambda i: str(i), li))
|
||||||
|
|
||||||
|
# c) lambda function
|
||||||
|
def to_string_map(li):
|
||||||
|
return list(map(str, li))
|
||||||
|
|
||||||
|
# d) lsit comprehension
|
||||||
|
def to_string_list_comprehension(li):
|
||||||
|
return [str(i) for i in li]
|
||||||
|
|
||||||
|
# e) lsit comprehension
|
||||||
|
def to_string_while(li):
|
||||||
|
i = 0
|
||||||
|
string_list = []
|
||||||
|
while i < len(li):
|
||||||
|
string_list.append(str(li[i]))
|
||||||
|
i += 1
|
||||||
|
return string_list
|
||||||
|
|
||||||
|
print(to_string_for(li)) # a)
|
||||||
|
print(to_string_lambda(li)) # b)
|
||||||
|
print(to_string_map(li)) # c)
|
||||||
|
print(to_string_list_comprehension(li)) # d)
|
||||||
|
print(to_string_while(li)) # e)
|
||||||
Loading…
x
Reference in New Issue
Block a user