Problem 1
My ideal day is contained within the myday list. I try to get a certain amount of things accomplished a day, but what actually happens is never the intended outcome…
myday = ['Breakfast','School','Lunch','School','Exercise','Dinner','HW','Sleep']
def day(n):
if n == 0:
return ''
elif n > 0:
return myday[0]+' '+day(n-1)
num = int(input("How many things do you want to do today?"))
print(f"You want to do {num} things today.")
print("Your efficiency: "+day(num))
You want to do 4 things today.
Your efficiency: Breakfast Breakfast Breakfast Breakfast
Problem 2
The two terminating statements are necessary in order to provide the two base terms for which the rest of the Fibonacci sequence is based off. For example, if the second Fibonacci number was desired and the terminating statements were not present, then the code would try to compute the sum of the first element and the zeroth element, which would not be possible since we have not defined a zeroth element. (The sequence cannot just depend on one base term)
Comments have been added to the Fibonacci code.
def fibonacci(x):
if x == 1:
return(0) # First terminating statement
# returns the first fibonacci number
elif x == 2:
return(1) # Second terminating statement
# returns the second fibonacci number
else:
return(fibonacci(x-1)+ fibonacci(x-2)) # calls the function again to produce the previous two and returns their sum
for i in range(8): # print the first eight Fibonacci numbers
print(fibonacci(i+1))
0
1
1
2
3
5
8
13
Factorial recursion
# factorial recursion
def factorial(n):
if n == 1:
return n
elif n >= 2:
return factorial(n-1)*n
print(factorial(1003))
Fibonacci Series but actually efficient
# fibonacci series. MUCH more efficient than recursion (handles cases up to orders of 10^5)
fiblist = [0,1]
def fib(n):
if n <= 1:
return fiblist[n]
else:
for i in range(n-2):
fiblist.append(fiblist[-2]+fiblist[-1])
return fiblist[-1]
num = int(input())
print(fib(num))