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

Python Homework Help

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)
22 views

Python Homework Help

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

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/

Python Homework Help


Problem
Here are some sample quiz questions. There are not intended to provide
comprehensive coverage of the material covered thus far in 6.00.
However, they should give you a sense of the kinds of questions that will
be on the quiz.
This 1)Are
quiz iseach
openofbook and open notes,
the following True orbut do not use a computer.
False:

1. Any program that can be written using only function


definitions and calls, the basic arithmetic operators,
assignment, and conditionals will run in constant time.

2. Newton’s method will always converge on a correct root of


a function.

3. In Python, dictionaries are immutable.

4. The value of ‘math.sqrt(2.0)*math.sqrt(2.0) == 2.0’ is


True.

5. One should always avoid iteration when a recursive


solution is possible.

6. Typically, the use of functions in a program reduces the total


number of lines of code.
2)Consider the implementations of compare1 and compare 2,
where a and b are floats.
1)Do compare1 and compare2 return the same value for all
possible
2.2) inputs? Ifand
Do compare1 not,compare2
give a pair
printofthe
inputs
sameforthing
which
forthey
all
return ainputs?
possible differentIfvalue.
not, give a pair of inputs for which they print
def compare1(a,
different things. b): if a < 0:
a = -a if b < 0:
b = -b
res = (a == b) if res:
print a, 'and', b, 'have the
same absolute value.'
else:
print a, 'and', b, 'have different absolute values.' return res

def absolute_value(n): if n < 0:


n = -n return n

def compare2(a, b):


res = absolute_value(a)
print a, 'and', b, 'have== the
absolute_value(b) if res:
same absolute value.' else:
print a, 'and', b, 'have
different absolute values.'
return res
3) Consider the following implementation of a function f, where x is a
positive integer:
def f(x):
xs = str(x)
if len(xs) == 1: return int(xs)
n = int(xs[0]) + int(xs[1]) if len(xs) == 2:
return n else:
return n + f(xs[2:])

What does f(2112) return?


3.2. Write a specification of f.

4) Provide a Python implementation of a function first_N that


takes a positive integer, n, as its only argument.
The function should print the first n perfect squares that are not even
numbers. E.g., if n were 2 it should print the perfect squares 1 and 9.

5. Write pseudo code for an exhaustive enumeration variant of guess


and check.

6.) Provide a Python implementation for the function findSide


specified below def findSide():
"""asks the user to enter the area of a rectangle and the length of
7)
oneDoes
side the following function meet its specification? If not, change
of the
the program
rectangle. so thata itfloating
Returns is consistent with the that
point number specification.
is the length of the
adjacent side."""
def f(L):
"""Returns a copy of the list L without modifying
L.""" result = []
for e in L: result.append(e) return
result
8) At McDonalds one can buy chicken nuggets in packages
containing 6, 9 or 20 pieces. Write a Python function that accepts an
integer, num, as an argument and decides whether or not it is
possible to buy num nuggets at McDonalds.
Write an appropriate specification for the function below.
Assume that n is an integer. def f(n):
s = str(n)
if len(s) <= 1: return s return s[-
1] + f(int(s[:-1]))
Solutions

Problem 1

1. False. Recursion means your program can run indefinitely.

2.False. You may end up jumping back and forth between the
same two forever, given an S-shaped function (draw a
diagram).
3.False. mydict[somekey] = somevalue.
4.False. Precision is finite.
5.False. Recursion may be a more natural way to express
certain problems (e.g.: fib, Towers of Hanoi).
6.True. Code reuse.
7.True. A quick lookup.
Problem 2

1.Yes, they return the same value for all possible inputs (at least
of the types that we’ve learned about so far in class).
2.No, they print different things for negative inputs. This is
because a and b are updated to refer to a different number in
compare1, whereas they are not updated in compare2.
Note about this function: it is a bit strange in that it handles
multiple argu­ ment types.
1. f(2112) returns 2+1+f(’12’) ==> 2+1+1+2 ==>
2.
6. Given an integer or a string representation of an integer, f
returns the sum of its digits.
Problem 4

def first_N(n): count = 0


current_sqrt = 1 while count < n:
square = current_sqrt * current_sqrt
# I f square i s not even i f square % 2 !=
0:
print square
count += 1
current_sqrt += 1
Problem 5
def guess_and_check(criteria): for a in
range(...):
for b in range(...):
for c in range(...):
...
i f satisfies_criteria(a, b, c, . . . , criteria):
Problem 6
return a, b, c , . . .
1 def
area = float( raw_input(’Enter the area of the rectangle: ’) ) side1
findSide():
= float( raw_input(’Enter the length of one side of the rectangle:
’) ) return area / side1
Problem 7

Yes, it meets its specification, because the list being


modified is a brand-new list (result) that is created
inside the function, then returned. L is only traversed.
Problem 8

Note the resemblance to the exhaustive


enumeration for guess-and-check, in Problem 5.
We’re assuming that by “decides,” we just need to
return True/False.

def nuggets(num):
for a in range(num/6+1):
for b in range(num/9+1):
for c in
range(num/20+1):
i f 6*a + 9*b + 20*c ==
num: return True
return False

Problem 9

Given an integer, take the string representation of


that integer and reverse its digits (returning this as
a string).

You might also like