Definition: The "candidate" is the smallest available ring that has not been moved on the most recent move. Definition: A ring is "available" if it is on the top of one of the towers. Your program will prompt the user for the number of rings. We may never put a larger numbered ring on top of a smaller numbered one. We can only move one ring at a time, and 2. We start with n rings on tower a and we need to transfer them to tower b subject to the following restrictions: 1. The problem: You are given three towers a, b, and c. With luck, I haven't butchered the translated strings too badly.Non-recursive solution for Towers of Hanoi Using the algorithm discussed in class, write an iterative program to solve the Towers of Hanoi problem. hanoi.py on my machine, gives: Podaj ilość dysków: 2 Run msgfmt on that to generate an hanoi.mo file, and store it the subdirectory: pl/LC_MESSAGES. If you run pygettext on this, you can make a hanoi.pot template file, copy it to hanoi.po and put translations into it: msgid "Moves:" Print(_('Minimal moves:'), minimum_moves)ĭiscs = int(input(_('Enter the number of disks: '))) Peg = peg + peg # Rotate the peg ordering Source, destination = possible_move(peg, peg) Move(peg, peg) # Smallest disc now on peg Gettext.install('hanoi', Path(_file_).parent) You can keep track of which order you need with a list: if discs % 2 = 1:Īnd move the smallest disc from peg to peg, without having to hunt for which peg the smallest disc is on: move(peg, peg)Īnd later rotate the peg list: peg = peg + peg # -> -> -> Īfter moving the smallest disc onto peg, the only possible moves for the larger disc will be peg -> peg or peg -> peg, so you can greatly simplify the possible move determination, by just looking at those two pegs: source, destination = possible_move(peg, peg) You always move the smallest disc either: While len(c) != discs: # A simpler termination conditionīut I've a different option. move_smallest_disc = move_left if disc % 2 != 0 else move_right You could move this decision out of the loop. Since the number of discs is constant, you always make the same choice. You should compute the result once, save it in a temporary, and use the temporary variable for further tests and assignments: for peg in peg_dict.values():Įach iteration, you check if the number of discs was even or odd, and call the moveLeft() or moveRight() function. You do not need the key at all, and could simply iterate over the contents of the dictionary: for peg in peg_dict.values():īut notice we are computing using possible_move(peg) twice. In fact, you never use the key for anything else. In this code, you are iterating over the PegDict, fetching the keys, and using the key to look up the dictionary value. Iterating over a container for key in PegDict: Then, if you change how the discs are shown (curses, GUI. ![]() Instead of repeating the code, you should move this into a function. Once moving the small disc, once moving a larger disc. Therefore, these operations could easily be combine into one statement: def move(source, destination): pop() returns the item removed from the list, which is the value you used fromm to retrieve. My personal preference is to use synonyms. The PEP-8 recommendation is a trailing underscore: from_. I'm going to assume fromm is not a spelling error, but rather avoiding the from keyword. You are already using the range() method to generate the disc numbers you could simply create a list directly from the result: a = list(range(discs, 0, -1)) Thus, this could be written slightly more compactly: moves_needed = 2 ** discs - 1 Python has the ** operator, for exponentiation. You mostly follow this, except for the print('Moves: '+ str(moves)) statements at the end.Įxponentiation movesNeeded = pow(2, discs) - 1 ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |