In: Computer Science
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
add some comments plz
Here is the required solution after adding comments
#this function find factorial of a number where first 4 terms
are defined
def annoying_factorial(n):
#return factorial of 0 and 1
if n == 0 or n == 1:
return 1
#return factorial of 2
if n == 2:
return 2
#return factorial of 3
if n == 3:
return 6
#return factorial of 4
if n == 4:
return 4 * annoying_factorial(3)
#return factorial of 5
if n == 5:
return 5 * annoying_factorial(4)
#return factorial of 6
if n == 6:
return 6 * annoying_factorial(5)
#return factorial of any number n>6
else:
return n * annoying_factorial(n-1)
#function to find fibbonacci series
def annoying_fibonacci(n):
#return fibbonacci number for n equal to 0
if n==0:
return 0
#return fibbonacci number for n equal to 1
if n==1:
return 1
#return fibbonacci number for n equal to 2
if n==2:
return 1
#return fibbonacci number for n equal to 3
if n==3:
return 2
#return fibbonacci number for n equal to 4
if n==4:
return annoying_fibonacci(4-1)+annoying_fibonacci(4-2)
#return fibbonacci number for n equal to 5
if n==5:
return annoying_fibonacci(5-1)+annoying_fibonacci(5-2)
#return fibbonacci number for n equal to 6
if n==6:
return annoying_fibonacci(6-1)+annoying_fibonacci(6-2)
#return fibbonacci number for n>6
else:
return(annoying_fibonacci(n-1)+annoying_fibonacci(n-2))
#function to find if a linked list is sorted or not
def is_sorted_recursive(head):
#base case
if head == None or head.next == None:
return True
else:
t = head
#checck if nodes are in sorted order
if t.val > t.next.val:
return False
#recursive call
return is_sorted_recursive(head.next)
#function to checck is accordion_recursive or not
def accordion_recursive(head):
#base case
if head == None:
return None
new_head = head.next
if new_head != None and new_head.next != None:
#recursive call to function accordion_recursive
new_head.next = accordion_recursive(new_head.next)
return new_head