From 5e9bb3e314ba70f3c2e065cbc2e30c3442740563 Mon Sep 17 00:00:00 2001 From: Anna Date: Thu, 5 Nov 2026 14:37:46 +0100 Subject: [PATCH] quadrat&ungerade zahlen --- funcs/07_quadratzahlen.py | 20 ++++++++++++++++++++ funcs/08_ungerade_zahlen.py | 12 ++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 funcs/07_quadratzahlen.py create mode 100644 funcs/08_ungerade_zahlen.py diff --git a/funcs/07_quadratzahlen.py b/funcs/07_quadratzahlen.py new file mode 100644 index 0000000..96193da --- /dev/null +++ b/funcs/07_quadratzahlen.py @@ -0,0 +1,20 @@ + + +class Squares: + def __init__(self, n): + self.n = n + self.current = 0 + + def __iter__(self): + return self + + def __next__(self): + if self.current >= self.n: + raise StopIteration + self.current += 1 + return self.current ** 2 + +for q in Squares(5): + print(q) # 1, 4, 9, 16, 25 + +print(list(Squares(5))) # [1, 4, 9, 16, 25] \ No newline at end of file diff --git a/funcs/08_ungerade_zahlen.py b/funcs/08_ungerade_zahlen.py new file mode 100644 index 0000000..746ebb7 --- /dev/null +++ b/funcs/08_ungerade_zahlen.py @@ -0,0 +1,12 @@ + + +def odd(n: int): + for i in range(1, n + 1, 2): + yield i + +print(list(odd(10))) # [1, 3, 5, 7, 9] +print(list(odd(7))) # [1, 3, 5, 7] + +gen = odd(5) +print(next(gen)) # 1 +print(next(gen)) # 3 \ No newline at end of file