diff --git a/fakultaet.py b/fakultaet.py index 5660151..43e5797 100644 --- a/fakultaet.py +++ b/fakultaet.py @@ -4,4 +4,11 @@ def fakultaet(n): else: return n * fakultaet(n - 1) -print(fakultaet(5)) \ No newline at end of file +def fakultaet_loop(n): + f = 1 + + for i in range(n, 1, -1): + f *= i + return f +print(fakultaet(5)) +print(fakultaet_loop(5)) \ No newline at end of file diff --git a/lambda_lc_while.py b/lambda_lc_while.py new file mode 100644 index 0000000..b0d092b --- /dev/null +++ b/lambda_lc_while.py @@ -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) \ No newline at end of file