I'm learning how to write loops in Python 3+ but need help in solving this function with another function:
def babylonian_square_root(N, estimate,
precision):
new_estimate = (estimate + (N / estimate)) / 2
# I need to complete this using this function close_enough().
def close_enough(x, y,
maximum_allowable_difference):
''' Returns True if x and y are within maximum_allowable_difference
of each other. '''
# Note: "maximum" implies that we should use <= , but for this
to work mathematically we need to use < .
# So the difference might be positive or negative, right?
# There are three ways to do this:
# 1. Use the built-in function abs() to get the absolute value
of (x - y):
return abs(x - y) <
maximum_allowable_difference
# 2.Check BOTH (x - y) AND (y - x):
return (x - y) < maximum_allowable_difference and (y -
x) < maximum_allowable_difference
# 3. Shorten the inequality into chained comparisons:
return -maximum_allowable_difference < (x - y) <
maximum_allowable_difference
# All three of the above return statements are
equivalent.
# Please note that Python is the ONLY language I have
encountered
# that allows comparisons to be combined as shown in answer
#3.
#
#
# ATTN: --- only one return statement is
necessary, but I'm just showing you different ways of
doing this.
#
#
# Fill in the missing code below using the function close_enough().
def babylonian_square_root(N, estimate,
precision):
new_estimate = (estimate + (N / estimate)) / 2
# Keep looping until new_estimate and estimate are CLOSE ENOUGH
(hint, hint).
# Return the last calculated estimate (i.e. new_estimate).
ENTER MISSING CODE HERE
# To use this function we need three things:
# - N, the number of which we want to find the square root
# - estimate, our first estimate
# - precision, how precise we want our answer to be
# For example, to find the square root of 5 to within 0.01 with an
initial estimate of 1 we would write:
print("Square root of 5 is", babylonian_square_root(5, 1,
0.01))
#
# Hint: If you are trying to use math.sqrt() you are going
down the wrong path. Step back and rethink your
plan.
#
# Examples of expected results:
# babylonian_square_root(100, 1, 0.00000001) ----> 10.0
# babylonian_square_root(100, 1, 0.0000001) ----> 10.0
# babylonian_square_root(100, 1, 0.000001) ----> 10.0
# babylonian_square_root(100, 1, 0.00001) ----> 10.0
# babylonian_square_root(100, 1, 0.0001) ---->
10.000000000139897
# babylonian_square_root(100, 1, 0.001) ---->
10.000000000139897
# babylonian_square_root(100, 1, 0.01) ---->
10.000000000139897
# babylonian_square_root(100, 1, 0.1) ---->
10.000052895642693
# babylonian_square_root(100, 1, 1) ---->
10.032578510960604
# babylonian_square_root(100, 1, 10) ---->
10.840434673026925
#
# babylonian_square_root(100, 10, 1) ----> 10.0
#
# babylonian_square_root(5, 1, 0.001) ---->
2.236067977499978
# babylonian_square_root(5, 1, 0.00000001) ---->
2.23606797749979
#
# ---------------------------------------------------------
#
---------------------------------------------------------
Babylonian method for square root
All text in bold is the python code make sure indentation in correct
def close_enough(x, y,
maximum_allowable_difference):
return abs(x - y) >
maximum_allowable_difference
def babylonian_square_root(N, estimate, precision):
x = N
y = estimate
e = precision
while close_enough(x,y,e):
y = N / x
x = (x + y)/2
return x
root = float(input("Enter number to find the root for
"))
estimate = float(input("Enter Estimated root "))
precision = float(input("Enter precision "))
print("The root of "+str(root)+" is "+str(babylonian_square_root(root,estimate,precision)))
OUTPUT
I'm learning how to write loops in Python 3+ but need help in solving this function...
Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...
I'm trying to write this code that calculates pi using the adamchik series to a 10^-3 precision. Something is wrong with it though and I cant seem to figure out how to get it to work. Any help? def adamchik(): M=3 pi=3.1415926535897932384626433832795028841971 m=0 myAdamchik=1 while (abs(myAdamchik-pi)>(10**(-M))): myAdamchik+=((4)/((8*n+1)))-((2)/((8*n+4)))-((1)/((8*n+5)))-((1)/((8*n+6)))*(pow((1)/((16))) m+=1 return(m)
need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...
def max_of_two( x, y): ifx>y: return x returny a. Write a Python function called max_of_three( x, y, z ) to return the largest numbers of three numbers passing to the function You can use the function max_of_two in your solution. /Your code
LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius cipher scheme above. The start of the function call the get_polybius_square function to get the square as a string. The second scenario when the number of characters is not even, excluding ‘\n’. For instance: “71\n5” is an invalid cipher because there is no way that the last number correspond to a character (we need two numbers). A customized version of Polybius square will be...
I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result = (3 * x *1) return result three_x_y_at_one(3) # 9 zero_to_four = list(range(0, 5)) def y_values_for_at_one(x_values): return list(map(lambda x : three_x_y_at_one(x), x_values)) -------> this is the function that I need to rewrite without using a lambda function . y_values_for_at_one(zero_to_four) # [0, 3, 6, 9, 12] Thanks
Need help writing these functions in Python: FUNCTION 1 get_course_ids: Consumes a list of Course dictionaries and returns a list of integers representing course IDs. Here's what I have so far but I'm getting a Type Error - list indices must be integers or slices, not dict. def get_course_ids(courses): course_ids = [] for course in courses: course_ids.extend(courses[course]) return course_ids FUNCTION 2 choose_course: Consumes a list of integers representing course IDs and prompts the user to enter a valid ID,...
Python 3 # fill in details ... def modify_tuple(t, i, x): modify_tuple((1,2,3), 1, 'foo') # should return (1, 'yo!', 3) 8. write a function modify_tuple that changes element n of a tuple t to a new value x and returns the new tuple. Hint: tuples aren't mutable. You need to construct a new one! # fill in details def modify_tuple (t , i, x): In modify_tuple( (1,2,3), 1, 'foo') # should return (1, 'yo!', 3) In
Write a Python Code for a Function: you need to come up with code for shift_char. Write the code in a way in which you can shift the character by writing the number of shifts. Use the ASCII code for this. For example in lie 11, the input for char_to shift is r. U shift it by 1 so it becomes s. below is the function def shift_char(char_to_shift, amount_to_shift): ''' shifts any character by moving it a certain amount on...
Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST be written as specified. In your main code test all of the specified functions. Each function must have a comment block explaining what it does, what the parameters are and what the return value is. Please remember the following two guidelines: unless the purpose of the function is to generate output DO NOT write to the screen within the function unless the purpose...