For any help regarding Python Homework Help
visit : - https://www.pythonhomeworkhelp.com/,
Email :- support@pythonhomeworkhelp.com or
call us at :- +1 678 648 4277
Questions:
1. Are each of the following True or False:
(a) Any program that can be written using only function definitions and calls, the basic
arithmetic operators, assignment, and conditionals will run in constant time.
(b) Newton’s method will always converge on a correct root of a function.
(c) In Python, dictionaries are immutable.
(d) The value of ‘math.sqrt(2.0)*math.sqrt(2.0) == 2.0’ is True.
(e) One should always avoid iteration when a recursive solution is possible.
(f) Typically, the use of functions in a program reduces the total number of lines of code.
(g) In Python, retrieving the value associated with a dictionary key takes roughly constant time.
2. Consider the implementations of compare1 and compare 2, where a and b are
floats.
2.1) Do compare1 and compare2 return the same value for all possible inputs? If
not, give a pair of inputs for which they return a different value.
2.2) Do compare1 and compare2 print the same thing for all possible inputs? If not,
give a pair of inputs for which they print different things.
def compare1(a, 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) == absolute_value(b)
if res:
print a, 'and', b, 'have the 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:])
(3.1) 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 one side
of the rectangle. Returns a floating point number that is the length of the adjacent
side."""
7) Does the following function meet its specification? If not, change the program so
that it is consistent with the specification.
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.
9) 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]))
Solution:
a) False. Recursion means your program can run indefinitely.
b) False. You may end up jumping back and forth between the same two forever,
given an S-shaped function.
c) False. mydict[somekey] = somevalue.
d) False. Precision is finite.
e) False. Recursion may be a more natural way to express certain problems (e.g.:
fib, Towers of Hanoi).
f) True. Code reuse.
g) True. A quick lookup.
2)
2.1) Yes, they return the same value for all possible inputs.
2.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.
3) Note about this function: it is a bit strange in that it handles multiple argument
types.
3.1) f(2112) returns 2+1+f(’12’) ==> 2+1+1+2 ==> 6.
3.2) Given an integer or a string representation of an integer, f returns the sum of its
digits.
4)
def first_N(n):
count = 0
current_sqrt = 1
while count < n:
square = current_sqrt * current_sqrt
# If square is not even
if square % 2 != 0:
print square
count += 1
current_sqrt += 1
5)
def guess_and_check(criteria):
for a in range(...):
for b in range(...):
for c in range(...):
...
if satisfies_criteria(a, b, c, ..., criteria):
return a, b, c,...
6.)
def findSide():
area = float( raw_input(’Enter the area of the rectangle: ’) )
side1 = float( raw_input(’Enter the length of one side of the rectangle: ’) )
return area / side1
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.
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):
if 6*a + 9*b + 20*c == num:
return True
return False
9)
Given an integer, take the string representation of that integer and reverse its
digits (returning this as a string).

More Related Content

PPTX
Introduction to Python Programming.pptx
DOCX
NPTEL QUIZ.docx
PDF
ComputerScience-SQP.pdffhtu h kya hua hai ap ka school
PDF
Python Homework Sample
DOCX
Question 1 briefly respond to all the following questions. make
PPTX
Chapter 02 functions -class xii
PPTX
Perfect Python Homework Help
PDF
selfstudys_com_file (4).pdfjsjdcjjsjxjdnxjj
Introduction to Python Programming.pptx
NPTEL QUIZ.docx
ComputerScience-SQP.pdffhtu h kya hua hai ap ka school
Python Homework Sample
Question 1 briefly respond to all the following questions. make
Chapter 02 functions -class xii
Perfect Python Homework Help
selfstudys_com_file (4).pdfjsjdcjjsjxjdnxjj

Similar to Introduction to Python Programming.pptx (20)

PDF
(2 - 1) CSE1021 - Module 2 Worksheet.pdf
PDF
Computer science sqp
DOCX
Python Laboratory Programming Manual.docx
PDF
Ee693 sept2014midsem
PDF
Xi CBSE Computer Science lab programs
PPTX
Introduction to Python Programming.pptx
DOCX
Ecs 10 programming assignment 4 loopapalooza
PDF
python practicals-solution-2019-20-class-xii.pdf
DOCX
Functions Practice Sheet.docx
PDF
09 a1ec01 c programming and data structures
DOCX
Mcq cpup
PPTX
ForLoopandUserDefinedFunctions.pptx
PDF
python_lab_manual_final (1).pdf
PDF
E10
PDF
Python cheatsheat.pdf
PDF
Gate-Cs 2007
PDF
XII CS QUESTION BANK FOR BRIGHT STUDENTS CHAPTER WISE SET-II.pdf
PDF
class 12 computer science pdf of class e
DOCX
QUESTION 11. Which of these is not a core datatypeLists.docx
PPTX
Python Programming Homework Help.pptx
(2 - 1) CSE1021 - Module 2 Worksheet.pdf
Computer science sqp
Python Laboratory Programming Manual.docx
Ee693 sept2014midsem
Xi CBSE Computer Science lab programs
Introduction to Python Programming.pptx
Ecs 10 programming assignment 4 loopapalooza
python practicals-solution-2019-20-class-xii.pdf
Functions Practice Sheet.docx
09 a1ec01 c programming and data structures
Mcq cpup
ForLoopandUserDefinedFunctions.pptx
python_lab_manual_final (1).pdf
E10
Python cheatsheat.pdf
Gate-Cs 2007
XII CS QUESTION BANK FOR BRIGHT STUDENTS CHAPTER WISE SET-II.pdf
class 12 computer science pdf of class e
QUESTION 11. Which of these is not a core datatypeLists.docx
Python Programming Homework Help.pptx
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
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
IGGE1 Understanding the Self1234567891011
PDF
International_Financial_Reporting_Standa.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
Computer Architecture Input Output Memory.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
HVAC Specification 2024 according to central public works department
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Trump Administration's workforce development strategy
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
AI-driven educational solutions for real-life interventions in the Philippine...
IGGE1 Understanding the Self1234567891011
International_Financial_Reporting_Standa.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Computer Architecture Input Output Memory.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Complications of Minimal Access-Surgery.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Empowerment Technology for Senior High School Guide
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
HVAC Specification 2024 according to central public works department
Introduction to pro and eukaryotes and differences.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Trump Administration's workforce development strategy

Introduction to Python Programming.pptx

  • 1. For any help regarding Python Homework Help visit : - https://www.pythonhomeworkhelp.com/, Email :- [email protected] or call us at :- +1 678 648 4277
  • 2. Questions: 1. Are each of the following True or False: (a) Any program that can be written using only function definitions and calls, the basic arithmetic operators, assignment, and conditionals will run in constant time. (b) Newton’s method will always converge on a correct root of a function. (c) In Python, dictionaries are immutable. (d) The value of ‘math.sqrt(2.0)*math.sqrt(2.0) == 2.0’ is True. (e) One should always avoid iteration when a recursive solution is possible. (f) Typically, the use of functions in a program reduces the total number of lines of code. (g) In Python, retrieving the value associated with a dictionary key takes roughly constant time.
  • 3. 2. Consider the implementations of compare1 and compare 2, where a and b are floats. 2.1) Do compare1 and compare2 return the same value for all possible inputs? If not, give a pair of inputs for which they return a different value. 2.2) Do compare1 and compare2 print the same thing for all possible inputs? If not, give a pair of inputs for which they print different things. def compare1(a, 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
  • 4. def absolute_value(n): if n < 0: n = -n return n def compare2(a, b): res = absolute_value(a) == absolute_value(b) if res: print a, 'and', b, 'have the 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])
  • 5. if len(xs) == 2: return n else: return n + f(xs[2:]) (3.1) 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 one side of the rectangle. Returns a floating point number that is the length of the adjacent side."""
  • 6. 7) Does the following function meet its specification? If not, change the program so that it is consistent with the specification. 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. 9) 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]))
  • 7. Solution: a) False. Recursion means your program can run indefinitely. b) False. You may end up jumping back and forth between the same two forever, given an S-shaped function. c) False. mydict[somekey] = somevalue. d) False. Precision is finite. e) False. Recursion may be a more natural way to express certain problems (e.g.: fib, Towers of Hanoi). f) True. Code reuse. g) True. A quick lookup. 2) 2.1) Yes, they return the same value for all possible inputs. 2.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. 3) Note about this function: it is a bit strange in that it handles multiple argument types.
  • 8. 3.1) f(2112) returns 2+1+f(’12’) ==> 2+1+1+2 ==> 6. 3.2) Given an integer or a string representation of an integer, f returns the sum of its digits. 4) def first_N(n): count = 0 current_sqrt = 1 while count < n: square = current_sqrt * current_sqrt # If square is not even if square % 2 != 0: print square count += 1 current_sqrt += 1 5) def guess_and_check(criteria): for a in range(...):
  • 9. for b in range(...): for c in range(...): ... if satisfies_criteria(a, b, c, ..., criteria): return a, b, c,... 6.) def findSide(): area = float( raw_input(’Enter the area of the rectangle: ’) ) side1 = float( raw_input(’Enter the length of one side of the rectangle: ’) ) return area / side1 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. 8) Note the resemblance to the exhaustive enumeration for guess-and-check, in Problem 5.
  • 10. 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): if 6*a + 9*b + 20*c == num: return True return False 9) Given an integer, take the string representation of that integer and reverse its digits (returning this as a string).