sw3 Übungen, codewars MenuDisplay

This commit is contained in:
schaermicha1 committed 2026-03-12 11:54:10 +01:00
1 parent 9e5606d464
commit 986a26890a
3 files changed
+69 -13

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
class Menu:
def __init__(self, selects: list):
self.selects = selects
self.cursor = 0
def to_the_right(self):
self.cursor = (self.cursor + 1) % len(self.selects)
def to_the_left(self):
self.cursor = (self.cursor - 1) % len(self.selects)
def display(self):
return (
"["
+ ", ".join(
[
f"'{s}'" if i != self.cursor else f"['{s}']"
for i, s in enumerate(self.selects)
]
)
+ "]"
)
if __name__ == "__main__":
menu = Menu(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"])
print(menu.display())
menu.to_the_right()
print(menu.display())
menu.to_the_left()
menu.to_the_left()
menu.to_the_left()
print(menu.display())
menu.to_the_left()
menu.to_the_left()
print(menu.display())