Question

In: Computer Science

CHALLENGE ACTIVITY 9.4.1: Recursive function: Writing the base case. Add an if branch to complete double_pennies()'s...

CHALLENGE ACTIVITY 9.4.1: Recursive function: Writing the base case. Add an if branch to complete double_pennies()'s base case. Sample output with inputs: 1 10 Number of pennies after 10 days: 1024.

the program language is python.

please highlight the code for me to copy. thanks  

Solutions

Expert Solution

If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.

Solution :

Code:

# function double pennies that will double the number of pennies until the number of days become 0
def double_pennies(initial_pennies,days):
# initialzie total number of pennies as 0
total = 0
# if the days become 0 then return initial_pennies
if days == 0:
return initial_pennies
# otherwise recursively call the function with doubled initial_pennies and with a decrement of 1 in number of days
else:
total = double_pennies((initial_pennies * 2), (days - 1))
# return total number of pennies
return total
# input initial_pennies
initial_pennies=int(input())
# input days
days=int(input())
# print the message
print('Number of pennies after',days, 'days: ', end="")
# call the function
print(double_pennies(initial_pennies,days))

Output :


Related Solutions

1.what is a base condition of recursive function? 2. why is the base condition of recursive...
1.what is a base condition of recursive function? 2. why is the base condition of recursive function important?
Write a recursive function that converts Old Roman Numeral to base 10 values.
C++ program Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier. Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII,...
Add a function to Programming Challenge 7 that allows the user to search the structure array...
Add a function to Programming Challenge 7 that allows the user to search the structure array for a particular customer's account. It should accept part of the customer's name as an argument and then search for an account with a name that matches it. All accounts that match should be displayed. If no account matches, a message saying so should be displayed. Currently I have #include <iostream> #include <string> using namespace std; void displayAccount(struct customerAccount Arr[], int index); void changeAccount(struct...
JAVA PLEASE Write a recursive function that does the following: Given a number, add all the...
JAVA PLEASE Write a recursive function that does the following: Given a number, add all the digits and display the sum. Example: ​​The sum of the number 5432 would be 14. o Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from the tail to the beginning. o...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
/* Complete the TO DO comments below */ window.onload = function() { /* TODO add a...
/* Complete the TO DO comments below */ window.onload = function() { /* TODO add a border to the header of the page (a) a simple type selector, and (b) the style property of the element object. */ document.querySelector('TODO').TODO = TODO; /* TODO change the text of the h1 element by using (a) the first-child pseudo selector, and (b) the innerHTML property of the element object. */ document.querySelector('TODO').TODO = TODO; /* TODO change the background-color of the section with id...
In the case of c++, how would I store data drom a recursive function into a...
In the case of c++, how would I store data drom a recursive function into a 2D array?
2. Specification - Given the following code base, add the appropriate function that will give the...
2. Specification - Given the following code base, add the appropriate function that will give the correct results/output. CODE: # TODO : Add Two Missing Functions HERE mlist = [(" Orange ", 10 , 0.25) ,( " Apple ", 5 , .20) , (" Banana ", 2 , 0.3) ,(" Kiwi ", 1 , 0.5)] addFruit (10 ," Lemon " ,0.1) displayFruitList ( mlist ) OUTPUT: Orange --- $ 2.50 Apple --- $ 1.00 Banana --- $ 0.60 Kiwi ---...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS FILE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT