For any help regarding Python Homework Help
visit : - https://www.pythonhomeworkhelp.com/,
Email :- support@pythonhomeworkhelp.com or
call us at :- +1 678 648 4277
1)Is each of the following True or False
1.A greedy algorithm can be used to solve the 0-1 knapsack optimization
problem.
2.Dynamic programming can be used to solve optimization problems where the
size of the space of possible solutions is exponentially large.
3.Dynamic programming can be used to find an approximate solution to an
optimization problem, but cannot be used to find a solution that is guaranteed
to be optimal.
4.In Python, an instance of a class is an object, but a class itself is not.
5.In Python, a subclass can override a function definition contained in a super
class.
6.Decision trees are always binary.
pythonhomeworkhelp.com
Problem
2) Provide code implementing a Python function that meets the specification
below.
def findMedian(L): """Finds median of L.
L: a non-empty list of floats
Returns:
If L has an odd number of elements, returns the median element of L.
For example, if L is the list
[15.0, 5.3, 18.2], returns 15.0.
If L has an even number of elements, returns the average of the two
median elements. For example, if L is the list [1.0, 2.0, 3.0, 4.0], returns
2.5.
If the list is empty, raises a ValueError exception.
Side effects: none. """
pythonhomeworkhelp.com
3) What does the following code print?
class Shape(object):
def __cmp__(s1, s2):
return cmp(s1.area(), s2.area())
class Square(Shape):
def __init__(self, h): self.side
= float(h)
def area(self):
return self.side**2 def
__str__(self):
return 'Square with side ' + str(self.side)
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159*(self.radius**2)
def __str__(self):
return 'Circle with radius ' + str(self.radius)
pythonhomeworkhelp.com
def f(L):
if len(L) == 0: return None
x = L[0]
for s in L:
if s >= x:
x = s
return x
s = Square(4)
print s.area() L =
[]
shapes = {0:Circle, 1:
Square} for i in range(10):
L.append(shapes[i%2](i)
) print L[4]
print f(L)
4) The following two formulas can be used to
formalize a 0-1 knapsack problem.
1
)
2)
4.1) What do each of n, pi, xi, wi, and C represent?
pythonhomeworkhelp.com
4.2) Use the formulas (refer to them as “formula 1” and “formula 2”) to describe the
optimization problem to be solved.
5. Write pseudo code describing merge sort.
6) Consider the two functions specified below that are used to play a “guess a
number game.”
def cmpGuess(guess):
"""Assumes that guess is an integer in range(maxVal). returns -1 if guess is <
than the magic number, 0 if it is equal to the magic number and 1 if it is greater
than the magic number."""
def findNumber(maxVal):
"""Assumes that maxVal is a positive integer. Returns a number, num,
such that cmpGuess(num) == 0."""
Write a Python implementation of findNumber that guesses the magic number
defined by cmpGuess. Your program should have the lowest time complexity
possible.
pythonhomeworkhelp.com
Problem 1
1. False. 2. True. 3. False. 4. False. 5. True. 6. False.
Problem 2
def findMedian(L):
if len(L) == 0: raise ValueError("Empty list") copy = L[:]
copy.sort()
if len(copy) % 2 == 1: return copy[len(copy) / 2]
else:
return (copy[len(copy) / 2] + copy[len(copy) / 2 - 1]) / 2
Problem 3
16.0
Circle with radius 4
Circle with radius 8
Problem 4
1.
•n — total number of items
•pi — value of item i
•xi — value is 1 if item i is taken; 0 otherwise
Solution
pythonhomeworkhelp.com
Divide list into left and right subsets of about the same size Sort
each sublist recursively by re-applying merge sort
Merge the returned (sorted) sublists
Problem 6
def findNumber(maxVal): """
Assumes that maxVal is a positive integer. Returns a number, num, such
that cmpGuess(num) == 0
"""
s = range(0, maxVal)
return bsearch(s, 0, len(s) - 1)
Probem 5
If the list is of length 0 or 1, then return the list Otherwise,
•wi — weight of item i
•C — maximum weight allotted
2. Maximize formula 1 while obeying the constraint of
formula 2.
pythonhomeworkhelp.com
def bsearch(s, first, last): if (last
- first) < 2:
if cmpGuess(s[first]) == 0: return
first
else:
return last
mid = first + (last - first)/2 if
cmpGuess(s[mid]) == 0:
return s[mid]
if cmpGuess(s[mid]) == -1:
return bsearch(s, first, mid - 1) return
bsearch(s, mid + 1, last)
def cmpGuess(guess):
"""
Assumes that guess is an integer in
range(maxVal). Returns -1 if guess
is < magic number, 0 if ==, 1 if > """
pythonhomeworkhelp.com

Perfect Python Homework Help

  • 1.
    For any helpregarding Python Homework Help visit : - https://www.pythonhomeworkhelp.com/, Email :- support@pythonhomeworkhelp.com or call us at :- +1 678 648 4277
  • 2.
    1)Is each ofthe following True or False 1.A greedy algorithm can be used to solve the 0-1 knapsack optimization problem. 2.Dynamic programming can be used to solve optimization problems where the size of the space of possible solutions is exponentially large. 3.Dynamic programming can be used to find an approximate solution to an optimization problem, but cannot be used to find a solution that is guaranteed to be optimal. 4.In Python, an instance of a class is an object, but a class itself is not. 5.In Python, a subclass can override a function definition contained in a super class. 6.Decision trees are always binary. pythonhomeworkhelp.com Problem
  • 3.
    2) Provide codeimplementing a Python function that meets the specification below. def findMedian(L): """Finds median of L. L: a non-empty list of floats Returns: If L has an odd number of elements, returns the median element of L. For example, if L is the list [15.0, 5.3, 18.2], returns 15.0. If L has an even number of elements, returns the average of the two median elements. For example, if L is the list [1.0, 2.0, 3.0, 4.0], returns 2.5. If the list is empty, raises a ValueError exception. Side effects: none. """ pythonhomeworkhelp.com
  • 4.
    3) What doesthe following code print? class Shape(object): def __cmp__(s1, s2): return cmp(s1.area(), s2.area()) class Square(Shape): def __init__(self, h): self.side = float(h) def area(self): return self.side**2 def __str__(self): return 'Square with side ' + str(self.side) class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14159*(self.radius**2) def __str__(self): return 'Circle with radius ' + str(self.radius) pythonhomeworkhelp.com
  • 5.
    def f(L): if len(L)== 0: return None x = L[0] for s in L: if s >= x: x = s return x s = Square(4) print s.area() L = [] shapes = {0:Circle, 1: Square} for i in range(10): L.append(shapes[i%2](i) ) print L[4] print f(L) 4) The following two formulas can be used to formalize a 0-1 knapsack problem. 1 ) 2) 4.1) What do each of n, pi, xi, wi, and C represent? pythonhomeworkhelp.com
  • 6.
    4.2) Use theformulas (refer to them as “formula 1” and “formula 2”) to describe the optimization problem to be solved. 5. Write pseudo code describing merge sort. 6) Consider the two functions specified below that are used to play a “guess a number game.” def cmpGuess(guess): """Assumes that guess is an integer in range(maxVal). returns -1 if guess is < than the magic number, 0 if it is equal to the magic number and 1 if it is greater than the magic number.""" def findNumber(maxVal): """Assumes that maxVal is a positive integer. Returns a number, num, such that cmpGuess(num) == 0.""" Write a Python implementation of findNumber that guesses the magic number defined by cmpGuess. Your program should have the lowest time complexity possible. pythonhomeworkhelp.com
  • 7.
    Problem 1 1. False.2. True. 3. False. 4. False. 5. True. 6. False. Problem 2 def findMedian(L): if len(L) == 0: raise ValueError("Empty list") copy = L[:] copy.sort() if len(copy) % 2 == 1: return copy[len(copy) / 2] else: return (copy[len(copy) / 2] + copy[len(copy) / 2 - 1]) / 2 Problem 3 16.0 Circle with radius 4 Circle with radius 8 Problem 4 1. •n — total number of items •pi — value of item i •xi — value is 1 if item i is taken; 0 otherwise Solution pythonhomeworkhelp.com
  • 8.
    Divide list intoleft and right subsets of about the same size Sort each sublist recursively by re-applying merge sort Merge the returned (sorted) sublists Problem 6 def findNumber(maxVal): """ Assumes that maxVal is a positive integer. Returns a number, num, such that cmpGuess(num) == 0 """ s = range(0, maxVal) return bsearch(s, 0, len(s) - 1) Probem 5 If the list is of length 0 or 1, then return the list Otherwise, •wi — weight of item i •C — maximum weight allotted 2. Maximize formula 1 while obeying the constraint of formula 2. pythonhomeworkhelp.com
  • 9.
    def bsearch(s, first,last): if (last - first) < 2: if cmpGuess(s[first]) == 0: return first else: return last mid = first + (last - first)/2 if cmpGuess(s[mid]) == 0: return s[mid] if cmpGuess(s[mid]) == -1: return bsearch(s, first, mid - 1) return bsearch(s, mid + 1, last) def cmpGuess(guess): """ Assumes that guess is an integer in range(maxVal). Returns -1 if guess is < magic number, 0 if ==, 1 if > """ pythonhomeworkhelp.com