Question

In: Computer Science

These code are correct, and I want some explanation or comment for them(purpose for each function)...

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

Solutions

Expert Solution

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


Related Solutions

I don't want explanation , I just want a correct answer, Very quickly. 12- If the...
I don't want explanation , I just want a correct answer, Very quickly. 12- If the slope of the consumption function is equal to one, then Select one: a. The multiplier is undefined b. The multiplier is smaller than one c. The multiplier is just one d. The multiplier is larger than one e. None of the above 11- If the nominal interest rate 8% and expected inflation 5%, the expected real interest rate in year t is approximately Select...
look this code is a correct but i want modify it to allow the client to...
look this code is a correct but i want modify it to allow the client to have three attempts to login to the server package hw2; import java.net.*; import java.util.Formatter; import java.util.Random; import java.util.Scanner; import java.io.*; public class Client {    Socket server;    int port;    Formatter toNet = null;    Scanner fromNet = null;    Scanner fromUser = new Scanner(System.in);    public Client() {        try {            // login at server at local host...
I just want some ideas on how to write this essay, for instance, explanation of the...
I just want some ideas on how to write this essay, for instance, explanation of the case and what are some possible solution. thanks “What Should I do, my friend?” A former classmate of yours, Liz Theranos, has landed an internship position in the accounting department at Brompton Travels, a small closely held company. She tells you at one of your regular get-togethers at the local coffee shop, that she is excited and anxious to make a good impression as...
The following code searches a text file for definitions and prints them out. I want to...
The following code searches a text file for definitions and prints them out. I want to create a function that can be called for the given vector and map. The function will either print the results in reverse order, print results without duplicate definitions, or do both. What is the best approach to make the code less repetitive. if (find(keyWords.begin(), keyWords.end(), v[0]) != keyWords.end()) {                        for (auto map_iter = mymap.cbegin(); map_iter != mymap.cend(); ++map_iter)...
The correct answers are highlighted. I would like an explanation for why some are null hypothesis...
The correct answers are highlighted. I would like an explanation for why some are null hypothesis and some are alternative. Thank you. 4 You are testing the hypothesis that the mean textbook expense per semester at IUPUI is at least $500. The null and alternative hypothesis statements for this test are: a H₀: μ ≤ 500 H₁: μ > 500 b H₀: μ ≥ 500 H₁: μ < 500 c H₀: μ < 500 H₁: μ ≥ 500 d H₀:...
code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
Please, I need a correct answer and clear explanation. Thank you, Part I – Explanation Q1...
Please, I need a correct answer and clear explanation. Thank you, Part I – Explanation Q1 Below are pairs of changes that affect elements of the accounting equation. Give an example of a transaction or economic event that reflects each: a. Asset increases, asset decreases b. Asset increases, liability increases c. Asset increases, shareholders’ equity increases d. Asset increases, revenue increases e. Liability decreases, asset decreases f. Asset decreases, expense increases g. Liability decreases, revenue increases h. Asset decreases, shareholders’...
Please I want answers for these questions. Thanks Note: I want them by typing not by...
Please I want answers for these questions. Thanks Note: I want them by typing not by hand writing Q2 Explain why a market structure in which money is used as a medium of exchange is more conducive to the expansion of trade and exchange than a barter system. What is money? Explain the three functions that money performs. What is the relationship between money and inflation? Explain
Code in python Hello I want a program to read only the price of each line...
Code in python Hello I want a program to read only the price of each line in a txt file in python. The price is present right after the 2nd comma within the txt file. The output that I want is also printed at the bottom. Inside the txt file: 2019-08-01 00:08:39,BTC/USD,10128.08 2019-08-01 00:08:21,BTC/USD,10117.54 2019-08-01 00:08:20,BTC/USD,10120.51 2019-08-01 00:08:19,BTC/USD,10118.13 2019-08-01 00:08:18,BTC/USD,10120.74 Python file output: Price 1: 10128.08 Price 2: 10117.54 Price 3: 10120.51 Price 4: 10118.13 Price 5: 10120.74
Please, I need correct answers and clear explanation. Thanks, Indicate whether each of the following statements...
Please, I need correct answers and clear explanation. Thanks, Indicate whether each of the following statements is true or false: a. Under the accrual basis of accounting, when cash is collected on accounts receivable, revenue is recorded. b. Cash receipts from customers are debited to Accounts Receivable. c. The cash basis of accounting recognizes expenses when they are incurred. d. Under the cash basis of accounting, there is no such thing as a Prepaid Expenses account. e. Asset accounts and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT