Question

This is Python Coding question. Can anyone help me figuring these out? (Please don't use built-in...

This is Python Coding question.

Can anyone help me figuring these out?

(Please don't use built-in function, and leave comments for my understanding)

Thanks!!

1. Write a function called span(n) that takes as a parameter a 2D list of numbers called n and returns the difference between the largest and smallest number in n. For example, it should return the value 6 for the list [[2, 1, 3],[3, 7, 4]] since the largest number is 7 and the smallest is 1, and their difference is 6.

--> (topic is 2D list)

2. A mortgage loan is charged interest every month based on the current balance of the loan. If the annual rate is r%, then the interest for the month is r/12% of the current balance. After the interest is applied each month, the borrower makes a payment which reduces the amount owed. Write a function mortgage(principal, rate, years, payment) that prints out the balance at the end of each month for years number of years.

3. Write a function that takes an integer parameter n and returns the nth Fibonacci number. The Fibonacci numbers start 1,1,2,3,5,8,... and every subsequent one is the sum of the previous two.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output

Code with comments

def span(n): #span function
mini=1000000 #minimum number in list
maxi=-100000 #maxiim number in list
for i in n: #for each list i in 2d list n
for j in i: #for each element in i
mini=min(j,mini) #update minimum
maxi=max(j,maxi) #update maximum
print("Minimum:",mini)
print("Maximum:",maxi)
print("Span output: "+str(maxi-mini))

span( [[2, 1, 3],[3, 7, 4]] ) #test

def mortgage(principal,rate,year,payment): #mortgage function
print("\nMortgage balance")
month=0 #month count
print("Month: "+str(month)+", Principal: "+str(principal)) #initial
while principal>0 and month<12*year: #either loan is paid or number of years
interest=principal*(rate/12) #calculate interest
amount=payment-interest #principal paid each month
principal=principal-amount #deduct from total
month=month+1
print("Month: "+str(month)+", Principal: "+str(principal))

mortgage(1000,0.12,2,40) #test

def nthfib(n): #nth fibonacci
if n<=2: # 1st and 2nd are 1
return 1
a=1
b=1
for i in range(3,n+1): #starting from 3rd to nth
c=a+b
a=b
b=c
return c
print("\n7th fibonacci number is: "+str(nthfib(7))) #test

For indentation purpose
  

Add a comment
Know the answer?
Add Answer to:
This is Python Coding question. Can anyone help me figuring these out? (Please don't use built-in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is Python coding question, and topic is recursive. Can anyone help me figuring this out?...

    This is Python coding question, and topic is recursive. Can anyone help me figuring this out? (Only recursive function is allowed.) Thanks. Write a function called Fibonacci(n) that takes as a parameter an integer n and returns the nth Fibonacci number. The first two Fibonacci numbers are 1 and every subsequent one is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, .... Write a recursive function to count the number of uppercase letters in a...

  • This is python coding recursive function question. Could anyone help me figuring out these not using...

    This is python coding recursive function question. Could anyone help me figuring out these not using built-in function? Thanks. Write a recursive function factorial(n) that returns the value of n!=1⋅2⋅3⋅⋯⋅n. 2. Write a recursive function reverse(text) that returns the reverse of the string named text. 3. Write a recursive function countUpper(s) that returns the number of uppercase letters in the string s. 4. Write a recursive function equal(list1, list2) that returns a Boolean value indicating whether the two lists are...

  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • %%Python Question%% get_min_payment() • Parameters ◦ the total amount of the mortgage (called the principal; should...

    %%Python Question%% get_min_payment() • Parameters ◦ the total amount of the mortgage (called the principal; should be a positive number) ◦ the annual interest rate (should be a float between 0 and 1) ◦ the term of the mortgage, in years (should be a positive integer; default value: 30) ◦ the number of payments per year (should be a positive integer; default value: 12) • Functionality ◦ Compute the minimum mortgage payment. Should use the formula A= Pr(1+r) n (1+r)...

  • Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a...

    Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a function biasedCoinFlip(p) to return a biased coin flip that returns ''heads'' with probability p and tails otherwise. For example, when p=0.5, this function will behave like the last. On the other hand, p=0.25 will mean that there is only one chance in four of getting the result of "heads". 2. Write a function randomCard() to randomly return a card in a standard deck(suits: diamonds,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT