Data Interview Qs - Programming the Tower of Hanoi

InterviewQs

Programming the Tower of Hanoi

Python Algorithms Loops Recursion

Forward this email to a friend or colleague and challenge them to solve it.

Hi, 

The Tower of Hanoi is a puzzle game with three rods and n disks of different sizes which can slide onto any rod. The puzzle starts with the disks stacked in ascending order of size on one rod, the smallest at the top, making a conical shape.

The goal of this puzzle is to move all the disks from the first rod to the last rod while following these rules:

  • You can only move one disk at a time.
  • A move consists of taking the uppermost disk from one of the stacks and placing it on top of another stack.
  • You cannot place a larger disk on top of a smaller disk.

Write a function that prints out all the steps necessary to complete the Tower of Hanoi. In your code, the disks will be numeric values and the rods will be alphabetic with a = first rod, b = middle rod, and c = destination rod.

For example, when n = 3, your output should look like the following:

    

Moving disk 1 from a to c

Moving disk 2 from a to b

Moving disk 1 from c to b

Moving disk 3 from a to c

Moving disk 1 from b to a

Moving disk 2 from b to c

Moving disk 1 from a to c

Solution will be written in Python for premium users.




 

You’ve accumulated 343 questions since joining our email list. Create a premium account to access detailed solutions to each problem (including this one) + our new Case studies.


Was this email forwarded to you? You can join our list here to receive questions

How did you like today's question?

15411 Boischatel, Montreal, QC, H9H1Y7, Canada
Don't want to get these emails anymore? We'll forgive you if you unsubscribe

Older messages

Probability of a robbery

Wednesday, September 7, 2022

Probability Theory, Probability Reasoning, Complement Rule, InterviewQs Probability of a robbery Probability Theory Probability Reasoning Complement Rule Forward this email to a friend or colleague and

Checking user retention and churn in SQL

Monday, September 5, 2022

SQL, Database Querying, Window Function, Subquery, Self-Join, InterviewQs Checking user retention and churn in SQL SQL Database Querying Window Function Subquery Self-Join Forward this email to a

Price per calorie of various snack foods

Friday, September 2, 2022

Data Analysis, Python, Pandas, Data Manipulation, InterviewQs Price per calorie of various snack foods Data Analysis Python Pandas Data Manipulation Forward this email to a friend or colleague and

Bubble sort algorithm

Wednesday, August 31, 2022

Sorting, Algorithim, Data Structures, InterviewQs Bubble sort algorithm Sorting Algorithim Data Structures Forward this email to a friend or colleague and challenge them to solve it. Hi, Bubble sort (

Covariance and correlation

Monday, August 29, 2022

Statistics, Covariance, Correlation, InterviewQs Covariance and correlation Statistics Covariance Correlation Forward this email to a friend or colleague and challenge them to solve it. Hi, Explain the

DotLeap 98 - Acala's return

Friday, October 7, 2022

KILT migrates to Polkadot, Moonbeam introduces XCM SDK, new ecosystem reports, and more. ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

[Python Mastery] The hidden costs of "copy-paste" Python programming

Friday, October 7, 2022

Hey there, Here's a development story that plays out thousands of times each day: Avid Pythonista Max is working on a function to find the 10 oldest files in a nested directory hierarchy. He needs

Why your startup needs a prenup

Friday, October 7, 2022

TechCrunch+ Newsletter TechCrunch+ logo TechCrunch+ Roundup logo By Walter Thompson Friday, October 07, 2022 Welcome to TechCrunch+ Friday Image Credits: skodonnell / Getty Images In the early days of

iOS Dev Weekly - Issue 579

Friday, October 7, 2022

The question of what technologies to use has no definitive answer! 🎲🤷‍♂️ View on the Web Archives ISSUE 579 October 7th 2022 Comment Why do I link to Alexandre Colucci's articles reporting on

Daily Coding Problem: Problem #898 [Easy]

Friday, October 7, 2022

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Amazon. Implement a stack API using only a heap. A stack implements the following

Wireless Apple CarPlay? How to add it to your car

Friday, October 7, 2022

Managers can't agree on who to hire; QuietComfort Earbuds II review -- ZDNET ZDNET Tech Today - US October 7, 2022 placeholder How to add wireless Apple CarPlay to your car Wired connections are a

Issue #457: 3D Diffusion vs GET3D, acid traces, and Tranquility

Friday, October 7, 2022

Weekly newsletter about HTML5 Game Development. Is this email not displaying correctly? View it in your browser. Issue #457 - October 7th 2022 If you have anything you want to share with the HTML5 game

🔎 What PMs should know about Product Marketing

Friday, October 7, 2022

Product Marketing - what product managers should know Do you think all you need to know about product marketing is “it's that stuff those product marketing managers do”? While product marketing may

Ranked | The World’s Most Surveilled Cities 📹

Friday, October 7, 2022

The world's most surveilled cities contain hundreds of thousands of cameras. See where these cities are located in this infographic. View Online | Subscribe Presented by: Special Report Now

Google Is Not Apple, and It Should Stop Trying to Be

Friday, October 7, 2022

Did You Know?: Dale Carnegie, most famous for the book How to Win Friends and Influence People, changed his last name from “Carnagey” to “Carnegie” in honor of the steel magnate, Andrew Carnegie, and