Develop a program to simulate the countdown sequence for a historic rocket launch. Given a positive integer n representing the number of seconds left, the program should print a countdown sequence in the format "n-(n-1)-...-1". This countdown replicates the final moments before the liftoff of a significant space mission. Write a recursive function called countdown that takes n as input and prints the countdown sequence.Input format :The input consists of an integer n, representing the number of seconds left for the rocket launch.Output format :The output prints the countdown sequence in the format: "n-(n-1)-...-1".
Question
Develop a program to simulate the countdown sequence for a historic rocket launch. Given a positive integer n representing the number of seconds left, the program should print a countdown sequence in the format "n-(n-1)-...-1". This countdown replicates the final moments before the liftoff of a significant space mission. Write a recursive function called countdown that takes n as input and prints the countdown sequence.Input format :The input consists of an integer n, representing the number of seconds left for the rocket launch.Output format :The output prints the countdown sequence in the format: "n-(n-1)-...-1".
Solution
Here is a Python program that simulates the countdown sequence for a historic rocket launch:
def countdown(n):
# Base case: if n is 0, print "Liftoff!" and return
if n == 0:
print("Liftoff!")
return
# Recursive case: print n, then call countdown with n-1
else:
print(str(n) + "-", end="")
countdown(n-1)
# Test the function
countdown(10)
This program defines a recursive function countdown that takes an integer n as input. If n is 0, it prints "Liftoff!" and returns. Otherwise, it prints n followed by a hyphen, then calls itself with n-1. This continues until n is 0, at which point the function prints "Liftoff!" and returns. The end="" argument in the print function is used to prevent it from printing a newline after each number, so the countdown is printed on a single line.
Similar Questions
Space Mission CountdownNASA is conducting a countdown for a space mission. Write a program to display the countdown from 10 to 1, followed by the message "Liftoff!".Constraints:NAExample:The countdown from 10 to 1, followed by "Liftoff!".10987654321Liftoff!Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 None10 9 8 7 6 5 4 3 2 1 Liftoff!
Problem StatementCreate a program to assist a fitness tracker. Input the user's current step count (n). If n is even, print 'M' for meeting the daily goal, and set the next day's goal to half of nIf n is odd, print 'A' for almost reaching the goal and set the goal to n minus 1. Repeat this until n becomes 1. Write a recursive function called fitness_tracker that performs the above adjustments and prints the achievement status.For example: If n=5:For n = 5, it prints 'A'. Since n is odd it subtracts 1 to get n = 4.For n = 4, it prints 'M'. Since n is even it halves n to get n = 2.For n = 2, it prints 'M'. Since n is even it halves to reach n = 1, completing the process.So the output is "AMM".Input format :The input consists of an integer n, representing the user's current step count.Output format :The output prints 'M' or 'A' based on meeting or almost reaching the daily goal, adjust and display the next day's goal, until 'n' becomes 1.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:5 ≤ n ≤ 50Sample test cases :Input 1 :5Output 1 :AMMInput 2 :13Output 2 :AMMAMInput 3 :50Output 3 :MAMMMAM
Objectivesimproving the ability to use numbers, operators, and arithmetic operations in Python;using the print() function's formatting capabilities;learning to express everyday-life phenomena in terms of programming language.ScenarioYour task is to prepare a simple code able to evaluate the end time of a period of time, given as a number of minutes (it could be arbitrarily large). The start time is given as a pair of hours (0..23) and minutes (0..59). The result has to be printed to the console.For example, if an event starts at 12:17 and lasts 59 minutes, it will end at 13:16.Don't worry about any imperfections in your code - it's okay if it accepts an invalid time - the most important thing is that the code produce valid results for valid input data.Test your code carefully. Hint: using the % operator may be the key to success.
Buzzcraft, an event-organizing company, has seen steady growth in its workforce. The company has observed a unique growth pattern in the number of employees, represented by the series: 20, 60, 104, 152, 204, … Write a program that simulates Buzzcraft's employee growth that takes an integer N as input and outputs the series of the number of employees up to the Nth term using a for loop.Note: This question was asked in InfyTQ coding test.Input format :The input consists of an integer N.
Helen is developing a program for a gaming application that involves generating a sequence of mystical numbers based on the Tribonacci series. She needs to implement a recursive function tribonacci to determine the Tribonacci numbers for various stages of the quest. Write a program to achieve her task.The Tribonacci series is a sequence of numbers defined as the sum of the three preceding terms. 0, 1, 1, 2, 4, 7, 13, and so on.Input format :The input consists of a positive integer n.Output format :The output displays the n terms in the Tribonacci series, separated by a space.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 20Sample test cases :Input 1 :2Output 1 :0 1 Input 2 :9Output 2 :0 1 1 2 4 7 13 24 44 Input 3 :18Output 3 :0 1 1 2 4 7 13 24 44 81 149 27
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.