Question

PYTHON

The first function you will write should be called ‘countDown’. Your function should take zero (0) arguments. The function should return one (1) list containing integers from 10 to 1 and finally, the string “Liftoff!”. (i.e., [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ‘Liftoff!’]). You function must be recursive (i.e., it should contain a call to itself within the function body). You will get NO credit if you use any loops (for, while, etc).

3. Function One: countDown The first function you will write should be called count own Your function should take zero (0) arguments The function should return one (1) list containing integers from 10 to 1 and finally, the string Liftoff!. (i.e. to You function must be recursive (i.e., it should contain a call to itself within the function body). You will get NO credit if you use any loops (for, while, etc).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For a recursive function to be executed properly, there should be one criteria to stop the recursion called base case. If base case is not presion, recursion will go on infinitely

There are two ways to hold the base case without passing it into parameter.

1) passing optional parameter. : - Here parameter is passed but is optional. i.e. while calling method, we dont need to specify is

the code is as below: -

def countDown(n=10):
    if n == 0:
        return ['Liftoff!']
    else:
        return [n] + countDown(n-1)

print(countDown())

2. By accessing some global variable

Here we use some declared global variable to create a base case. The code is as below

n = 11

def countDown():

global n

n = n - 1

if n == 0:

return ['Liftoff!']

else:

return [n] + countDown()

print(countDown())

3. Using method variables

def countDown():
countDown.counter = countDown.counter - 1
if countDown.counter == 0:
return ['Liftoff!']
else:
return [countDown.counter] + countDown()
countDown.counter = 11
print(countDown())

main.py def countDown (): [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Liftoff!] countDown . counter countDown counter - 1 if countDown.

4. Using helper function

def countDown():
return countDownHelper(10)
  
def countDownHelper(num):
if num == 0:
return ['Liftoff!']
else:
return [num] + countDownHelper(num-1)

print(countDown())

main.py - def countDown (): [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Liftoff!] return countDownHelper (10) def countDownHelper(num):

Note:- Without base case we cannot define any recursion. Please comment if you have any query on this.

  

Add a comment
Know the answer?
Add Answer to:
PYTHON The first function you will write should be called ‘countDown’. Your function should take zero...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take...

    USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take two (2) arguments, an integer and a string. The function should return one (1) integer calculated as follows. If the input integer is 1, the function should return the sum of the return value of the ord() function on each character of the string (e.g., varOrd(1, ‘cat’) should return the result of ord(‘c’) + ord(‘a’) + ord(‘t’)). If the input integer is 2, the...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • Task 5 Write a function called “earring_iter” that takes two integer arguments, size and count. This...

    Task 5 Write a function called “earring_iter” that takes two integer arguments, size and count. This function should use a (for or while) loop to draw a Hawaiian earring containingcount-manycircles,wherethefirstcircledrawnhassizesize,andeachsubsequent circle has size earring_ratio times the size of the previous circle. Here is a recursive specification for drawing a Hawaiian earring of a given size: • To draw a Hawaiian earring of a given size with zero hoops, do nothing. • To draw a Hawaiian earring of a given size...

  • PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one...

    PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one function named multiply. This function should accept one parameter, which you can assume will be a list with one or more integers in it. (If you don’t know what I mean when I say “list of integers”, go do the reading!) The function should loop through all of the numbers in the list, and multiply all of the numbers together. It should return the...

  • USING MATLAB...Write a function that creates a structure variable called createSpacecraft. The function should take four...

    USING MATLAB...Write a function that creates a structure variable called createSpacecraft. The function should take four inputs, a number called mass (in kg), a number called fuel (in joules of energy), a number called orbit (in meters), and a number called engine (in newtons). It should return an output structure called spacecraft containing each of those values. Please ensure that both the function name, AND the names of variables within the spacecraft structure are exactly as instructed

  • Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions....

    Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions. Each function will take an array of integers as a parameter and sort those integers in increasing order. One will use insertion sort, and the other will use selection sort (described below). Simple functions will suffice here; do not create any classes. Your insertion sort function should be called insertion_sort(arr). Your selection sort function should be called selection_sort(arr). Selection sort must use a while...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • C++ Linux Compiler Letter Frequency Write a function that will take a string and return a...

    C++ Linux Compiler Letter Frequency Write a function that will take a string and return a count of each letter in the string. For example, "my dog ate my homework" contains 3 m's, 3 o's, 2 e's, 2 y's and one each of d, g, a, t, h, w, r and k. Your function should take a single string argument and return a dynamically allocated array of 26 integers representing the count of each of the letters a .. z...

  • Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function...

    Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...

  • Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the...

    Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT