Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python 2..fundamentals of python 1.Package Newtons method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number

python 2..fundamentals of python

1.Package Newtons method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key.

2.Convert Newtons method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of the square root should be passed as a second argument to the function.)

3.Elena complains that the recursive newton function in Project 2 includes an extra argument for the estimate. The functions users should not have to provide this value, which is always the same, when they call this function. Modify the definition of the function so that it uses a keyword parameter with the appropriate default value for this argument, and call the function without a second argument to demonstrate that it solves this problem.

4.Restructure Newtons method (Case Study 3.6) by decomposing it into three cooperating functions. The newton function can use either the recursive strategy of Project 1 or the iterative strategy of Case Study 3.6. The task of testing for the limit is assigned to a function named limitReached, whereas the task of computing a new approximation is assigned to a function named improveEstimate. Each function expects the relevant arguments and returns an appropriate value.

5.A list is sorted in ascending order if it is empty or each item except the last one is less than or equal to its successor. Define a predicate isSorted that expects a list as an argument and returns True if the list is sorted, or returns False otherwise. (Hint: For a list of length 2 or greater, loop through the list and compare pairs of items, from left to right, and return False if the first item in a pair is greater.)

6. Add a command to this chapters case study program that allows the user to view the contents of a file in the current working directory. When the command is selected, the program should display a list of filenames and a prompt for the name of the file to be viewed. Be sure to include error recovery.

7. Write a recursive function that expects a pathname as an argument. The path- name can be either the name of a file or the name of a directory. If the pathname refers to a file, its name is displayed, followed by its contents. Otherwise, if the pathname refers to a directory, the function is applied to each name in the direc- tory. Test this function in a new program.

8. Lee has discovered what he thinks is a clever recursive strategy for printing the elements in a sequence (string, tuple, or list). He reasons that he can get at the first element in a sequence using the 0 index, and he can obtain a sequence of the rest of the elements by slicing from index 1. This strategy is realized in a function that expects just the sequence as an argument. If the sequence is not empty, the first element in the sequence is printed and then a recursive call is executed. On each recursive call, the sequence argument is sliced using the range 1:. Here is Lees function definition:

def printAll(seq): if seq:

print(seq[0])

printAll(seq[1:])

Write a script that tests this function and add code to trace the argument on each call. Does this function work as expected? If so, explain how it actually works, and describe any hidden costs in running it.

9. Write a program that computes and prints the average of the numbers in a text file. You should make use of two higher-order functions to simplify the design.

10. Define and test a function myRange. This function should behave like Pythons standard range function, with the required and optional arguments, but it should return a list. Do not use the range function in your implementation! (Hints: Study Pythons help on range to determine the names, positions, and what to do with your functions parameters. Use a default value of None for the two optional parameters. If these parameters both equal None, then the function has been called with just the stop value. If just the third parameter equals None, then the function has been called with a start value as well. Thus, the first part of the func- tions code establishes what the values of the parameters are or should be. The rest of the code uses those values to build a list by counting up or down.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Implement special techniques in writing online sales messages.

Answered: 1 week ago