From d6f9b8c68a077d9cb604ea6dbbbe4766bb80e17b Mon Sep 17 00:00:00 2001 From: ericries Date: Sat, 1 Jan 2022 17:04:12 -0800 Subject: [PATCH 1/4] Add files via upload simple Python implementation of Tower - apologies for any errors, I only had a minute to dash this off --- tower.py | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 tower.py diff --git a/tower.py b/tower.py new file mode 100644 index 00000000..72c67a61 --- /dev/null +++ b/tower.py @@ -0,0 +1,159 @@ +import sys + +class Disk: + def __init__(self, size): + self.__size = size + + def size(self): + return self.__size + + def print(self): + print("[ %s ]" % self.size()) + +class Tower: + def __init__(self): + self.__disks = [] + + def empty(self): + return len(self.__disks) == 0 + + def top(self): + if self.empty(): + return None + else: + return self.__disks[-1] + + def add(self, disk): + if not self.empty(): + t = self.top() + if disk.size() > t.size(): + raise Exception("YOU CAN'T PLACE A LARGER DISK ON TOP OF A SMALLER ONE, IT MIGHT CRUSH IT!") + self.__disks.append(disk) + + def pop(self): + if self.empty(): + raise Exception("empty pop") + return self.__disks.pop() + + def print(self): + r = "Needle: [%s]" % (", ".join([str(x.size()) for x in self.__disks])) + print(r) + + + +print(""" +IN THIS PROGRAM, WE SHALL REFER TO DISKS BY NUMERICAL CODE. +3 WILL REPRESENT THE SMALLEST DISK, 5 THE NEXT SIZE, +7 THE NEXT, AND SO ON, UP TO 15. IF YOU DO THE PUZZLE WITH +2 DISKS, THEIR CODE NAMES WOULD BE 13 AND 15. WITH 3 DISKS +THE CODE NAMES WOULD BE 11, 13 AND 15, ETC. THE NEEDLES +ARE NUMBERED FROM LEFT TO RIGHT, 1 TO 3. WE WILL +START WITH THE DISKS ON NEEDLE 1, AND ATTEMPT TO MOVE THEM +TO NEEDLE 3. + +GOOD LUCK! + +""") + + + + +class Game: + def __init__(self): + self.__sizes = [3, 5, 7] # ,9,11,13,15] + self.__sizes.sort() + + self.__towers = [] + self.__moves = 0 + self.__towers = [Tower(), Tower(), Tower()] + self.__sizes.reverse() + for size in self.__sizes: + disk = Disk(size) + self.__towers[0].add(disk) + + def winner(self): + return self.__towers[0].empty() and self.__towers[1].empty() + + def print(self): + for t in self.__towers: + t.print() + + def moves(self): + return self.__moves + + def which_disk(self): + w = int(input("WHICH DISK WOULD YOU LIKE TO MOVE\n")) + if w in self.__sizes: + return w + else: + raise Exception() + + def pick_disk(self): + which = None + while which is None: + try: + which = self.which_disk() + except: + print("ILLEGAL ENTRY... YOU MAY ONLY TYPE 3,5,7,9,11,13, OR 15.\n") + + valids = [t for t in self.__towers if t.top() and t.top().size() == which] + assert len(valids) in (0, 1) + if not valids: + print("THAT DISK IS BELOW ANOTHER ONE. MAKE ANOTHER CHOICE.\n") + return None + else: + assert valids[0].top().size() == which + return valids[0] + + def which_tower(self): + try: + needle = int(input("PLACE DISK ON WHICH NEEDLE\n")) + tower = self.__towers[needle - 1] + except: + print("I'LL ASSUME YOU HIT THE WRONG KEY THIS TIME. BUT WATCH IT,\nI ONLY ALLOW ONE MISTAKE.\n") + return None + else: + return tower + + def take_turn(self): + from_tower = None + while from_tower is None: + from_tower = self.pick_disk() + + to_tower = self.which_tower() + if not to_tower: + to_tower = self.which_tower() + + if not to_tower: + print("I TRIED TO WARN YOU, BUT YOU WOULDN'T LISTEN.\nBYE BYE, BIG SHOT.\n") + sys.exit(0) + + disk = from_tower.pop() + try: + to_tower.add( disk ) + self.__moves += 1 + except Exception as err: + print(err) + from_tower.add(disk) + +game = Game() +while True: + game.print() + + game.take_turn() + + if game.winner(): + print("CONGRATULATIONS!!\nYOU HAVE PERFORMED THE TASK IN %s MOVES.\n" % game.moves()) + while True: + yesno = input("TRY AGAIN (YES OR NO)\n") + if yesno.upper() == "YES": + game = Game() + break + elif yesno.upper() == "NO": + print("THANKS FOR THE GAME!\n") + sys.exit(0) + else: + print("'YES' OR 'NO' PLEASE\n") + elif game.moves() > 128: + print("SORRY, BUT I HAVE ORDERS TO STOP IF YOU MAKE MORE THAN 128 MOVES.") + sys.exit(0) \ No newline at end of file From 5e2647682f1fde87702de7ea4f9786143172e47f Mon Sep 17 00:00:00 2001 From: ericries Date: Sat, 1 Jan 2022 17:05:56 -0800 Subject: [PATCH 2/4] Add files via upload Simple python implementation. Apologies for any errors, I only had a minute to whip this up --- 90_Tower/python/tower.py | 159 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 90_Tower/python/tower.py diff --git a/90_Tower/python/tower.py b/90_Tower/python/tower.py new file mode 100644 index 00000000..72c67a61 --- /dev/null +++ b/90_Tower/python/tower.py @@ -0,0 +1,159 @@ +import sys + +class Disk: + def __init__(self, size): + self.__size = size + + def size(self): + return self.__size + + def print(self): + print("[ %s ]" % self.size()) + +class Tower: + def __init__(self): + self.__disks = [] + + def empty(self): + return len(self.__disks) == 0 + + def top(self): + if self.empty(): + return None + else: + return self.__disks[-1] + + def add(self, disk): + if not self.empty(): + t = self.top() + if disk.size() > t.size(): + raise Exception("YOU CAN'T PLACE A LARGER DISK ON TOP OF A SMALLER ONE, IT MIGHT CRUSH IT!") + self.__disks.append(disk) + + def pop(self): + if self.empty(): + raise Exception("empty pop") + return self.__disks.pop() + + def print(self): + r = "Needle: [%s]" % (", ".join([str(x.size()) for x in self.__disks])) + print(r) + + + +print(""" +IN THIS PROGRAM, WE SHALL REFER TO DISKS BY NUMERICAL CODE. +3 WILL REPRESENT THE SMALLEST DISK, 5 THE NEXT SIZE, +7 THE NEXT, AND SO ON, UP TO 15. IF YOU DO THE PUZZLE WITH +2 DISKS, THEIR CODE NAMES WOULD BE 13 AND 15. WITH 3 DISKS +THE CODE NAMES WOULD BE 11, 13 AND 15, ETC. THE NEEDLES +ARE NUMBERED FROM LEFT TO RIGHT, 1 TO 3. WE WILL +START WITH THE DISKS ON NEEDLE 1, AND ATTEMPT TO MOVE THEM +TO NEEDLE 3. + +GOOD LUCK! + +""") + + + + +class Game: + def __init__(self): + self.__sizes = [3, 5, 7] # ,9,11,13,15] + self.__sizes.sort() + + self.__towers = [] + self.__moves = 0 + self.__towers = [Tower(), Tower(), Tower()] + self.__sizes.reverse() + for size in self.__sizes: + disk = Disk(size) + self.__towers[0].add(disk) + + def winner(self): + return self.__towers[0].empty() and self.__towers[1].empty() + + def print(self): + for t in self.__towers: + t.print() + + def moves(self): + return self.__moves + + def which_disk(self): + w = int(input("WHICH DISK WOULD YOU LIKE TO MOVE\n")) + if w in self.__sizes: + return w + else: + raise Exception() + + def pick_disk(self): + which = None + while which is None: + try: + which = self.which_disk() + except: + print("ILLEGAL ENTRY... YOU MAY ONLY TYPE 3,5,7,9,11,13, OR 15.\n") + + valids = [t for t in self.__towers if t.top() and t.top().size() == which] + assert len(valids) in (0, 1) + if not valids: + print("THAT DISK IS BELOW ANOTHER ONE. MAKE ANOTHER CHOICE.\n") + return None + else: + assert valids[0].top().size() == which + return valids[0] + + def which_tower(self): + try: + needle = int(input("PLACE DISK ON WHICH NEEDLE\n")) + tower = self.__towers[needle - 1] + except: + print("I'LL ASSUME YOU HIT THE WRONG KEY THIS TIME. BUT WATCH IT,\nI ONLY ALLOW ONE MISTAKE.\n") + return None + else: + return tower + + def take_turn(self): + from_tower = None + while from_tower is None: + from_tower = self.pick_disk() + + to_tower = self.which_tower() + if not to_tower: + to_tower = self.which_tower() + + if not to_tower: + print("I TRIED TO WARN YOU, BUT YOU WOULDN'T LISTEN.\nBYE BYE, BIG SHOT.\n") + sys.exit(0) + + disk = from_tower.pop() + try: + to_tower.add( disk ) + self.__moves += 1 + except Exception as err: + print(err) + from_tower.add(disk) + +game = Game() +while True: + game.print() + + game.take_turn() + + if game.winner(): + print("CONGRATULATIONS!!\nYOU HAVE PERFORMED THE TASK IN %s MOVES.\n" % game.moves()) + while True: + yesno = input("TRY AGAIN (YES OR NO)\n") + if yesno.upper() == "YES": + game = Game() + break + elif yesno.upper() == "NO": + print("THANKS FOR THE GAME!\n") + sys.exit(0) + else: + print("'YES' OR 'NO' PLEASE\n") + elif game.moves() > 128: + print("SORRY, BUT I HAVE ORDERS TO STOP IF YOU MAKE MORE THAN 128 MOVES.") + sys.exit(0) \ No newline at end of file From fb7b7ffe0e7eea768951e31a3e876149f86803bf Mon Sep 17 00:00:00 2001 From: ericries Date: Sat, 1 Jan 2022 17:08:10 -0800 Subject: [PATCH 3/4] Delete tower.py --- tower.py | 159 ------------------------------------------------------- 1 file changed, 159 deletions(-) delete mode 100644 tower.py diff --git a/tower.py b/tower.py deleted file mode 100644 index 72c67a61..00000000 --- a/tower.py +++ /dev/null @@ -1,159 +0,0 @@ -import sys - -class Disk: - def __init__(self, size): - self.__size = size - - def size(self): - return self.__size - - def print(self): - print("[ %s ]" % self.size()) - -class Tower: - def __init__(self): - self.__disks = [] - - def empty(self): - return len(self.__disks) == 0 - - def top(self): - if self.empty(): - return None - else: - return self.__disks[-1] - - def add(self, disk): - if not self.empty(): - t = self.top() - if disk.size() > t.size(): - raise Exception("YOU CAN'T PLACE A LARGER DISK ON TOP OF A SMALLER ONE, IT MIGHT CRUSH IT!") - self.__disks.append(disk) - - def pop(self): - if self.empty(): - raise Exception("empty pop") - return self.__disks.pop() - - def print(self): - r = "Needle: [%s]" % (", ".join([str(x.size()) for x in self.__disks])) - print(r) - - - -print(""" -IN THIS PROGRAM, WE SHALL REFER TO DISKS BY NUMERICAL CODE. -3 WILL REPRESENT THE SMALLEST DISK, 5 THE NEXT SIZE, -7 THE NEXT, AND SO ON, UP TO 15. IF YOU DO THE PUZZLE WITH -2 DISKS, THEIR CODE NAMES WOULD BE 13 AND 15. WITH 3 DISKS -THE CODE NAMES WOULD BE 11, 13 AND 15, ETC. THE NEEDLES -ARE NUMBERED FROM LEFT TO RIGHT, 1 TO 3. WE WILL -START WITH THE DISKS ON NEEDLE 1, AND ATTEMPT TO MOVE THEM -TO NEEDLE 3. - -GOOD LUCK! - -""") - - - - -class Game: - def __init__(self): - self.__sizes = [3, 5, 7] # ,9,11,13,15] - self.__sizes.sort() - - self.__towers = [] - self.__moves = 0 - self.__towers = [Tower(), Tower(), Tower()] - self.__sizes.reverse() - for size in self.__sizes: - disk = Disk(size) - self.__towers[0].add(disk) - - def winner(self): - return self.__towers[0].empty() and self.__towers[1].empty() - - def print(self): - for t in self.__towers: - t.print() - - def moves(self): - return self.__moves - - def which_disk(self): - w = int(input("WHICH DISK WOULD YOU LIKE TO MOVE\n")) - if w in self.__sizes: - return w - else: - raise Exception() - - def pick_disk(self): - which = None - while which is None: - try: - which = self.which_disk() - except: - print("ILLEGAL ENTRY... YOU MAY ONLY TYPE 3,5,7,9,11,13, OR 15.\n") - - valids = [t for t in self.__towers if t.top() and t.top().size() == which] - assert len(valids) in (0, 1) - if not valids: - print("THAT DISK IS BELOW ANOTHER ONE. MAKE ANOTHER CHOICE.\n") - return None - else: - assert valids[0].top().size() == which - return valids[0] - - def which_tower(self): - try: - needle = int(input("PLACE DISK ON WHICH NEEDLE\n")) - tower = self.__towers[needle - 1] - except: - print("I'LL ASSUME YOU HIT THE WRONG KEY THIS TIME. BUT WATCH IT,\nI ONLY ALLOW ONE MISTAKE.\n") - return None - else: - return tower - - def take_turn(self): - from_tower = None - while from_tower is None: - from_tower = self.pick_disk() - - to_tower = self.which_tower() - if not to_tower: - to_tower = self.which_tower() - - if not to_tower: - print("I TRIED TO WARN YOU, BUT YOU WOULDN'T LISTEN.\nBYE BYE, BIG SHOT.\n") - sys.exit(0) - - disk = from_tower.pop() - try: - to_tower.add( disk ) - self.__moves += 1 - except Exception as err: - print(err) - from_tower.add(disk) - -game = Game() -while True: - game.print() - - game.take_turn() - - if game.winner(): - print("CONGRATULATIONS!!\nYOU HAVE PERFORMED THE TASK IN %s MOVES.\n" % game.moves()) - while True: - yesno = input("TRY AGAIN (YES OR NO)\n") - if yesno.upper() == "YES": - game = Game() - break - elif yesno.upper() == "NO": - print("THANKS FOR THE GAME!\n") - sys.exit(0) - else: - print("'YES' OR 'NO' PLEASE\n") - elif game.moves() > 128: - print("SORRY, BUT I HAVE ORDERS TO STOP IF YOU MAKE MORE THAN 128 MOVES.") - sys.exit(0) \ No newline at end of file From 437b4c29205c2a5eb5a338215eeb9a5207130e1d Mon Sep 17 00:00:00 2001 From: ericries Date: Sat, 1 Jan 2022 17:26:04 -0800 Subject: [PATCH 4/4] Update tower.py easier debugging with fewer disks --- 90_Tower/python/tower.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/90_Tower/python/tower.py b/90_Tower/python/tower.py index 72c67a61..5e4a25b7 100644 --- a/90_Tower/python/tower.py +++ b/90_Tower/python/tower.py @@ -60,7 +60,10 @@ GOOD LUCK! class Game: def __init__(self): - self.__sizes = [3, 5, 7] # ,9,11,13,15] + # use fewer sizes to make debugging easier + # self.__sizes = [3, 5, 7] # ,9,11,13,15] + self.__sizes = [3, 5, 7, 9, 11, 13, 15] + self.__sizes.sort() self.__towers = [] @@ -156,4 +159,4 @@ while True: print("'YES' OR 'NO' PLEASE\n") elif game.moves() > 128: print("SORRY, BUT I HAVE ORDERS TO STOP IF YOU MAKE MORE THAN 128 MOVES.") - sys.exit(0) \ No newline at end of file + sys.exit(0)