Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a8a2f6

Browse files
Solutions
1 parent a42da7f commit 9a8a2f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1865
-0
lines changed

‎CH6/Checkpoints

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#6.1
2+
(1) Reuse code; (2) Reduce complexity; (3) Easy to maintain
3+
4+
#6.2
5+
You can define a function using def keyword followed by function name
6+
ex: def myFuction(param1):
7+
...statements...
8+
9+
To inovke a function, just write it's name in the proper line
10+
11+
#6.3
12+
def max(n1,n2):
13+
return n1 if n1>n2 else n2
14+
15+
#6.4
16+
A call to a None function is always a statement itself(TRUE), but a call to a
17+
value-returning function is always a component of an expression(FALSE).
18+
19+
#6.5
20+
Yes,we can have a return statement in in a None function.
21+
No,no syntax errors.
22+
23+
#6.6
24+
The header begins with the def keyword, followed
25+
by the function’s name and parameters, and ends with a colon.
26+
The variables in the function header are known as formal parameters or simply parameters.
27+
A parameter is like a placeholder: When a function is invoked, you pass a value to the parameter.
28+
This value is referred to as an actual parameter or argument.
29+
30+
#6.7
31+
(1) def computeSalesCommisions(salesAmount,salesRate):. Should return a value.
32+
(2) def printCalendar(month,year):. No return is needed.
33+
(3) def squarRoot(num):. Either print the result or return return it.
34+
(4) def isEven(n):. should return a value.
35+
(5) def computeMonthlyPayment(loanAmount,years,annualInterestRate):. Either print the result or return return it.
36+
(6) def upper(c):. No return is needed.
37+
38+
#6.8
39+
8. Line 2, 5-10: not indented correctly.
40+
41+
#6.9
42+
None.
43+
44+
#6.10
45+
The program prints None, as min function does not return a value.
46+
47+
#6.11
48+
In positional arguments, the arguments are passed in the same order as
49+
their respective parameters in the function header.
50+
In keyword arguments, passing each argument in the form name=value.
51+
52+
#6.12
53+
f(1, p2 = 3, p3 = 4, p4 = 4): correct
54+
f(1, p2 = 3, 4, p4 = 4): wrong
55+
f(p1 = 1, p2 = 3, 4, p4 = 4): wrong
56+
f(p1 = 1, p2 = 3, p3 = 4, p4 = 4): correct
57+
f(p4 = 1, p2 = 3, p3 = 4, p1 = 4): correct
58+
59+
#6.13
60+
A variable for an object is actually a reference to the object.
61+
When you invoke a function with arguments, the reference value of each argument is
62+
passed to the parameter. This is referred to as pass-by-value in programming terminology
63+
64+
#6.14
65+
Yes.
66+
67+
#6.15
68+
(a) 0
69+
(b)
70+
71+
2
72+
2 4
73+
2 4 8
74+
2 4 8 16
75+
2 4 8 16 32
76+
77+
(c)
78+
Before the call, variable times is 3
79+
n = 3
80+
Welcome to CS!
81+
n = 2
82+
Welcome to CS!
83+
n = 1
84+
Welcome to CS!
85+
After the call, variable times is 3
86+
87+
(d)
88+
89+
90+
i is 1
91+
1
92+
i is 2
93+
2 1
94+
i is 3
95+
then an infinite loop
96+
97+
i is 1
98+
1
99+
i is 2
100+
2 1
101+
i is 3
102+
103+
#6.16
104+
-Before invoking max, the stack contains the space required for the main function
105+
with max variable = 0.
106+
-In the max function, the first element in the stack is the space required for max function
107+
with variables max = 0, value 1 =1 and value2 = 2. The next element in the stack is the
108+
space required for main function with max variable = 0.
109+
-Before max return statement, the elements are the same except that max = 2 in the space of max function.
110+
-Return back to main function, the stack contains only space required for main function with max = 0.
111+
112+
#6.17
113+
(a)
114+
2
115+
3.4
116+
2
117+
4
118+
(b)
119+
3
120+
6
121+
5
122+
123+
#6.18
124+
The variables x and y which are used in lines 8 and 9 are not
125+
defined outside the function.
126+
127+
#6.19
128+
Yes. It prints y is 1
129+
130+
#6.20
131+
1 2
132+
5 2
133+
1 24
134+
4 5
135+
136+
#6.21
137+
The parameter n (has non-default argument) should be defined before parameter message(has a default argument).
138+
139+
#6.22
140+
The later definition replaces the previous definitions.
141+
142+
#6.23
143+
Yes.
144+
14 4 45 1.8
145+
146+
#6.24
147+
random.randint(34, 55)
148+
149+
#6.25
150+
chr(random.randint(ord('B'),ord('M')))
151+
152+
#6.26
153+
6.5 + random.random() *49.9
154+
155+
#6.27
156+
chr(random.randint(ord('a'),ord('z')))

‎CH6/EX6.1.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 6.1 (Math: pentagonal numbers) A pentagonal number is defined as for
2+
# and so on. So, the first few numbers are 1, 5, 12, 22, .... Write a
3+
# function with the following header that returns a pentagonal number:
4+
# def getPentagonalNumber(n):
5+
# Write a test program that uses this function to display the first 100 pentagonal
6+
# numbers with 10 numbers on each line.
7+
8+
def getPentagonalNumber(n):
9+
num = n * (3 * n - 1) / 2
10+
return int(num)
11+
12+
13+
def main():
14+
count = 1
15+
for i in range(1, 101):
16+
print(getPentagonalNumber(i), end=' ')
17+
if count % 10 == 0:
18+
print()
19+
count = 0
20+
21+
count += 1
22+
23+
24+
main()

‎CH6/EX6.10.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 6.10 (Use the isPrime Function) Listing 6.7, PrimeNumberFunction.py, provides the
2+
# isPrime(number) function for testing whether a number is prime. Use this
3+
# function to find the number of prime numbers less than 10,000.
4+
from CH6Module import MyFunctions
5+
6+
count = 0
7+
for i in range(1, 10001):
8+
if MyFunctions.isPrime(i):
9+
count += 1
10+
11+
print("The number of primes less than 10,000 is", count)

‎CH6/EX6.11.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 6.11 (Financial application: compute commissions) Write a function that computes
2+
# the commission, using the scheme in Exercise 5.39. The header of the function is:
3+
# def computeCommission(salesAmount):
4+
# Write a test program that displays the following table:
5+
# Sales Amount Commission
6+
# 10000 900.0
7+
# 15000 1500.0
8+
# ...
9+
# 95000 11100.0
10+
# 100000 11700.0
11+
from CH6Module import MyFunctions
12+
13+
print("Sales Amount\t\tCommission")
14+
15+
salesAmount = 10000
16+
for i in range(1, 20):
17+
print(salesAmount, format(" ", "14s"), format(MyFunctions.computeCommission(salesAmount), ".1f"))
18+
salesAmount += 5000

‎CH6/EX6.12.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 6.12 (Display characters) Write a function that prints characters using the following
2+
# header:
3+
# def printChars(ch1, ch2, numberPerLine):
4+
# This function prints the characters between ch1 and ch2 with the specified
5+
# numbers per line. Write a test program that prints ten characters per line from 1
6+
# to Z.
7+
8+
def printChars(ch1, ch2, numberPerLine):
9+
count = 1
10+
for i in range(ord(ch1), ord(ch2)+1):
11+
print(chr(i), end=" ")
12+
if count % numberPerLine == 0:
13+
print()
14+
count += 1
15+
16+
17+
printChars("1", "z", 10)

‎CH6/EX6.13.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 6.13 (Sum series) Write a function to compute the following series:
2+
# Write a test program that displays the following table:
3+
# i m(i)
4+
# 1 0.5000
5+
# 2 1.1667
6+
# ...
7+
# 19 16.4023
8+
# 20 17.3546
9+
10+
def sumSeries(i):
11+
sum = 0
12+
for i in range(1, i + 1):
13+
sum += i / (i + 1)
14+
return sum
15+
16+
17+
def main():
18+
print("i\t m(i)")
19+
for i in range(1, 21):
20+
print(format(i, "-0.0f"), format(" ", "4s"), format(sumSeries(i), ".4f"))
21+
22+
23+
main()

‎CH6/EX6.14.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 6.14 (Estimate ) can be computed using the following series:
2+
# Write a function that returns m(i) for a given i and write a test program that displays
3+
# the following table:
4+
# i m(i)
5+
# 1 4.0000
6+
# 101 3.1515
7+
# 201 3.1466
8+
# 301 3.1449
9+
# 401 3.1441
10+
# 501 3.1436
11+
# 601 3.1433
12+
# 701 3.1430
13+
# 801 3.1428
14+
# 901 3.1427
15+
from CH6Module import MyFunctions
16+
17+
18+
def main():
19+
print("i\t\t\t m(i)")
20+
for i in range(1, 1001, 100):
21+
print(format(i, ".0f"), "\t\t", format(MyFunctions.computePi(i), "-3.4f"))
22+
23+
24+
main()

‎CH6/EX6.15.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 6.15 (Financial application: print a tax table) Listing 4.7, ComputeTax.py, gives a
2+
# program to compute tax. Write a function for computing tax using the following
3+
# header:
4+
# def computeTax (status, taxableIncome ):
5+
# Use this function to write a program that prints a tax table for taxable income from
6+
# 50,000ドル to 60,000ドル with intervals of 50ドル for all four statuses, as follows:
7+
# Taxable Single Married Married Head of
8+
# Income Joint Separate a House
9+
# 50000 8688 6665 8688 7352
10+
# 50050 8700 6673 8700 7365
11+
# ...
12+
# 59950 11175 8158 11175 9840
13+
# 60000 11188 8165 11188 9852
14+
from CH6Module import MyFunctions
15+
16+
print("Taxable\t\tSingle\t\tMarried\t\tMarried\t\tHead of")
17+
print("Income\t\t\t\t\tJoint\t\tSeparate\ta House")
18+
19+
for i in range(50000, 60001, 50):
20+
print(i, "\t ", MyFunctions.computeTax(0, i), "\t", MyFunctions.computeTax(1, i),
21+
"\t", MyFunctions.computeTax(2, i), "\t", MyFunctions.computeTax(3, i))

‎CH6/EX6.16.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 6.16 (Number of days in a year) Write a function that returns the number of days in a
2+
# year using the following header:
3+
# def numberOfDaysInAYear(year):
4+
# Write a test program that displays the number of days in the years from 2010 to
5+
# 2020.
6+
from CH6Module import MyFunctions
7+
8+
for year in range(2010, 2021):
9+
print(year, "has", MyFunctions.numberOfDaysInAYear(year))

‎CH6/EX6.17.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# *6.17 (The MyTriangle module) Create a module named MyTriangle that contains
2+
# the following two functions:
3+
# # Returns true if the sum of any two sides is
4+
# # greater than the third side.
5+
# def isValid(side1, side2, side3):
6+
# # Returns the area of the triangle.
7+
# def area(side1, side2, side3):
8+
# Write a test program that reads three sides for a triangle and computes the area if the
9+
# input is valid. Otherwise, it displays that the input is invalid. The formula for computing
10+
# the area of a triangle is given in Exercise 2.14.
11+
from CH6Module import MyFunctions
12+
13+
s1, s2, s3 = eval(input("Enter three sides in double: "))
14+
15+
if MyFunctions.isValid(s1, s2, s3):
16+
print("The area of the triangle is", MyFunctions.area(s1, s2, s3))
17+
else:
18+
print("Input is invalid")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /