Question

In: Computer Science

Python HW Open a new Jupyter notebook Create a new function named fibonacci() that takes one...

Python HW

  1. Open a new Jupyter notebook

  2. Create a new function named fibonacci() that takes one required parameter:

    1. maxint, an integer that will serve as the upper bound of the loop
  3. 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:

    1. While you will use a while loop to make a Fibonacci sequence, the upper bound of the sequence will be your maxint parameter.
    2. Store the results into a list and append each new generated number
  4. 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

  1. Keep working on the same notebook
  2. Create a function named lexicographics() that takes one parameter:
    1. to_analyze, a required string
  3. Using a single for loop, calculate the following for your text:
    1. The maximum number of words per line in to_analyze (eg, the length of the longest line in to_analyze)
    2. The minimum number of words per line in to_analyze (eg, the length of the shortest line in to_analyze)
    3. The average number of words per line in to_analyze, stored as a decimal.
  4. Return these values as a tuple, in the order in which they are defined above

Expected Output

>>> lexicographics('''Don't stop believing,
Hold on to that feeling.''')
(5, 3, Decimal(4.0))

Solutions

Expert Solution

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.


Related Solutions

Create a new Python 3 Jupyter Notebook. At the top, be sure to name your notebook...
Create a new Python 3 Jupyter Notebook. At the top, be sure to name your notebook as "*assignment 2.08 - Your Name Here.ipynb*" (obviously, replace the "Your Name Here" part with your actual name). Create a single python cell to program the following specifications. Use what you've learned on this page to: 1. Find the index of "lmno" in the English alphabet using an appropriate instruction and store it in a variable. (hint: you'll need to define a string that...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
The purpose of this is to plot data using Matplotlib. Description complete the Jupyter notebook named...
The purpose of this is to plot data using Matplotlib. Description complete the Jupyter notebook named main.ipynb that reads in the file diamonds.csv into a Pandas DataFrame. Information about the file can be found here: ------- diamonds R Documentation Prices of over 50,000 round cut diamonds Description A dataset containing the prices and other attributes of almost 54,000 diamonds. The variables are as follows: Usage diamonds Format A data frame with 53940 rows and 10 variables: price price in US...
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Focuses on the design, development, implementation, and testing of a Python program using Jupyter Notebook only...
Focuses on the design, development, implementation, and testing of a Python program using Jupyter Notebook only to solve the problem described below. You will write a program that simulates an Automatic Teller Machine (ATM). For this program, your code can have of user-defined functions only. However, the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Note, the program can be completed without the use of user-defined functions. Requirements:...
python practice! 1. Create a function that takes a user choice and one number as parameters...
python practice! 1. Create a function that takes a user choice and one number as parameters and returns the operation result. -Square: print the number square -Sqrt: print the square root of the number -Reverse: reverse the sign of the number (pos or neg) and print it Note: Detect invalid choices and throw an error message – Number can be anything. 2. Create a function that takes a user choice and two numbers (start and end) as parameters. For example,...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
1.1 Write a python in Jupiter notebook function called trng that takes three numbers x, y,...
1.1 Write a python in Jupiter notebook function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangle otherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT