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

You Might Also Like

AI search engine startup Perplexity eyes a $3B valuation

Tuesday, April 23, 2024

Plus: It's Tesla earnings day and AWS wants to host your AI models View this email online in your browser By Cody Corrall Tuesday, April 23, 2024 Welcome back to TechCrunch PM. Today we have big

🎞️ We Tried 3D Printing a Photo — You'll Love This Secret Samsung Galaxy Bluetooth Feature

Tuesday, April 23, 2024

Also: Transferring Your Phone Number to a New Carrier, and More! How-To Geek Logo April 23, 2024 📩 Get expert reviews, the hottest deals, how-to's, breaking news, and more delivered directly to

You're invited – product sense, prioritization, careers

Tuesday, April 23, 2024

Product Sense Product Sense Wednesday, May 1st @ 01:00 PM EST Learn how to identify opportunities, assess risks, and make informed decisions that lead to successful product innovations by better

CTRL-C, Exceptions, Ruff Speed-up, and More

Tuesday, April 23, 2024

Asyncio Handle Control-C (SIGINT) #626 – APRIL 23, 2024 VIEW IN BROWSER The PyCoder's Weekly Logo Asyncio Handle Control-C (SIGINT) When the user presses CTRL-C on the keyboard, the OS raises an

Writing Contests Just Landed On Product Hunt 🔥

Tuesday, April 23, 2024

Upvote us to keep the $$$ coming! 👍 ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌ ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌ ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌ ͏ ‌  ͏ ‌  ͏ ‌  ͏ ‌ 

Daily Coding Problem: Problem #1421 [Hard]

Tuesday, April 23, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Uber. Given an array of integers, return a new array such that each element at index i

Ranked | The Top 10 EV Battery Manufacturers 🔋

Tuesday, April 23, 2024

Asia dominates this ranking of the world's largest EV battery manufacturers in 2023. See which battery makers feature in the top 10. View Online | Subscribe Presented by: EnergyX's

Bringing PGO to the build pipeline

Tuesday, April 23, 2024

Plus how Go grew at Google, cmp.Or, and ways to visualize makefiles, Go binaries, and live Go processes. | #​504 — April 23, 2024 Unsub | Web Version Together with Three Dots Labs Go Weekly How Dolt

Noonification: Leetcode: Two-sum an Intuitive Approach

Tuesday, April 23, 2024

Top Tech Content sent at Noon! Get Algolia: AI Search that understands How are you, @newsletterest1? 🪐 What's happening in tech this week: The Noonification by HackerNoon has got you covered with

The best AI chatbot for coding

Tuesday, April 23, 2024

9 video gadget must-haves; 6 things Linux should borrow from MacOS -- ZDNET ZDNET Tech Today - US April 23, 2024 placeholder Can Meta AI code? I tested it against Llama, Gemini and ChatGPT - it wasn