sw3 Übungen, codewars MenuDisplay
This commit is contained in:
1 parent
9e5606d464
commit
986a26890a
3 files changed
+69
-13
No files matched your search
@@ -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())
|
||||
Reference in new issue
Block a user