Starting from:

$25

STATS507-Homework 1 Solved

Homework 1: Data Types, Functions and Conditionals

 
1           Warm up: Defining Simple Functions (2 points)
In this problem, you will get practice defining simple functions in Python.

Define a function called say_hi, which takes no arguments and prints the string Hello, world! when called.
Define a function called goat_pad, which takes a string as its only argument, and prints that string, prepended and appended with the string goat. So, goat_pad(’bird’) should produce the output goatbirdgoat
goat_pad(’_’) should produce the output

goat_goat

and so on. You may assume that the input is a string, so there is no need to perform any error checking in your function.

Define a function called print_n, which takes two arguments, a string s and an integer n (in that order), and prints the string n times, each on a separate line. You may assume that s is a string and that the integer n is non-negative.
2           Euclid’s algorithm (2 points)
Euclid’s algorithm (https://en.wikipedia.org/wiki/Euclidean_algorithm) is a method for finding the greatest common divisor (GCD) of two numbers. Recall that the GCD of two numbers m and n is the largest number that divides both m and n.

The Wikipedia page above includes several pseudocode implementations of Euclid’salgorithm. Choose one of these, and use it to implement a function gcd, which takes two integers as its arguments and returns their GCD. You may assume that both inputs are integers, so there is no need to include any error checking in your function. Note: this is one of the rare occasions where you have my explicit permission to look up your answer. Unless otherwise stated (e.g., as in this problem), looking up solutions on Wikipedia or in any other non-class resource will be considered cheating!
Use your function to evaluate the GCDs of the following pairs of numbers:2018, 2019
1200, 300
5040, 60
What does your function do if one or both of its arguments are negative? Does thisbehavior make sense?
3           Approximating Euler’s number e (3 points)
The base of the natural logarithm, e, is typically defined as the infinite sum

(1)

where k! denotes the factorial of k,

k! = k · (k − 1) · (k − 2) · ··· · 3 · 2 · 1,

where we define 0! = 1 by convention. For more on Euler’s number, see https://en. wikipedia.org/wiki/E_(mathematical_constant). In this problem, we will explore different approaches to approximating this number.

An early characterization of Euler’s number, due to Jacob Bernoulli, was as the limit
.                                                     (2)

Define a function called euler_limit that takes as an argument an integer n, and returns a float that approximates e by taking x = n in Equation (2). You may assume that the input to your function will be a positive integer.

Define a function called euler_infinite_sum that takes a single non-negative integer argument n, and returns an approximation to e based on the first n terms of the sum in Equation (1). Your function should return a float. You may assume that the input will be a non-negative integer, so you do not need to include error checking in your function. As an example, euler_infinite_sum(4) should return the sum of the first four terms in Equation 1, so that euler_infinite_sum(4) returns 1+1+1/2+1/6 ≈ 2. Note: the sum in Equation 1 starts counting with k = 0 (i.e., it is “0-indexed”), while our function starts counting with n = 1 (i.e., it is “1-indexed”). euler_infinite_sum(1) should use one term from Equation (1), so that euler_infinite_sum(1) returns 1. Similarly, euler_infinite_sum(0) should return 0, since by convention an empty sum is equal to zero.
Define a function called euler_approx that takes a single argument, a float epsilon, and uses the sum in (1) to obtain an approximation of e that is within epsilon of the true value of e. Hint: use a while-loop. Note: you can use the Python math module to get the true value of e (up to floating point accuracy): exp(1).
Define a function called print_euler_sum_table that takes a single positive integer n as an argument and prints the successive values obtained from euler_infinite_sum(k) as k ranges from 1 to n, one per line.
Which of these two approximations is better?
4           Testing Properties of an Integer (3 points)
In this problem, you’ll get a bit more practice working with conditionals, and a first exposure to the kind of thinking that is required in a typical “coding interview” question. A positive integer n is a power of 2 if n = 2p for some integer p ≥ 0.

Write a function is_power_of_2 that takes a positive integer as its only argument and returns a Boolean indicating whether or not the input is a power of 2. You may assume that the input is a positive integer. You may not use the built-in sqrt function in your solution. You should need only the division and modulus (%) operations. Hint: the simplest solution to this problem makes use of recursion, though recursion is not necessary.
Generalize your previous solution to a function is_power that takes two positive integers as its arguments, b and n, in that order, and returns a Boolean. is_power(b,n) should return True if n is a power of b and False

More products