In: Computer Science
Python HW
Open a new Jupyter notebook
Create a new function named fibonacci() that takes one required parameter:
Following the example on the Python tutorial:
https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming
Our implementation will have a few changes:
Return the newly generated sequence as a list.
Note
In our example we are choosing to include the initial 0 value.
Expected Output
>>> fibonacci(10) [0, 1, 1, 2, 3, 5, 8]
Task 02
In this task, you'll be asked to create a simple for-loop to loop over a simple data construct, in this case, to provide the maximum, minimum, and average length of words in a speech performing a lexicographical analysis not unlike what's used to measure reading level.
Specifications
Expected Output
>>> lexicographics('''Don't stop believing, Hold on to that feeling.''') (5, 3, Decimal(4.0))
Ans 1:
Febonacci . There are different methods to do this problem . The solution i'm giving here must be easier to understand .
###########################
def fibonacci(maxint):
a,b=0,1
c,i=1,0
lis=[]
while(c<=maxint):
i=i+1
if(i==0):
lis.append(a)
elif(i==1):
lis.append(b)
else:
lis.append(c)
temp=a
a=b
b=c
c=a+b
return lis
if __name__=='__main__':
maxint=int(input())
print(fibonacci(maxint))
########################################
Ans 2:
#lexicographics
def lexicographics(to_analyze):
lengths=[]
sums=0
split_string=to_analyze.split('.')
for i in split_string:
line=i.replace(',',' ') #to remove commas
line=line.split(" ")
line=" ".join(line).split() #to remove empty strtings
n=len(line)
if(n==0): #to remove any furthur empty strings
break
lengths.append(n)
sums=sums+n
print("Max:"max(lengths))
print("Min:"min(lengths))
print("Average:",sums//len(lengths)) #put / instead of // if you
wish to have a non-int value
if __name__=='__main__':
string=input()
lexicographics(string)
#############################################
If you find my answers helpful, please consider giving a positive feedback.