Question

In: Computer Science

def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2:...

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

Solutions

Expert Solution

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


Related Solutions

def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
def mystery(L, x): if L==[]: return False if L[0] == x: return True L.pop(0) return mystery(L,...
def mystery(L, x): if L==[]: return False if L[0] == x: return True L.pop(0) return mystery(L, x) What is the input or length size of the function mystery? What is the final output of mystery([1,3,5,7], 0)? Explain in one sentence what mystery does? What is the smallest input that mystery can have? Does the recursive call have smaller inputs? Why? Assuming the recursive call in mystery is correct, use this assumption to explain in a few sentences why mystery is...
from PIL import Image def stringToBits(string):     return str(bin(int.from_bytes(string.encode(), 'big')))[2:] def bitsToString(bits):     if bits[0:2] != '0b':         bits.
from PIL import Image def stringToBits(string):     return str(bin(int.from_bytes(string.encode(), 'big')))[2:] def bitsToString(bits):     if bits[0:2] != '0b':         bits = '0b' + bits     value = int(bits, 2)     return value.to_bytes((value.bit_length() + 7) // 8, 'big').decode() def writeMessageToRedChannel(file, message):     image = Image.open(file)     width, height = image.size     messageBits = stringToBits(message)     messageBitCounter = 0     y = 0     while y < height:         x = 0         while x < width:             r, g, b, a = image.getpixel((x, y))             print("writeMessageToRedChannel: Reading pixel %d, %d - Original values (%d, %d, %d, %d)"...
The functions are tested in the following main(): def recSum1(somelist): if len(somelist) == 1: return somelist[0]...
The functions are tested in the following main(): def recSum1(somelist): if len(somelist) == 1: return somelist[0] else: a = recSum1(somelist[:len(somelist)//2]) b = recSum1(somelist[len(somelist)//2:]) return a + b def recSum2(somelist): if len(somelist) == 1: return somelist[0] else: return somelist[0] + recSum2(somelist[1:]) import random def main(): N = 100 myList = [random.randint(-500, 500) for i in range(N)] print(myList) print("The sum of the numbers is: " + str(sum(myList))) print("The sum of the numbers using recSum1 is: " + str(recSum1(myList))) print("The sum of the...
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember,...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember, n is a scalar quantity (float/int). x is a NumPy array and you should return an equal length NumPy array :param x: numpy array :param max_val: The maximum value :return: a tuple with two numpy arrays x and the sum """
def vend():     a = {'key': '0', 'item': 'choc', 'price': 1.50, 'stock': (2)}     b = {'key': '1',...
def vend():     a = {'key': '0', 'item': 'choc', 'price': 1.50, 'stock': (2)}     b = {'key': '1', 'item': 'pop', 'price': 1.75, 'stock': 1}     c = {'key': '2', 'item': 'chips', 'price': 2.00, 'stock': 3}     d = {'key': '3', 'item': 'gum', 'price': 0.50, 'stock': 1}     e = {'key': '4', 'item': 'mints', 'price': 0.75, 'stock': 3}     items = [a, b, c, d, e]          def show(items):         cim = 0         for item in items:             if item.get('stock') == 0:                 items.remove(item)         for item in items:             print(item.get('key'), item.get('item'),item.get('price'),...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
Solve the following recurrence relation for the given initial conditions. y(n+2) - 0.3y(n + 1) + 0.02y(n) = 10 y(0) = 2; y(1) = 0
Solve the following recurrence relation for the given initial conditions.y(n+2) - 0.3y(n + 1) + 0.02y(n) = 10        y(0) = 2;    y(1) = 0
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus...
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design an algorithm in ​(Theta(log n)) to find the missing integer. Your algorithm should be given in pseudo code. For example, the array A could be {1, 2, 3, 4, 6, 7, 8, 9, 10} in which 5 is missing.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT