100% found this document useful (1 vote)
29 views

Python Homework Help: Are Each of The Following True or False

Get Best Python Homework Help
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
29 views

Python Homework Help: Are Each of The Following True or False

Get Best Python Homework Help
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Homework Help

For any Assignment related queries, Call us at : -  +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- https://www.pythonhomeworkhelp.com/

Are each of the following True or False

1. In Python the values of a dict must be immutable. FALSE.


The values of keys must be immutable.

2. There exist problems that cannot be solved in Python


without using either iteration or recursion. TRUE.
Otherwise, the complexity of programs would be bound by
the length of the code.

3. Floating point arithmetic behaves exactly like normal


arithmetic on real numbers. FALSE. Floating point
provides only an approximation to real numbers.

4. On all inputs, a bisection search will run faster than a linear


search. FALSE. It has a higher asymptotic complexity, but
there can be inputs on which it will run more slowly.
Consider, for example, searching for an element that happens
to the first element of the list.

5. Let L be a list, each element of which is a list of ints. In


Python, the assignment statement L[0][0] = 3 mutates the list
2) What does the following code print?

T = (0.1, 0.1)
x = 0.0
for i in range(len(T)): for j in T:
x += i + j print x
print i

0.1
0.2
1.3
2.4
1
3) What does the following code
print?
def f(s):
if len(s) <= 1:
return s #Note double
return f(f(s[1:])) recursion
+ s[0]f('mat')
print
print f('math')

at
m
hat
m
4) Implement the body of the function specified in
the box.
def findAll(wordList, lStr):
"""assumes: wordList is a list of words in
lowercase. lStr is a str of lowercase
letters.
No letter occurs in lStr more than once
returns: a list of all the words in wordList that
contain each of the letters in lStr exactly
once and no letters not in lStr."""
def findAll(wordList,
letters): result = []
letters = sorted(letters)
for w in
wordList:
w =
sorted(w)
if w == letters:
result.append(
5) The following
w) return resultcode does not meet its specification. Correct it.

def addVectors(v1, v2):


"""assumes v1 and v2 are lists of ints.
Returns a list containing the pointwise sum of
the elements in v1 and v2. For example,
addVectors([4,5], [1,2,3]) returns [5,7,3],and
addVectors([], []) returns []. Does not modify inputs.""" if
len(v1)
> len(v2):
result = v1
other =
v2 else:
result
= v2
other
= v1
for i in
range(len(other)):
result[i] += other[i]
return result

insert the lines

v1 = v1[:]
v2 = v2[:]
6)Consider the following code:

def f(s, d):


for k in d.keys(): d[k]
=0
for c in s:
if c in d:
d[c] += 1
else: d[c] = 0
def addUp(d):
return d
result = 0 for k in d:
result += d[k] return result
d1 = {}
d2 = d1
d1 = f('abbc', d1) print addUp(d1)
d2 = f('bbcaa', d2)
print addUp(d2) print f('', {}) print
result

1) What does it print?


1
5
{}

2) Does it terminate normally? Why or


why not?
No, it terminates with a NameError
exception, because result will not be
defined.
7)Consider the following code:

def logBase2(n):
"""assumes that n is a positive int
returns a float that approximates the log base 2 of
n""" import math
return math.log(n, 2)

def f(n):
"""assumes n is an int""" if n < 1:
return
curDigit = int(logBase2(n)) ans = 'n =
'
while curDigit >= 0:
if n%(2**curDigit) < n: ans = ans +
'1'
n = n - 2**curDigit
else:
ans = ans + '0' curDigit -= 1
return ans

for i in range(3): print f(i)

1) What does it print?

None n = 1
n = 10

2)Under the assumption that logBase2 is O(n), what is the order (use
big Oh notation) of f?

O(n)
8) Next to each item in the left column write the letter labeling the item in the
right column that best matches the item in the left column. No item in the
right column should be used more than once.

Big O notation B a) induction

Newton’s b) upper bound


method D

Recursion A c) lower bound

d) approximation

e) expected running time

f) exponential

9.Do you think that the lectures are too slow paced, too fast paced, about
right?

10.Do you think that the problem sets are too short, too long, about right?

You might also like