1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vivado [14]
3 years ago
6

Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120

Computers and Technology
1 answer:
tamaranim1 [39]3 years ago
8 0

Answer:

Here is the complete code to complete factorial_str()'s recursive case:

Just add this line to the recursive part of the code for the solution:

output_string += factorial_str(next_counter,next_value)  

The above statement calls factorial_str() method recursively by passing the values of next_counter and next_value. This statement continues to execute and calls the factorial_str() recursively until the base case is reached.

Explanation:

Here is the complete code:

def factorial_str(fact_counter, fact_value):  #method to find the factorial

   output_string = ''   #to store the output (factorial of an input number)

   if fact_counter == 0:  # base case 1 i.e. 0! = 1

       output_string += '1'  # displays 1 in the output

   elif fact_counter == 1:  #base case 2 i.e. 1! = 1

       output_string += str(fact_counter) + ' = ' + str(fact_value)  #output is 1

   else:  #recursive case

       output_string += str(fact_counter) + ' * '  #adds 8 between each value of fact_counter

       next_counter = fact_counter - 1  #decrement value of fact_counter by 1

       next_value = next_counter * fact_value  #multiplies each value of fact_value by next_counter value to compute the factorial

       output_string += factorial_str(next_counter,next_value) #recursive call to factorial_str to compute the factorial of a number

   return output_string   #returns factorial  

user_val = int(input())  #takes input number from user

print('{}! = '.format(user_val),end="")  #prints factorial in specified format

print(factorial_str(user_val,user_val))  #calls method by passing user_val to compute the factorial of user_val

I will explain the program logic with the help of an example:

Lets say user_val = 5

This is passed to the method factorial_str()

factorial_str(fact_counter, fact_value) becomes:

factorial_str(5, 5):  

factorial_str() method has two base conditions which do not hold because the fact_counter is 5 here which is neither 1 nor 0 so the program control moves to the recursive part.

output_string += str(fact_counter) + ' * '  adds an asterisk after the value of fact_counter i.e. 5 as:

5 *

next_counter = fact_counter - 1 statement decrements the value of fact_counter  by 1 and stores that value in next_counter. So

next_counter = 5 - 1

next_counter = 4

next_value = next_counter * fact_value  multiplies the value of next_counter by fact_value and stores result in next_value. So

next_value = 4 * 5

next_value = 20

output_string += factorial_str(next_counter,next_value)  this statement calls the factorial_str() to perform the above steps again until the base condition is reached. This statement becomes:

output_string = output_string + factorial_str(next_counter,next_value)

output_string = 5 * 4 = 20

output_string = 20

Now factorial_str(next_counter,next_value) becomes:

factorial_str(4,20)

output_string += str(fact_counter) + ' * ' becomes

5 * 4 * 3

next_counter = fact_counter - 1  becomes:

4 - 1 = 3

next_counter = 3

next_value = next_counter * fact_value  becomes:

3 * 20 = 60

next_value = 60

output_string = 5 * 4 * 3= 60

output_string = 60

factorial_str(next_counter,next_value) becomes:

factorial_str(3,60)

output_string += str(fact_counter) + ' * ' becomes

5 * 4 * 3 * 2

next_counter = fact_counter - 1  becomes:

3 - 1 = 2

next_counter = 2

next_value = next_counter * fact_value  becomes:

2 * 60 = 120

next_value = 120

output_string += factorial_str(next_counter,next_value) becomes:

output_string = 120 + factorial_str(next_counter,next_value)

output_string = 5 * 4 * 3 * 2 = 120

factorial_str(2,120)

output_string += str(fact_counter) + ' * ' becomes

5 * 4 * 3 * 2 * 1

next_counter = fact_counter - 1  becomes:

2 - 1 = 1

next_counter = 1

next_value = next_counter * fact_value  becomes:

1 * 120 = 120

next_value = 120

output_string += factorial_str(next_counter,next_value) becomes:

output_string = 120 + factorial_str(next_counter,next_value)

factorial_str(next_counter,next_value) becomes:

factorial_str(1, 120)

Now the base case 2 evaluates to true because next_counter is 1

elif fact_counter == 1

So the elif part executes which has the following statement:

output_string += str(fact_counter) + ' = ' + str(fact_value)  

output_string = 5 * 4 * 3 * 2 * 1 = 120

So the output of the above program with user_val = 5 is:

5! = 5 * 4 * 3 * 2 * 1 = 120

You might be interested in
SOMEONE HELP PLEASE ​
exis [7]
Do what?? please tell me
3 0
2 years ago
How are the functions different and how do i determine the answer
worty [1.4K]

Answer:

this isn't a fair question...

you'll either get 2,3 or 4

not a nice question

3 0
2 years ago
What is force? what are the types of force ? Explain all with suitable examples<br>​
ad-work [718]
Force is a push or pull which is exerted on an objects. Types of Force are

Contact forces
Non contact forces or At a distance forces

Examples of Contact forces are:
Stretching of the spring of a balance
pushing of a pram
kicking of a football
Pushing the door
Hitting the ball etc are some examples of contact forces.

some types of contact forces are given in the list below:

Applied force
Normal force
Frictional force
Tension force
Air resistance force
Spring force
6 0
3 years ago
What kind of advertising is used in Saunders's story when a man and his grandson walk down the street? O A. An individualized au
omeli [17]

Answer: An individualized audio device.

Explanation:

Advertising simply refers to an attempt which is made by an individual or company in order to influence the buying behavior of the customer by convincing them to buy a product.

The kind of advertising that is used in Saunders's story when a man and his grandson walk down the street is an individualized audio device. Therefore, the correct option is A.

8 0
3 years ago
Plz help ( which is an example of a good URL?
Simora [160]

The answer is B because it is short and simple and gets you to the point

8 0
3 years ago
Read 2 more answers
Other questions:
  • How should a cover letter begin?
    13·2 answers
  • In reference to computer communications, what does the term noise mean?
    8·2 answers
  • Computers help eliminate that repetitive of manual task. How can this benefit you in in your overall career
    7·2 answers
  • How do you make the "Colorize" feature in GIMP consistent if you cannot connect the different places? Brainliest if you also exp
    12·2 answers
  • I think my knee....
    14·1 answer
  • Write a Python program to find whether a given number (accept from the user) is positive
    11·1 answer
  • (Giving away points!)
    13·1 answer
  • You are starting a small web hosting business and
    11·1 answer
  • What actions might contribute to recommendations you see online?
    9·1 answer
  • Write a program that produces the following output: CCCCCCCCC ++ ++ CC ++ ++ CC ++++++++++++++ +++++++++++++++ CC ++++++++++++++
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!