In: Computer Science
These code are correct, and I want some explanation or comment for them(purpose for each function)
def annoying_factorial(n):
if n == 0 or n == 1:
return 1
if n == 2:
return 2
if n == 3:
return 6
if n == 4:
return 4 * annoying_factorial(3)
if n == 5:
return 5 * annoying_factorial(4)
if n == 6:
return 6 * annoying_factorial(5)
else:
return n * annoying_factorial(n-1)
def annoying_fibonacci(n):
if n==0:
return 0
if n==1:
return 1
if n==2:
return 1
if n==3:
return 2
if n==4:
return annoying_fibonacci(4-1)+annoying_fibonacci(4-2)
if n==5:
return annoying_fibonacci(5-1)+annoying_fibonacci(5-2)
if n==6:
return annoying_fibonacci(6-1)+annoying_fibonacci(6-2)
else:
return(annoying_fibonacci(n-1)+annoying_fibonacci(n-2))
def is_sorted_recursive(head):
if head == None or head.next == None:
return True
else:
t = head
if t.val > t.next.val:
return False
return is_sorted_recursive(head.next)
def accordion_recursive(head):
if head == None:
return None
new_head = head.next
if new_head != None and new_head.next != None:
new_head.next = accordion_recursive(new_head.next)
return new_head
Factorial:
For a positive number,Factorial of a number is the multiplication of all integers smaller than or equal to n.
For example:factorial of 5 is 5*4*3*2*1
It can be calcukated using the formula:
n! = n * (n-1)!
n! = 1 if n = 0 or n = 1
def annoying_factorial(n): // named our annoying_factorial
if n == 0 or n == 1:
return 1 //initially if n=1 or 0 we return the value as such
if n == 2:
return 2 //when n is 2 value is 2*1
if n == 3:
return 6 //when n is 3 value is 3*2
if n == 4:
return 4 * annoying_factorial(3) //when n=4 we know its 4*3*2*1 so
we call the same function for factorial of 3
if n == 5:
return 5 * annoying_factorial(4) //for 5 its is 5*4!
if n == 6:
return 6 * annoying_factorial(5)
else:
return n * annoying_factorial(n-1) //similarly we get to the
general form n*(n-1)!
Fibonacci series
It is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
def annoying_fibonacci(n): //we callled the function
annoying_fibonacci
if n==0:
return 0 //when 0 is given 0 is returned
if n==1:
return 1 //when 1 entered it returns 1
if n==2:
return 1 //when its 2 its again 1 the third term
if n==3:
return 2 //when its 3 we need to get 0 1 1 2
if n==4:
return annoying_fibonacci(4-1)+annoying_fibonacci(4-2) //when n is
4 we need fib3 and fib2
if n==5:
return annoying_fibonacci(5-1)+annoying_fibonacci(5-2)
if n==6:
return annoying_fibonacci(6-1)+annoying_fibonacci(6-2) //similarly
fib6 is fib5+fib4
else:
return(annoying_fibonacci(n-1)+annoying_fibonacci(n-2)) //hence we
can write in general form
To find if it is sorted or not:
def is_sorted_recursive(head):
if head == None or head.next == None: //check if there is value in
the first node,if no check next value
return True //if nothong found return true
else:
t = head //otherwise let t be first element
if t.val > t.next.val: //if value of t is greater than next
value
return False //not sorted
return is_sorted_recursive(head.next) //again check next element
and repeatedly check for all
To find the last node:
def accordion_recursive(head):
if head == None: //if nothing in the first element
return None
new_head = head.next //else make next element as first and
check
if new_head != None and new_head.next != None: //if there is value
in it and in the next elemnt after that
new_head.next = accordion_recursive(new_head.next) //again check
the new node by calling repeatedly
return new_head //return the node