8 Feb 2015

Tower of Hanoi

#Python

'''
Recursive program to implement Tower of Hanoi for n rings and 3 stacks
'''

def printMove(fr, to):
    print('move from ' + str(fr) + ' to ' + str(to))

def Towers(n, fr, to, spare):
    if n == 1:
        printMove(fr, to)
    else:
        Towers(n-1, fr, spare, to)
        Towers(1, fr, to, spare)
        Towers(n-1, spare, to, fr)




Categories: , ,

Related Posts:

  • Minimum Monthly Payment of Credit Card using Bisection Search #Python ''' Program to calculate minimum Monthly Payment of a Credit Card using Bisection Search ''' balance = 320000annualInterestRate = 0.2newbalance=balancei=1monthlyInterestRate=annualInterestRate/12.0lowerb=newbalan… Read More
  • Monthly Payment of a Credit Card #Python #Program to calculate minimum Monthly Payment of a Credit Cardbalance = 3329annualInterestRate = 0.2minimumMonthlyPay=100newbalance=balancei=1monthlyInterestRate=annualInterestRate/12.0while(i<=12):  &… Read More
  • The 6.00 Word Game #Python   # 6.00x Problem Set 4A# The 6.00 Word Gameimport randomimport stringVOWELS = 'aeiou'CONSONANTS = 'bcdfghjklmnpqrstvwxyz'HAND_SIZE = 7SCRABBLE_LETTER_VALUES = {    'a': 1, 'b': 3, 'c': 3, 'd': 2,… Read More
  • Hangman game # Python # Problem Set 3 # Hangman game import randomimport stringWORDLIST_FILENAME = "words.txt"def loadWords():    """    Returns a list of valid words. Words are strings of lowercase le… Read More
  • The 6.00 Word Game # Python # The 6.00 Word Game # Problem Set 4A   from ps4a import *import time# Problem #6: Computer chooses a worddef compChooseWord(hand, wordList, n):    """    Given a hand and a wordL… Read More

0 comments:

Post a Comment

Copyright © 2025 UPgradeCODING | Powered by Blogger