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

Python HW with Jupyter Notebook Declare a variable named DATA as a dictionary object. Assign the...
Python HW with Jupyter Notebook Declare a variable named DATA as a dictionary object. Assign the set of key/value pairs shown below. Create a function named iter_dict_funky_sum() that takes one dictionary argument.    Declare a running total integer variable. Extract the key/value pairs from DATA simultaneously in a loop. Do this with just one for loop and no additional forms of looping. Assign and append the product of the value minus the key to the running total variable. Return the funky...
Create a new Python 3 Jupyter Notebook. Create one python code cell for the problem below....
Create a new Python 3 Jupyter Notebook. Create one python code cell for the problem below. Use any additional variables and comments as needed to program a solution to the corresponding problem. All functions should be defined at the top of the code in alphabetical order. When done, download the ipynb file and submit it to the appropriate dropbox in the course's canvas page. Problem: Define a function with the following characteristics: The function's purpose will be calculating a factORRial...
create a new Python 3 Jupyter Notebook.. Create one python code cell for the problem below....
create a new Python 3 Jupyter Notebook.. Create one python code cell for the problem below. Use any additional variables and comments as needed to program a solution to the corresponding problem. All functions should be defined at the top of the code in alphabetical order. Problem: Define a function with the following characteristics: The function's purpose will be checking that a number matches another, so it should be named appropriately. It must accept one parameter, which is the number...
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...
Write a python programme in a Jupyter notebook which asks the user to input the number...
Write a python programme in a Jupyter notebook which asks the user to input the number of radioactive nuclei in a sample, the number at a later time, and the elapsed time. The programme should calculate and display the decay constant and the half-life. The decay is described by the equations: ? = ? ?−?? ?0 ?1/2 = ln (2) . Test your programme a sample that contains 4.00x108 nuclei initially and 1.57x107 nuclei after 150 seconds. SO THEREFORE A...
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:...
Write a program in Python jupyter notebook for following: Part1: Course grade calculation: Course grades for...
Write a program in Python jupyter notebook for following: Part1: Course grade calculation: Course grades for CIS 1100 are calculated based on two assignments, a midterm exam, and a final exam. Here are the weights of these. Assignments 25% Midterm exam 35% Final exam 40% Ask the user for the scores they received for the two assignments, midterm exam, and the final exam. Then calculate and display their total weighted score they received for the course. Based on the weighted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT