SlideShare a Scribd company logo
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

More Related Content

PDF
Python Homework Sample
PPT
FUNDAMETAL ALG.ppt
DOCX
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PPTX
Numerical analysisgroup19
PPTX
Introduction to Python Programming.pptx
PPT
Recursion
PPTX
Algorithm, Concepts in performance analysis
PPTX
Python Programming Homework Help.pptx
Python Homework Sample
FUNDAMETAL ALG.ppt
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
Numerical analysisgroup19
Introduction to Python Programming.pptx
Recursion
Algorithm, Concepts in performance analysis
Python Programming Homework Help.pptx

Similar to Perfect Python Homework Help (20)

PPTX
Introduction to Python Programming.pptx
PPT
Stdlib functions lesson
PPTX
Introduction to Python Programming.pptx
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
PDF
Python for Scientific Computing
PPTX
Python Workshop - Learn Python the Hard Way
PPTX
Introduction to Python External Course !!!
PPT
recursion based on koffmann and wolfgang
PPTX
Polymorphism.pptx
PPT
python language programming presentation
PDF
Introduction to Functional Programming
DOCX
Python lab manual all the experiments are available
PPT
Python High Level Functions_Ch 11.ppt
PPTX
matlab presentation fro engninering students
PDF
CE344L-200365-Lab2.pdf
PDF
Real World Haskell: Lecture 6
PPTX
Advanced Programming_Basics of functional Programming.pptx
PPT
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
PDF
High-Performance Haskell
Introduction to Python Programming.pptx
Stdlib functions lesson
Introduction to Python Programming.pptx
Book.pdf01_Intro.ppt algorithm for preperation stu used
Python for Scientific Computing
Python Workshop - Learn Python the Hard Way
Introduction to Python External Course !!!
recursion based on koffmann and wolfgang
Polymorphism.pptx
python language programming presentation
Introduction to Functional Programming
Python lab manual all the experiments are available
Python High Level Functions_Ch 11.ppt
matlab presentation fro engninering students
CE344L-200365-Lab2.pdf
Real World Haskell: Lecture 6
Advanced Programming_Basics of functional Programming.pptx
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
High-Performance Haskell
Ad

More from Python Homework Help (20)

PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Complete my Python Homework
PPTX
Introduction to Python Dictionary.pptx
PPTX
Basic Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Quality Python Homework Help
PPTX
Python Homework Help
PPTX
Quality Python Homework Help
PPTX
Python Homework Help
PPTX
Python Programming Homework Help
PPTX
Python Homework Help
Python Homework Help
Python Homework Help
Python Homework Help
Python Homework Help
Python Homework Help
Complete my Python Homework
Introduction to Python Dictionary.pptx
Basic Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Python Homework Help
Python Homework Help
Quality Python Homework Help
Python Homework Help
Quality Python Homework Help
Python Homework Help
Python Programming Homework Help
Python Homework Help
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
GDM (1) (1).pptx small presentation for students
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Basic Mud Logging Guide for educational purpose
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPH.pptx obstetrics and gynecology in nursing
O5-L3 Freight Transport Ops (International) V1.pdf
Anesthesia in Laparoscopic Surgery in India
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Perfect Python Homework Help

  • 1. For any help regarding Python Homework Help visit : - https://www.pythonhomeworkhelp.com/, Email :- support@pythonhomeworkhelp.com or call us at :- +1 678 648 4277
  • 2. 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
  • 3. 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
  • 4. 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
  • 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 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
  • 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 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
  • 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