Skip to content

Commit c90879a

Browse files
committed
Solutions
1 parent 9a8a2f6 commit c90879a

21 files changed

+840
-0
lines changed

CH6Module/MyFunctions.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#############################################################
2+
# This file contains all functions required for this chapter
3+
#############################################################
4+
5+
6+
# Converts from Celsius to Fahrenheit
7+
def celsiusToFahrenheit(celsius):
8+
fahrenheit = (9 / 5) * celsius + 32
9+
return fahrenheit
10+
11+
12+
# Converts from Fahrenheit to Celsius
13+
def fahrenheitToCelsius(fahrenheit):
14+
celsius = (5 / 9) * (fahrenheit - 32)
15+
return celsius
16+
17+
18+
# Converts from feet to meters
19+
def footToMeter(foot):
20+
meter = 0.305 * foot
21+
return meter
22+
23+
24+
# Converts from meters to feet
25+
def meterToFoot(meter):
26+
foot = meter / 0.305
27+
return foot
28+
29+
30+
# Check whether number is prime
31+
def isPrime(number):
32+
divisor = 2
33+
while divisor <= number / 2:
34+
if number % divisor == 0:
35+
# If true, number is not prime
36+
return False # number is not a prime
37+
divisor += 1
38+
return True # number is prime
39+
40+
41+
def computeCommission(salesAmount):
42+
commission = 0
43+
if salesAmount > 10000:
44+
commission = (5000 * 0.08) + (5000 * 0.1) + (salesAmount - 10000) * 0.12
45+
elif salesAmount > 5000:
46+
commission = 5000 * 0.08 + (salesAmount - 5000) * 0.1
47+
else:
48+
commission = salesAmount * 0.08
49+
50+
return commission
51+
52+
53+
def computePi(i):
54+
sum = 0
55+
for j in range(1, i + 1):
56+
sum += ((-1) ** (j + 1)) / (2 * j - 1)
57+
58+
pi = 4 * sum
59+
return pi
60+
61+
62+
def computeTax(status, taxableIncome):
63+
tax = 0
64+
if status == 0:
65+
if taxableIncome <= 8350:
66+
tax = taxableIncome * 0.10
67+
elif taxableIncome <= 33950:
68+
tax = 8350 * 0.10 + taxableIncome - 8350 * 0.15
69+
elif taxableIncome <= 82250:
70+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + taxableIncome - 33950 * 0.25
71+
elif taxableIncome <= 171550:
72+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 82250 - 33950 \
73+
* 0.25 + taxableIncome - 82250 * 0.28
74+
elif taxableIncome <= 372950:
75+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 82250 - 33950 \
76+
* 0.25 + 171550 - 82250 * 0.28 + taxableIncome - 171550 * 0.33
77+
else:
78+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 82250 - 33950 \
79+
* 0.25 + 171550 - 82250 * 0.28 + 372950 - 171550 \
80+
* 0.33 + taxableIncome - 372950 * 0.35
81+
elif status == 1:
82+
if taxableIncome <= 16700:
83+
tax = taxableIncome * 0.10
84+
elif taxableIncome <= 67900:
85+
tax = 16700 * 0.10 + taxableIncome - 16700 * 0.15
86+
elif taxableIncome <= 137050:
87+
tax = 16700 * 0.10 + 67900 - 16700 * 0.15 \
88+
+ taxableIncome - 67900 * 0.25
89+
elif taxableIncome <= 208850:
90+
tax = 16700 * 0.10 + 67900 - 16700 * 0.15 + 137050 - 67900 \
91+
* 0.25 + taxableIncome - 137050 * 0.28
92+
elif taxableIncome <= 372950:
93+
tax = 16700 * 0.10 + 67900 - 16700 * 0.15 + 137050 - 67900 \
94+
* 0.25 + 208850 - 137050 * 0.28 \
95+
+ taxableIncome - 208850 * 0.33
96+
else:
97+
tax = 16700 * 0.10 + 67900 - 16700 * 0.15 + 137050 - 67900 \
98+
* 0.25 + 208850 - 137050 * 0.28 + 372950 - 208850 \
99+
* 0.33 + taxableIncome - 372950 * 0.35
100+
elif status == 2:
101+
if taxableIncome <= 8350:
102+
tax = taxableIncome * 0.10
103+
elif taxableIncome <= 33950:
104+
tax = 8350 * 0.10 + taxableIncome - 8350 * 0.15
105+
elif taxableIncome <= 68525:
106+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 \
107+
+ taxableIncome - 33950 * 0.25
108+
elif taxableIncome <= 104425:
109+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 68525 - 33950 \
110+
* 0.25 + taxableIncome - 68525 * 0.28
111+
elif taxableIncome <= 186475:
112+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 68525 - 33950 \
113+
* 0.25 + 104425 - 68525 * 0.28 \
114+
+ taxableIncome - 104425 * 0.33
115+
else:
116+
tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 68525 - 33950 \
117+
* 0.25 + 104425 - 68525 * 0.28 + 186475 - 104425 \
118+
* 0.33 + taxableIncome - 186475 * 0.35
119+
elif status == 3:
120+
if taxableIncome <= 11950:
121+
tax = taxableIncome * 0.10
122+
elif taxableIncome <= 45500:
123+
tax = 11950 * 0.10 + taxableIncome - 11950 * 0.15
124+
elif taxableIncome <= 117450:
125+
tax = 11950 * 0.10 + 45500 - 11950 * 0.15 \
126+
+ taxableIncome - 45500 * 0.25
127+
elif taxableIncome <= 190200:
128+
tax = 11950 * 0.10 + 45500 - 11950 * 0.15 + 117450 - 45500 \
129+
* 0.25 + taxableIncome - 117450 * 0.28
130+
elif taxableIncome <= 372950:
131+
tax = 11950 * 0.10 + 45500 - 11950 * 0.15 + 117450 - 45500 \
132+
* 0.25 + 190200 - 117450 * 0.28 \
133+
+ taxableIncome - 190200 * 0.33
134+
else:
135+
tax = 11950 * 0.10 + 45500 - 11950 * 0.15 + 117450 - 45500 \
136+
* 0.25 + 190200 - 117450 * 0.28 + 372950 - 190200 \
137+
* 0.33 + taxableIncome - 372950 * 0.35
138+
else:
139+
print("Error: invalid status")
140+
return 0
141+
142+
return tax
143+
144+
145+
def numberOfDaysInAYear(year):
146+
if isLeapYear(year):
147+
return 366
148+
else:
149+
return 365
150+
151+
152+
# Determine if it is a leap year *
153+
def isLeapYear(year):
154+
return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
155+
156+
157+
# Returns true if the sum of any two sides is
158+
# greater than the third side.
159+
def isValid(side1, side2, side3):
160+
b1 = side1 + side2 > side3
161+
b2 = side2 + side3 > side1
162+
b3 = side1 + side3 > side2
163+
return (b1 and b2 and b3)
164+
165+
166+
# Returns the area of the triangle.
167+
def area(side1, side2, side3):
168+
if isValid(side1, side2, side3):
169+
s = (side1 + side2 + side3) / 2
170+
area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5
171+
return area
172+
173+
174+
# Return true if point (x2, y2) is on the left side of the
175+
# directed line from (x0, y0) to (x1, y1)
176+
def leftOfTheLine(x0, y0, x1, y1, x2, y2):
177+
d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)
178+
return d > 0
179+
180+
181+
# Return true if point (x2, y2) is on the same
182+
# line from (x0, y0) to (x1, y1)
183+
def onTheSameLine(x0, y0, x1, y1, x2, y2):
184+
d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)
185+
return d == 0
186+
187+
188+
# Return true if point (x2, y2) is on the
189+
# line segment from (x0, y0) to (x1, y1)
190+
def onTheLineSegment(x0, y0, x1, y1, x2, y2):
191+
d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)
192+
return d < 0
193+
194+
195+
def sqrt(n):
196+
lastGuess = n
197+
init = 0.0001
198+
nextGuess = (lastGuess + (n / lastGuess)) / 2
199+
while (lastGuess - nextGuess) >= init:
200+
lastGuess = nextGuess
201+
nextGuess = (lastGuess + (n / lastGuess)) / 2
202+
203+
return nextGuess
204+
205+
206+
def isPalindrome(number):
207+
return number == reverse(number)
208+
209+
210+
# Return the reversal of an integer, i.e. reverse(456) returns 654
211+
def reverse(number):
212+
result = 0
213+
while number != 0:
214+
remainder = number % 10
215+
result = result * 10 + remainder
216+
number = number // 10
217+
218+
return result

CH7/Account.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Account:
2+
def __init__(self,id=0,balance=100,annualIntersetRate=0):
3+
self.__id = id
4+
self.__balance = balance
5+
self.__annualIntersetRate = annualIntersetRate
6+
7+
def getId(self):
8+
return self.__id
9+
10+
def getBalance(self):
11+
return self.__balance
12+
13+
def getAnnualIntersetRate(self):
14+
return self.__annualIntersetRate
15+
16+
def setId(self,id):
17+
self.__id = id
18+
19+
def setBalance(self,balance):
20+
self.__balance = balance
21+
22+
def setAnnualIntersetRate(self,annualIntersetRate):
23+
self.__annualIntersetRate = annualIntersetRate
24+
25+
def getMonthlyInterestRate(self):
26+
return (self.__annualIntersetRate/ 12) /100
27+
28+
def getMonthlyInterest(self):
29+
return self.__balance *self.getMonthlyInterestRate()
30+
31+
def deposit(self,balance):
32+
self.__balance += balance
33+
34+
def withdraw(self,amount):
35+
self.__balance -= amount
36+

CH7/Checkpoints

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#7.1
2+
Objects of the same kind are defined by using a common class. The relationship between
3+
classes and objects is analogous to that between an apple-pie recipe and apple pies. You can
4+
make as many apple pies (objects) as you want from a single recipe (class).
5+
6+
#7.2
7+
class ClassName:
8+
initializer
9+
methods
10+
11+
#7.3
12+
objName = ClassName(args)
13+
14+
#7.4
15+
__init__
16+
17+
#7.5
18+
self is a parameter that references the object itself. Using self, you can access object’s
19+
members in a class definition.
20+
21+
#7.6
22+
ClassName(args)
23+
The arguments of the constructor match the parameters in the __init__ method without self.
24+
The constructor first creates an object in the memory and then invokes the initializer.
25+
26+
#7.7
27+
Initializer is a special method that is used to initialize object's data when creating object.
28+
29+
#7.8
30+
dot operator(.).
31+
32+
#7.9
33+
Variable i in class a isn't initialized.
34+
Fix: a = A(x), where x is any value.
35+
36+
#7.10
37+
(a) The constructor should be __init__(self).
38+
(b) radius = 3 should be self.radius = 3.
39+
40+
#7.11
41+
count is 100
42+
times is 0
43+
44+
#7.12
45+
count is 0
46+
n is 1
47+
48+
#7.13
49+
AttributeError: no attribute '__i', the i attribute is
50+
a private data field.
51+
To fix this we can make i public by removing the leading __
52+
or provide getter method.
53+
54+
#7.14
55+
Yes. It prints "Welcome"
56+
57+
#7.15
58+
The code is not correct.
59+
To fix this we can make on attribute public by
60+
removing leading __ or provide getter method to
61+
on attribute:
62+
class A:
63+
def __init__(self, on):
64+
self.on = not on
65+
OR
66+
class A:
67+
def __init__(self, on):
68+
self.__on = not on
69+
def isOn(self,on):
70+
return self.__on
71+
72+
def main():
73+
a = A(False)
74+
print(a.isOn)
75+
76+
main() # Call the main function
77+
78+
#7.16
79+
Making data fields private protects data and makes the class easy to maintain.
80+
To prevent direct modifications of data fields, don’t let the client directly access data fields.
81+
This is known as data hiding. This can be done by defining private data fields. In Python, the
82+
private data fields are defined with two leading underscores. You can also define a private
83+
method named with two leading underscores.
84+
85+
#7.17
86+
def __methodName(parms):
87+
88+
#7.18
89+
The object-oriented approach combines the power of the procedural paradigm
90+
with an added dimension that integrates data with operations into objects.
91+
In procedural programming, data and operations are separate, and this methodology
92+
requires sending data to methods. Object-oriented programming places data and the operations
93+
that pertain to them together in an object. This approach solves many of the problems
94+
inherent in procedural programming. The object-oriented programming approach organizes
95+
programs in a way that mirrors the real world, in which all objects are associated with both
96+
attributes and activities. Using objects improves software reusability and makes programs
97+
easier to develop and easier to maintain.

CH7/EX7.1.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 7.1 (The Rectangle class) Following the example of the Circle class in Section
2+
# 7.2, design a class named Rectangle to represent a rectangle. The class
3+
# contains:
4+
# ■ Two data fields named width and height.
5+
# ■ A constructor that creates a rectangle with the specified width and height.
6+
# The default values are 1 and 2 for the width and height, respectively.
7+
# ■ A method named getArea() that returns the area of this rectangle.
8+
# ■ A method named getPerimeter() that returns the perimeter.
9+
# Draw the UML diagram for the class, and then implement the class. Write a test
10+
# program that creates two Rectangle objects—one with width 4 and height 40
11+
# and the other with width 3.5 and height 35.7. Display the width, height, area,
12+
# and perimeter of each rectangle in this order.
13+
from CH7.Rectangle import Rectangle
14+
15+
rect1 = Rectangle(4, 40)
16+
rect2 = Rectangle(3.5, 35.7)
17+
area1 = rect1.getArea()
18+
area2 = rect2.getArea()
19+
per1 = rect1.getPerimeter()
20+
per2 = rect2.getPerimeter()
21+
22+
print("Rectangle1:\n\tWidth =", rect1.width, "\n\tHeight =", rect1.height,
23+
"\n\tArea =", area1, "\n\tPerimeter =", per1)
24+
25+
print("Rectangle2:\n\tWidth =", rect2.width, "\n\tHeight =", rect2.height,
26+
"\n\tArea =", area2, "\n\tPerimeter =", per2)

0 commit comments

Comments
 (0)