In: Computer Science
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.
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