Question

In: Computer Science

What code would I add to the following program to have it print out the runtime...

What code would I add to the following program to have it print out the runtime each time the program is used?

def moveTower(n, source, dest, temp):
if n == 1:
print("Move disk from", source, "to", dest+".")
else:
moveTower(n-1, source, temp, dest)
moveTower(1, source, dest, temp)
moveTower(n-1, temp, dest, source)

def hanoi(n):
moveTower(n, "A", "C", "B")

def main():
print("Towers of Hanoi")
n = int(input("How many disks? "))
moveTower(n, "A", "C", "B")

if __name__ == '__main__': main()

Solutions

Expert Solution

Corrected Python code:

#importing time library

import time

def moveTower(n,source,dest,temp):

    if n==1:

        print("Move disk from",source,"to",dest+".")

    else:

        moveTower(n-1,source,temp,dest)

        moveTower(1,source,dest,temp)

        moveTower(n-1,temp,dest,source)

def hanoi(n):

    moveTower(n,"A","C","B")

def main():

    #obtaining start time

    s_time=time.time()

    print("Towers of Hanoi")

    n=int(input("How many disks? "))

    moveTower(n,"A","C","B")

    #printing current time-start time to obatin the runtime

    print(str(time.time()-s_time)+" seconds")

if __name__ == '__main__': main()

Screenshot:

Output:


Related Solutions

I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
What would I add to the following code to detect the pitch of the audio signal?...
What would I add to the following code to detect the pitch of the audio signal? (use cepstral method) clear all; close all; clc; info = audiodevinfo; info.input(1) info.input(2) pause(3); % delete it if necessary clc; fs = 44.1e3; noBits = 16; noChannels = 1; recordObject = audiorecorder(fs,noBits,noChannels); disp('Start speaking...'); record(recordObject); pause(3); stop(recordObject); disp('End of Recording.'); x = getaudiodata(recordObject); %takin in as aperiodic signal noSamples = length(x); %length of vector x "max(size(x)) time = [1:noSamples]/fs; %time ticks %DFT: X[k]= (sigma...
This is what I have so far. The out print I am looking for is Date...
This is what I have so far. The out print I am looking for is Date is 7/22/2020 New month: 9 New day: 5 New Year: 2020 Date is 9/5/2020 #include    class Date {    public:        Date::Date(int dateMonth, int dateDay, int dateYear)        month{dateMonth}, day{dateDay}, int dateYear}{}               //void displayDate();         //set month        void Date::setMonth(std::int dateMonth){            month = dateMonth;        }        //retrieve month   ...
How can I write a simple MIPS program to print out the following elements of an...
How can I write a simple MIPS program to print out the following elements of an array of the size of 10: 5,10,15,20,25,30,35,40,45,50
How would I add an automatic please fill out this info to my complete code. Something...
How would I add an automatic please fill out this info to my complete code. Something similar to this code. <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Untitled Document</title> <script>    function phonenumber(inputtxt){           var phoneno = (\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4};       if(inputtxt.value.match(phoneno)){        document.getElementById("yourEntry").innerHTML = document.getElementById("myphone").value;    }    //if(!inputtxt.value.match(phoneno)){        // alert("Does not Work!")        //}    } </script> </head> <body> <form name="form1" action="#"> <input type="text" name="myphone" id="myphone" value="" pattern= "(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}" placeholder="(555) 555-1212" required /> <input...
I have a working code for a dice game. What is the best way to add...
I have a working code for a dice game. What is the best way to add coding to keep track how many points each player makes? When they tie both players gets no points. When they reach to 100 the game ends or unless the player quit. How would I make it work? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Dice Game</title> <link rel="stylesheet" type="text/css" href="dice.css"> </head> <body> <div class="row" align="center"> <div class="col-4"> <h3>Your Dice</h3> <img src="diceimages/m1.png" width="100" height="100" alt="roll: 1"...
hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
C++ How can I print out the subtrees in my self-balancing tree? What I would like...
C++ How can I print out the subtrees in my self-balancing tree? What I would like to do is have it display something like " ROOT = X, LEFT PARENT = X, X,X, RIGHT PARENT = X,X,X. Here is my code: #include <iostream> using namespace std; class TreeNode { public:    int data;    TreeNode* left;    TreeNode* right; }; TreeNode* newNode(int data); /* A function that constructs Balanced Binary Search Tree from a sorted array */ TreeNode* convertToBTS(int arr[],...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT