Question

In: Computer Science

From your math courses, you may recall that some number sequences can be defined as a...

From your math courses, you may recall that some number sequences can be defined as a cumulative sum of previous terms. With this nature, we can construct a new sequence called K sequence as shown below:

K(0) = 2;
K(1) = 1;
K(n) = (K(n-1) + K(n-2))2, n > 1.

For this lab, you are to write a python function Kseq(start,stop,step) that when passed the definition of a sequence of numbers in terms of a start, stop, and step size value, returns the corresponding K sequence as a list.

Sample Inputs and Outputs

Inputs: (start, stop, step)
Kseq(0,6,1)
Kseq(2,6,2) #starts from the 2nd to the 6th number in the sequence, lists them with a step size of 2

Outputs
[2, 1, 9, 100, 11881, 143544361] [9, 11881]

The initial python code given (lab7.py)

def Kseq(start, stop, step):
""" (int,int,int) -> list of integers

Input: This function is passed start (>= 0), stop (>start), and step (>= 1) values that define a sequence of numbers.
Output: This function returns a list of the corresponding K sequence.

>>>Kseq(0,6,1)
[2, 1, 9, 100, 11881, 141419664]
>>>Kseq(2,6,2)
[9, 11881]
"""

TO DO:

  • Download the file lab7.py and complete the functions.

  • Test and submit your code

  • IMPORTANT: Do not change the file name or function names. Do not use input() or print(). Your file should not contain any additional lines of code outside the function definitions. Additional test cases will be used in grading.

Solutions

Expert Solution

def kseq(start,stop,step):

    k=[]

    list1=[]

    for i in range(0,stop):

        k.append(0)

    for i in range(0,stop):

        if(i==0):

            k[i]=2

            k.append(k[i])

        elif(i==1):

            k[i]=1

            k.append(k[i])

        else:

            k[i]=((k[i-1]+k[i-2]))*((k[i-1]+k[i-2]))

            k.append(k[i])

    if(start>=0 and stop>start and step>=1):

        while(start<stop):

            list1.append(k[start])

            start=start+step

    return list1

>kseq(0,6,1) [2, 1, 9, 100, 11881,143544361] >kseq (2,6,2) , 11881] >>kse(1,1,1) Il


Related Solutions

You need to complete n courses in order to complete your degree. Some of these courses...
You need to complete n courses in order to complete your degree. Some of these courses have prerequisites, for example: “course 1 has to be completed before course 3”. Your goal is to find an order to take all n courses and complete your degree. Observe the following input file. The first line in the file has 2 numbers n and p where n is number of courses and p is the number of prerequisites. Input: 5 3 1 2...
Recall that the absolute value |x| for a real number x is defined as the function...
Recall that the absolute value |x| for a real number x is defined as the function |x| = x if x ≥ 0 or −x if x < 0. Prove that, for any real numbers x and y, we have (a) | − x| = |x|. (b) |xy| = |x||y|. (c) |x-1| = 1/|x|. Here we assume that x is not equal to 0. (d) −|x| ≤ x ≤ |x|.
The buffer capacity of a solution may be defined as the number of moles of H+...
The buffer capacity of a solution may be defined as the number of moles of H+ that will change the pH of 1.00 L of the buffer by 1.00 pH units. What is the buffer capacity of a solution which is 0.10 M in acetic acid (Ka = 1.8 × 10-5) and 0.30 M in sodium acetate, in units of mol (H+) per liter? Please show full work. (Answer key says 0.21 mol).
You must write, draw, do some math, document your work, and reflect on your result. According...
You must write, draw, do some math, document your work, and reflect on your result. According to sources that should know these things, a piece of an object hit by a projectile can move toward the launch site rather than away from the launch site. A projectile is launched, strikes a target, the target splits into two pieces and one piece (allegedly) under certain circumstances moves back toward the launch site. Does this make sense? Rather than argue about common...
Find the number of N digit sequences from the alphabet a, b, c, d with an...
Find the number of N digit sequences from the alphabet a, b, c, d with an even number of a's and an odd number of b's
You just started your first full time job out of college. You recall from your finance...
You just started your first full time job out of college. You recall from your finance course the importance of starting to save early for retirement. You plan on making deposits of $215 per pay check into a stock account and $130 per pay check into a bond account. You are paid every two weeks (26 pay checks per year). It is your plan to make these deposits for the next thirty-years. You expect that you will earn 8.75% per...
You just started your first full time job out of college. You recall from your finance...
You just started your first full time job out of college. You recall from your finance course the importance of starting to save early for retirement. You plan on making deposits of $215 per pay check into a stock account and $130 per pay check into a bond account. You are paid every two weeks (26 pay checks per year). It is your plan to make these deposits for the next thirty-years. You expect that you will earn 8.75% per...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60...
What is the number of ordered sequences of length k where each digit is taken from...
What is the number of ordered sequences of length k where each digit is taken from a set of size n? What is the number of ordered sequences of length k where each digit is taken from a set of size n without repetition? What is the number of subsets of size k of a set of size n?
You randomly choose some unfurnished one-bedroom apartments from a large number of advertisements in your local...
You randomly choose some unfurnished one-bedroom apartments from a large number of advertisements in your local newspaper. You calculated their mean monthly rent is $570 and their standard deviation is $150. Construct the following confidence intervals for the mean monthly rent of all advertised one-bedroom apartments. Confidence interval with C=90%, n=10:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT