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 bool(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):
while k>0:
k-=2
return bool(k+1)
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):
data.sort()
return data[0],data[-1]
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 fun(n):
res=0
while n>0:
n-=1
res+=n**2
return res
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**2 for i in range(n))
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.
def fun6(n):
res=0
k=1
while k<n:
res+=k**2
k+=2
return res
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**2 for i in range(1,n,2))
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=k+len(s)
s='Shimu Yang'
for k in range(-1,-len(s)-1,-1):
j=k+len(s)
print((s[k],s[j]))
R-1.9 What parameters should be sent to the range constructor, to produce a range with values 50, 60, 70, 80?
range(50,90,10)
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?
range(8,-10,-2)
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].
l=[2**i for i in range(0,9)]
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.
import random
def choice(data):
return data[random.randrange(len(data))]
C-1.13 Write a pseudo-code description of a function that reverses a list of n integers, so that the numbers are listed in the opposite order than they were before, and compare this method to an equivalent Python function for doing the sam