1.12 Exercises
Reinforcement
R-1.1 Write a short Python function, is_multiple(n, m), that takes two integer
values and returns True if n is a multiple of m, that is, n = mi for some
integer i, and False otherwise.
def is_multiple(n,m):
return not n%m
R-1.2 Write a short Python function, is_even(k), that takes an integer value and
returns True if k is_even, and False otherwise. However, your function
cannot use the multiplication, modulo, or division operators.
def is_even(k):
k=abs(k)
while True:
if k==0:
return True
elif k==1:
return False
k=k-2
R-1.3 Write a short Python function, minmax(data), that takes a sequence of
one or more numbers, and returns the smallest and largest numbers, in the
form of a tuple of length two. Do not use the built-in functions min or
max in implementing your solution.
def minmax(data):
min=data[0]
max=data[0]
for i in data:
if i<min:
min=i
if i>max:
max=i
return (min,max)
R-1.4 Write a short Python function that takes a positive integer n and returns
the sum of the squares of all the positive integers smaller than n.
def sumOfSquares(n):
sum=0
for i in range(1,n+1):
sum=sum+i*i
return sum
R-1.5 Give a single command that computes the sum from Exercise R-1.4, relying
on Python’s comprehension syntax and the built-in sum function.
sum([i*i for i in range(1,n+1)])
R-1.6 Write a short Python function that takes a positive integer n and returns
the sum of the squares of all the odd positive integers smaller than n.
In [20]: def sumOfSquaresOfOdd(n):
...: return sum([i*i for i in range(1,n+1) if i%2>0])
...:
...:
In [21]: sumOfSquaresOfOdd(3)
Out[21]: 10
R-1.7 Give a single command that computes the sum from Exercise R-1.6, relying
on Python’s comprehension syntax and the built-in sum function.
sum([i*i for i in range(1,n+1) if i%2>0])
R-1.8 Python allows negative integers to be used as indices into a sequence,
such as a string. If string s has length n, and expression s[k] is used for index
−n≤k<0, what is the equivalent index j ≥0 such that s[j] references
the same element?
j=n-k
R-1.9 What parameters should be sent to the range constructor, to produce a
range with values 50, 60, 70, 80?
In [26]: list(range(50,90,10))
Out[26]: [50, 60, 70, 80]
R-1.10 What parameters should be sent to the range constructor, to produce a
range with values 8, 6, 4, 2, 0, −2, −4, −6, −8?
In [27]: list(range(8,-10,-2))
Out[27]: [8, 6, 4, 2, 0, -2, -4, -6, -8]
R-1.11 Demonstrate how to use Python’s list comprehension syntax to produce
the list [1, 2, 4, 8, 16, 32, 64, 128, 256].
In [29]: [pow(2,i) for i in range(9)]
Out[29]: [1, 2, 4, 8, 16, 32, 64, 128, 256]
R-1.12 Python’s random module includes a function choice(data) that returns a
random element from a non-empty sequence. The random module includes
a more basic function randrange, with parameterization similar to
the built-in range function, that return a random choice from the given
range. Using only the randrange function, implement your own version
of the choice function.
In [31]: lst
Out[31]: [1, 2, 3]
In [32]: random.choice(lst)
Out[32]: 3
In [33]: random.randrange(10)
Out[33]: 2
In [34]: def myChoice(lst):
...: import random
...: minN=min(lst)
...: maxN=max(lst)
...: while True:
...:
...: i=random.randrange(minN,maxN+1)
...: &nb