Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:42:20+00:00 2026-05-28T07:42:20+00:00

I was studying tower of hanoi recursive implementation in python. In my prgram I

  • 0

I was studying tower of hanoi recursive implementation in python. In my prgram I gave print at different points to know it better like

def hanoi(n, src, inm, dest):
    print "n=",n,"src=",src,"inm=",inm,"dest=",dest
    if n == 0:
        return
    hanoi(n-1, src, dest, inm)
    print src, '->', dest
    print n 
    hanoi(n-1, inm, src, dest)

hanoi(2,'A','B','C')

The answer is printed like:

n= 2 src= A inm= B dest= C
n= 1 src= A inm= C dest= B
n= 0 src= A inm= B dest= C
A -> B
1
n= 0 src= C inm= A dest= B
A -> C
2
n= 1 src= B inm= A dest= C
n= 0 src= B inm= C dest= A
B -> C
1
n= 0 src= A inm= B dest= C

I could understand upto

   1
    n= 0 src= C inm= A dest= B

I couldnt understand how A -> C is printed after this. After the call with n= 0 src= A inm= B dest= C, I know function will be returned. There the active function is n= 1 src= A inm= C dest= B. What happens to that?

Please explain the trace

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:42:21+00:00Added an answer on May 28, 2026 at 7:42 am

    If you add two more prints:

    print "r1"   # before first return
    print "r2"   # at the end of the function, before second, implicit return
    

    Then you will see that there were two returns in a row:

    n= 2 src= A inm= B dest= C
    n= 1 src= A inm= C dest= B
    n= 0 src= A inm= B dest= C
    r1
    A -> B
    1
    n= 0 src= C inm= A dest= B
    r1
    r2
    A -> C
    2
    n= 1 src= B inm= A dest= C
    n= 0 src= B inm= C dest= A
    r1
    B -> C
    1
    n= 0 src= A inm= B dest= C
    r1
    r2
    r2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
When studying a snippet of unknown Python code, I occasionally bump into the varName.methodName()
While studying different programming languages, I recently hit upon Icon programming language . It
I studying NSCharacterset class. NSString and NSCharacterset look like similar class. Who can explain
I have just started studying Python . I am referring to the tutorials at
After studying the Perl require docs and other links like this on Stackoverflow I'm
Studying vision, I would like to play with the Microsoft Kinect. Can I use
While studying an implementation of Group signature I came across code where Group certificate
Studying C#, my books are showing me classes for readin files. I've found 2
Studying compilers course, I am left wondering why use registers at all. It is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.