Posts: 27
Threads: 8
Joined: Jan 2020
def _factorial_sample_(n):
if n == 0 or n == 1:
return 1
else:
result = n * _factorial_sample_(n-1)
print("n is ", n, "*", result )
return result
print(_factorial_sample_(10))
Posts: 50
Threads: 2
Joined: Nov 2019
Jan-06-2020, 06:41 AM
(This post was last modified: Jan-06-2020, 06:41 AM by sandeep_ganga.)
I would use something like,
def _factorial_sample_(n):
if n == 0 or n == 1:
return 1
else:
#result = n * _factorial_sample_(n-1)
print("\nGiven number to find factorial is ", n ,"\n")
i=n
fact=1
while(i>0):
print( " ",fact," * ",i)
fact=fact*i
print("temp_computed_result= ",fact)
i -= 1
print("\nfactorial of ",n," is : ", fact)
return fact
print(_factorial_sample_(5))Output: python 6jan.py
Given number to find factorial is 5
1 * 5
temp_computed_result= 5
5 * 4
temp_computed_result= 20
20 * 3
temp_computed_result= 60
60 * 2
temp_computed_result= 120
120 * 1
temp_computed_result= 120
factorial of 5 is : 120
120
Best Regards,
Sandeep
GANGA SANDEEP KUMAR
Posts: 27
Threads: 8
Joined: Jan 2020
Thanks Mr.Sandeep,
Here I am trying to do it through recurssion. I need to save the previous result and print it. So I can understand how the program is working.
Posts: 1,955
Threads: 8
Joined: Jun 2018
(Jan-07-2020, 05:52 AM)sbabu Wrote: I need to save the previous result and print it. So I can understand how the program is working.
There are other possibilities to understand recursion - for example visualisation. There is Thonny which is suited for beginners and has built-in stack visualisation for understanding recursion (first screenshot on their webpage); line-by-line debugger etc. Maybe it's worth a try for better understanding what is going on.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 27
Threads: 8
Joined: Jan 2020
Jan-08-2020, 03:11 AM
(This post was last modified: Jan-08-2020, 03:12 AM by sbabu.)
Hi Perfringo,
I used Thonny. I understand what it is doing. Still I want to learn how to save the old result and print it.
[python]def _factorial_sample_(n):
previous=0
if n == 0 or n ==1:
return 1
else:
result = n * _factorial_sample_(n-1)
previous = result
print (previous)
print("n is ",n, "result",previous)
return result
print(_factorial_sample_(5))[/python]
Output: 2
n is 2 result 2
6
n is 3 result 6
24
n is 4 result 24
120
I want to print
n is 4 and old result is 6 the current result is 24
n is 5 and old result is 24 the current result is 120
how do I bring this 6 value into the print statement.
Thanks
Posts: 1,955
Threads: 8
Joined: Jun 2018
Jan-08-2020, 06:48 AM
(This post was last modified: Jan-08-2020, 06:48 AM by perfringo.)
I observe following problematic areas:
- base case is not printed out (one can't understand if whole 'process' is not printed out)
- this function is not defensive against negative values (RecursionError)
For better understanding one can write (requires 3.6 <= Python due to f-strings):
def _factorial_sample_(n):
if n < 0:
raise ValueError('No factorials for negative numbers')
if n == 0 or n == 1:
print(f'n is {n}, carrying over {1}, this is base case')
return 1
else:
result = n * _factorial_sample_(n-1)
print(f'n is {n}, carrying over {result}')
return resultWith different arguments one will get:
>>> _factorial_sample_(5)
n is 1, carrying over 1, this is base case
n is 2, carrying over 2
n is 3, carrying over 6
n is 4, carrying over 24
n is 5, carrying over 120
120
>>> _factorial_sample_(0)
n is 0, carrying over 1, this is base case
1
>>> _factorial_sample_(1)
n is 1, carrying over 1, this is base case
1
>>> _factorial_sample_(-5)
/.../
ValueError: No factorials for negative numbers To your original problem: you probably should use memoization
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 27
Threads: 8
Joined: Jan 2020
n is 1, carrying over 1, this is base case
n is 2, carrying over 2 How do we bring the top 1 here such that we can print 2 X 1 = 2
n is 3, carrying over 6 How do we bring the top 2 here such that we can print 3 X 2 = 6
n is 4, carrying over 24 same thing
n is 5, carrying over 120
I was told to save it in an intermediary variable. How to do that?
Thanks.
Posts: 1,955
Threads: 8
Joined: Jun 2018
Jan-08-2020, 12:25 PM
(This post was last modified: Jan-08-2020, 12:25 PM by perfringo.)
As I told - one way is to use memoization. It seems to me that if you are 'told' then it's homework therefore I provide a solution which has caveats and may not pass automated test system:
def factorial(n, d={0:1, 1:1}):
if n < 0:
raise ValueError('No factorials for negative numbers!')
if n in d:
print(f'n is {n}, carry over {d[n]}, this is base case')
return d[n]
else:
value = factorial(n-1, d) * n
d[n] = value
print(f'n is {n}, carry over {n} * {d[n-1]} = {d[n]}')
return valueThis gives:
>>> factorial(5)
n is 1, carry over 1, this is base case
n is 2, carry over 2 * 1 = 2
n is 3, carry over 3 * 2 = 6
n is 4, carry over 4 * 6 = 24
n is 5, carry over 5 * 24 = 120
120
>>> factorial(0)
n is 0, carry over 1, this is base case
1
>>> factorial(-5)
/.../
ValueError: No factorials for negative numbers!
# But! Second time in same session:
>>> factorial(5)
n is 5, carry over 120, this is base case
120
>>> factorial(6)
n is 5, carry over 120, this is base case
n is 6, carry over 6 * 120 = 720
720
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 1,359
Threads: 2
Joined: May 2019
In the spirit of this being homework, here is code that almost works. Stores in intermediate variable (output) a list of prior values.
The bug for you to fix is that it does not make the final calculation.
def _factorial_sample_(n,output):
if n == 0 or n == 1:
output.append(1)
return n,output
else:
x, output = _factorial_sample_(n-1,output)
new_val = x * output[-1]
output.append(new_val)
print(output)
return n,output
print(_factorial_sample_(6,[]))Output: [1, 1]
[1, 1, 2]
[1, 1, 2, 6]
[1, 1, 2, 6, 24]
[1, 1, 2, 6, 24, 120]
(6, [1, 1, 2, 6, 24, 120])
Posts: 27
Threads: 8
Joined: Jan 2020
Jan-09-2020, 04:05 AM
(This post was last modified: Jan-09-2020, 04:35 AM by sbabu.)
Hi Mr.JefSummers,
I am a self learner. I gathered lot of material and started working on them. What I found is in all of them they just teach syntax with 1 or 2 examples. And give lot of exercises (some will give solutions using techniques which were not discussed).
How do I develop the mindset to think several steps a head, using different scenarios? Are there any exercises to train the brain?
How long did it take for you to become comfortable with basic coding? I am putting 4 hrs every day for the past 2 months. It's taking lot time.
Any advise appreciated.
Thanks again.
(Jan-08-2020, 12:25 PM)perfringo Wrote: As I told - one way is to use memoization. It seems to me that if you are 'told' then it's homework therefore I provide a solution which has caveats and may not pass automated test system:
def factorial(n, d={0:1, 1:1}):
if n < 0:
raise ValueError('No factorials for negative numbers!')
if n in d:
print(f'n is {n}, carry over {d[n]}, this is base case')
return d[n]
else:
value = factorial(n-1, d) * n
d[n] = value
print(f'n is {n}, carry over {n} * {d[n-1]} = {d[n]}')
return valueThis gives:
>>> factorial(5)
n is 1, carry over 1, this is base case
n is 2, carry over 2 * 1 = 2
n is 3, carry over 3 * 2 = 6
n is 4, carry over 4 * 6 = 24
n is 5, carry over 5 * 24 = 120
120
>>> factorial(0)
n is 0, carry over 1, this is base case
1
>>> factorial(-5)
/.../
ValueError: No factorials for negative numbers!
# But! Second time in same session:
>>> factorial(5)
n is 5, carry over 120, this is base case
120
>>> factorial(6)
n is 5, carry over 120, this is base case
n is 6, carry over 6 * 120 = 720
720
Hi perfringo,
I am a self learner. I gathered lot of material and started working on them. What I found is in all of them they just teach syntax with 1 or 2 examples. And give lot of exercises (some will give solutions using techniques which were not discussed).
How do I develop the mindset to think several steps a head, using different scenarios? Are there any exercises to train the brain?
How long did it take for you to become comfortable with basic coding? I am putting 4 hrs every day for the past 2 months. It's taking lot time.
Any advise appreciated.
Thanks again.
|