Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python 2..fundamentals of python 1.Package Newton’s method for approximating squ

ID: 3876718 • Letter: P

Question

python 2..fundamentals of python

1.Package Newton’s 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 Newton’s 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 function’s 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 Newton’s 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 chapter’s 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 Lee’s 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 Python’s 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 Python’s help on range to determine the names, positions, and what to do with your function’s 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- tion’s 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.)

Explanation / Answer

Ans1:-

import math

class A:
def __init__(self):
self.number=float(input("Enter No whose square root yoy want to calculate"))
self.result=newton(self.number)
print("square root of a number is")
print(self.result)

def newton(number):
numb=number
result=math.sqrt(number)
return result


a=A()

Ans2:

import math
class A:
def __init__(self):
self.number=int(input("Enter Perfect squre number whose root you want to calculate"))
self.guess=int(input("Enter guess square root of a number"))

self.result=newton(self.number,self.guess)
print("square root of a number is")
print(self.result)


def newton(number,guess):
while guess**2<number: #if the guess is smaller than the actual result
guess=guess+1
  
while guess**2>number: # if the guess is larger than actual result
guess=guess-1

return guess


a=A()

Ans3

#squareroot through iteration and guess

import math
class A:
def __init__(self):
self.number=int(input("Enter Perfect squre number whose root you want to calculate"))
  

self.result=keywordArg(self.number,guess=10) #guess=10 is my default parameter now user dont have to enter any guess
print("square root of a number is")
print(self.result)


def keywordArg(number,guess=10):
while guess**2<number: #if the guess is smaller than the actual result
guess=guess+1
  
while guess**2>number: # if the guess is larger than actual result
guess=guess-1

return guess


a=A()

Ans9:

>>> fo=open("C:/Python27/fir.txt","r") ---------> opening the file in read only mode
>>> list3=[]
>>> l3=fo.read().split(",")
>>> print l3

>>>sum=0

>>> count1=0

>>> for y in l3:

sum=sum+int(y)

count1=count1+1

print sum

>>>average=0

>>>average=sum/count1

>>>print("Average of the number present in the file is")

>>>print(Average)