Python Forum
Want to print each iteration of factorial program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Want to print each iteration of factorial program
#10
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 value
This 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.
Reply


Messages In This Thread
RE: Want to print each iteration of factorial program - by sbabu - Jan-09-2020, 04:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 3,220 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,398 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 89,577 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial, repeating Aldiyar 4 2,988 Sep-01-2020, 05:22 PM
Last Post: DPaul
  factorial using recursive function spalisetty06 12 4,427 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  minor mistake in code for factorial spalisetty06 2 2,046 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Help with getting sub-program to print text djwilson0495 2 1,997 Aug-16-2020, 05:03 PM
Last Post: deanhystad
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,460 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Factorial sketch(python 3) KingArthur526 1 2,057 Sep-25-2019, 01:51 PM
Last Post: ichabod801
  Factorial leodavinci1990 8 4,704 Jul-19-2019, 10:59 PM
Last Post: leodavinci1990

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020