Question

#Write a function called next_fib. next_fib should take #have two parameters: a list of integers and...

#Write a function called next_fib. next_fib should take
#have two parameters: a list of integers and a single integer.
#For this description, we'll call the single integer n.
#
#next_fib should modify the list to add the next n pseudo-
#Fibonacci numbers to the end of the sequence. A pseudo-
#Fibonacci number is the sum of the previous two numbers in
#the sequence, but in our case, the previous two numbers may
#not be the original numbers from the Fibonacci sequence.
#
#For example, if the original list given was:
#
# a_list = [5, 5, 10, 15, 25, 40, 65]
#
# Then next_fib(a_list, 3) would return:
# [5, 5, 10, 15, 25, 40, 65, 105, 170, 275]
#
#All the original numbers in the list are still there, and
#three new ones have been added.
#
#You may assume the list parameter will always have at least
#two numbers.
#
#HINT: Python gets mad if you try to change a list while
#iterating over it with a for-each loop. You'll have to get
#clever with a for or while loop to do this!


#Add your code here!

def next_fib(integer_list, n):
  
  

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print:
#[5, 5, 10, 15, 25, 40, 65, 105, 170, 275]
a_list = [5, 5, 10, 15, 25, 40, 65]
print(next_fib(a_list, 3))

0 0
Add a comment Improve this question Transcribed image text
Answer #1
# Write a function called next_fib. next_fib should take
# have two parameters: a list of integers and a single integer.
# For this description, we'll call the single integer n.
#
# next_fib should modify the list to add the next n pseudo-
# Fibonacci numbers to the end of the sequence. A pseudo-
# Fibonacci number is the sum of the previous two numbers in
# the sequence, but in our case, the previous two numbers may
# not be the original numbers from the Fibonacci sequence.
#
# For example, if the original list given was:
#
# a_list = [5, 5, 10, 15, 25, 40, 65]
#
# Then next_fib(a_list, 3) would return:
# [5, 5, 10, 15, 25, 40, 65, 105, 170, 275]
#
# All the original numbers in the list are still there, and
# three new ones have been added.
#
# You may assume the list parameter will always have at least
# two numbers.
#
# HINT: Python gets mad if you try to change a list while
# iterating over it with a for-each loop. You'll have to get
# clever with a for or while loop to do this!


# Add your code here!

def next_fib(integer_list, n):
    for i in range(n):
        integer_list.append(integer_list[-1] + integer_list[-2])
    return integer_list


# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print:
# [5, 5, 10, 15, 25, 40, 65, 105, 170, 275]
a_list = [5, 5, 10, 15, 25, 40, 65]
print(next_fib(a_list, 3))

Add a comment
Know the answer?
Add Answer to:
#Write a function called next_fib. next_fib should take #have two parameters: a list of integers and...
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
  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float...

    Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float Description: Given a list of integers and a number, you want to add the numbers in the list to a total value by iterating through the given list. However, when the index you are at is divisible by the integer passed in, you must instead divide the current total by the element at that index. You must be careful when dividing the total (you...

  • 1.Write a function called high_point that takes a list of integers and returns True if there...

    1.Write a function called high_point that takes a list of integers and returns True if there exists a value in the list that is bigger than the values immediately before and after it in the list. Your function must return False if no such value exists. The values in the beginning and the end of the list cannot be high points. (20 points) Test Cases: print(high_point([2,5,8,9,7,9])) True print(high_point([2,5,6,6,3])) False print(high_point([2,5,8,21,22])) False 2. Write a while loop to repeatedly ask for...

  • USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...

    USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...

  • Write a C++ program that contains a function called swapNums. This function takes two integer parameters,...

    Write a C++ program that contains a function called swapNums. This function takes two integer parameters, swaps them in memory, and outputs the results (there is nothing to return). In main, ask the user to enter two different numbers, output them as entered (step 1), call the function swapNums() which will output the numbers swapped (step 2), and then output the values again in main (step 3). You should have three sets of output. Sample run (10 and 5 were...

  • Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount...

    Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end...

  • 5) Define a function called remainder _is_even which receives two positive integer numbers as parameters: num...

    5) Define a function called remainder _is_even which receives two positive integer numbers as parameters: num and div. This function should return a boolean value. The value to be returned should be True if the remainder of dividing num by div is even and it should return False otherwise. As an example, the following code fragment: print (remainder_is_even(23,2)) should produce the output: False 6) Define a function first_last_repeated which receives as input parameter a string (orig) with at least one...

  • Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...

    Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,

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