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 cfd0676

Browse files
Solutions
1 parent b0fddc3 commit cfd0676

Some content is hidden

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

57 files changed

+2797
-0
lines changed

‎CH11/Checkpoints

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#11.1
2+
lst = [
3+
[0,0,0,0],
4+
[0,0,0,0],
5+
[0,0,0,0]
6+
]
7+
8+
#11.2
9+
Yes
10+
11+
#11.3
12+
[[2, 1, 1], [1, 1, 1], [1, 1, 1]]
13+
14+
#11.4
15+
[[[1, 1, 1]], [[1, 1, 1]], [[1, 1, 1]]]
16+
[3, [[1, 1, 1]], [[1, 1, 1]]]
17+
18+
#11.5
19+
[[1, 2, 3], [4, 5], [6, 7, 8, 9]]
20+
21+
22+
#11.6
23+
00
24+
01
25+
11
26+
12
27+
28+
#11.7
29+
0001
30+
0001
31+
1112
32+
1112

‎CH11/EX11.1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 11.1 (Sum elements column by column) Write a function that returns the sum of all the
2+
# elements in a specified column in a matrix using the following header:
3+
# def sumColumn(m, columnIndex):
4+
# Write a test program that reads a 3*4 matrix and displays the sum of each column.
5+
6+
def sumColumn(m, columnIndex):
7+
sum = 0
8+
for row in range(len(m)):
9+
sum += m[row][columnIndex]
10+
11+
return sum
12+
13+
14+
m = []
15+
16+
for i in range(3):
17+
row = input("Enter a 3-by-4 matrix row for row " + str(i)+": ").split()
18+
m.append([float(x) for x in row])
19+
20+
for i in range(4):
21+
s = sumColumn(m, i)
22+
print("Sum of the elements for column", i, 'is', s)

‎CH11/EX11.10.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 11.10 (Largest rows and columns) Write a program that randomly fills in 0s and 1s into
2+
# a 4*4 matrix, prints the matrix, and finds the rows and columns with the most
3+
# 1s.
4+
import random
5+
6+
7+
def count_ones(lst):
8+
count = 0
9+
current_count = count
10+
indx = []
11+
for row in range(len(lst)):
12+
for col in lst[row]:
13+
if col == 1:
14+
current_count += 1
15+
if current_count > count:
16+
indx.clear()
17+
count = current_count
18+
indx.append(row)
19+
elif current_count == count:
20+
indx.append(row)
21+
current_count = 0
22+
return indx
23+
24+
25+
lst = []
26+
for i in range(4):
27+
lst.append([])
28+
for j in range(4):
29+
lst[i].append(random.randint(0, 1))
30+
31+
for r in lst:
32+
for c in r:
33+
print(c, end=" ")
34+
print()
35+
36+
print("The largest row index:", count_ones(lst))
37+
38+
# transose matrix
39+
lst = [[row[i] for row in lst] for i in range(len(lst[0]))]
40+
41+
print("The largest column index:", count_ones(lst))

‎CH11/EX11.11.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 11.11 (Game: nine heads and tails) Nine coins are placed in a matrix with some
2+
# face up and some face down. You can represent the state of the coins with the values
3+
# 0 (heads) and 1 (tails). Here are some examples:
4+
# 0 0 0 1 0 1 1 1 0 1 0 1 1 0 0
5+
# 0 1 0 0 0 1 1 0 0 1 1 0 1 1 1
6+
# 0 0 0 1 0 0 0 0 1 1 0 0 1 1 0
7+
# Each state can also be represented using a binary number. For example, the preceding
8+
# matrices correspond to the numbers:
9+
# 000010000 101001100 110100001 101110100 100111110
10+
# There are a total of 512 possibilities. So, you can use the decimal numbers 0, 1, 2,
11+
# 3, ..., and 511 to represent all states of the matrix. Write a program that prompts
12+
# the user to enter a number between 0 and 511 and displays the corresponding
13+
# 3 * 3 matrix with the characters H and T.
14+
# The user entered 7, which corresponds to 000000111. Since 0 stands for H and 1 for T, the output is correct.
15+
16+
def dec_to_bin(num):
17+
bin = []
18+
while num > 0:
19+
bin.append(num % 2)
20+
num = num // 2
21+
22+
while len(bin) < 9:
23+
bin.append(0)
24+
25+
bin.reverse()
26+
return bin
27+
28+
29+
def print_matrix(mat):
30+
for i in range(len(mat)):
31+
if mat[i] == 0:
32+
print('H', end=' ')
33+
else:
34+
print('T', end=' ')
35+
36+
if (i + 1) % 3 == 0:
37+
print()
38+
39+
40+
num = int(input("Enter a number between 0 and 511: "))
41+
mat = dec_to_bin(num)
42+
print_matrix(mat)

‎CH11/EX11.12.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 11.12 (Financial application: compute tax) Rewrite Listing 4.7, ComputeTax.py, using
2+
# lists. For each filing status, there are six tax rates. Each rate is applied to a certain
3+
# amount of taxable income. For example, from the taxable income of 400,000ドル for
4+
# a single filer, 8,350ドル is taxed at 10%, (33,950 – 8,350) at 15%, (82,250 – 33,950)
5+
# at 25%, (171,550 – 82,250) at 28%, (372,950 – 171,550) at 33%, and (400,000 –
6+
# 372,950) at 35%. The six rates are the same for all filing statuses, which can be
7+
# represented in the following list:
8+
# rates = [0.10, 0.15, 0.25, 0.28, 0.33, 0.35]
9+
# The brackets for each rate for all the filing statuses can be represented in a twodimensional
10+
# list as follows:
11+
# brackets = [
12+
# [8350, 33950, 82250, 171550, 372950], # Single filer
13+
# [16700, 67900, 137050, 208850, 372950], # Married jointly
14+
# [8350, 33950, 68525, 104425, 186475], # Married separately
15+
# [11950, 45500, 117450, 190200, 372950] # Head of household
16+
# ]
17+
# Suppose the taxable income is 400,000ドル for single filers. The tax can be computed
18+
# as follows:
19+
# tax = brackets[0][0] * rates[0] +
20+
# (brackets[0][1] – brackets[0][0]) * rates[1] +
21+
# (brackets[0][2] – brackets[0][1]) * rates[2] +
22+
# (brackets[0][3] – brackets[0][2]) * rates[3] +
23+
# (brackets[0][4] – brackets[0][3]) * rates[4] +
24+
# (400000 – brackets[0][4]) * rates[5]
25+
26+
def main():
27+
status = eval(input("(0-single filer, 1-married jointly,\n" +
28+
"2-married separately, 3-head of household)\n" +
29+
"Enter the filing status: "))
30+
31+
# Prompt the user to enter taxable income
32+
income = eval(input("Enter the taxable income: "))
33+
34+
# Compute and display the result
35+
print("Tax is", format(computeTax(status, income), "7.2f"))
36+
37+
38+
def computeTax(status, income):
39+
rates = [0.10, 0.15, 0.25, 0.28, 0.33, 0.35]
40+
41+
brackets = [
42+
[8350, 33950, 82250, 171550, 372950], # Single filer
43+
[16700, 67900, 137050, 20885, 372950], # Married jointly
44+
[8350, 33950, 68525, 104425, 186475], # Married separately
45+
[11950, 45500, 117450, 190200, 372950] # Head of household
46+
]
47+
48+
tax = 0 # Tax to be computed
49+
50+
# Compute tax in the first bracket
51+
if income <= brackets[status][0]:
52+
return income * rates[0] # Done
53+
else:
54+
tax = brackets[status][0] * rates[0]
55+
56+
# Compute tax in the 2nd, 3rd, 4th, and 5th brackets, if needed
57+
for i in range(1, len(brackets[0])):
58+
if income > brackets[status][i]:
59+
tax += (brackets[status][i] - brackets[status][i - 1]) * rates[i]
60+
else:
61+
tax += (income - brackets[status][i - 1]) * rates[i]
62+
return tax # Done
63+
64+
# Compute tax in the last (i.e., 6th) bracket
65+
return tax + (income - brackets[status][4]) * rates[5]
66+
67+
68+
main()

‎CH11/EX11.13.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 11.13 (Locate the largest element) Write the following function that returns the location
2+
# of the largest element in a two-dimensional list:
3+
# def locateLargest(a):
4+
# The return value is a one-dimensional list that contains two elements. These
5+
# two elements indicate the row and column indexes of the largest element in the
6+
# two-dimensional list. Write a test program that prompts the user to enter a two dimensional
7+
# list and displays the location of the largest element in the list.
8+
9+
def locateLargest(a):
10+
max_indx_row = 0
11+
max_indx_col = 0
12+
for i in range(len(a)):
13+
for j in range(len(a[i])):
14+
if a[i][j] > a[max_indx_row][max_indx_col]:
15+
max_indx_row = i
16+
max_indx_col = j
17+
18+
return max_indx_row, max_indx_col
19+
20+
21+
rows = int(input("Enter the number of rows in the list: "))
22+
matrix = []
23+
24+
for i in range(rows):
25+
r = input("Enter a row: ").split()
26+
r = [float(x) for x in r]
27+
matrix.append(r)
28+
29+
row, col = locateLargest(matrix)
30+
print("The location of the largest element is at (" + str(row) + "," + str(col) + ")")

‎CH11/EX11.14.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 11.14 (Explore matrix) Write a program that prompts the user to enter the length of a
2+
# square matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and
3+
# finds the rows, columns, and major diagonal with all 0s or all 1s.
4+
import random
5+
6+
7+
def print_matrix(mat):
8+
for i in mat:
9+
for j in i:
10+
print(j, end='')
11+
print()
12+
13+
14+
def find_in_row(mat, x):
15+
indx = []
16+
for row in mat:
17+
if sum(row) == x * len(row):
18+
indx.append(mat.index(row))
19+
if len(indx) == 0:
20+
return None
21+
22+
return indx
23+
24+
25+
def find_in_diagonal(mat, x):
26+
indx = []
27+
for i in range(len(mat)):
28+
if mat[i][i] != x:
29+
return None
30+
31+
for i in range(len(mat)-1, 0, -1):
32+
if mat[i][i] != x:
33+
return None
34+
35+
return True
36+
37+
38+
size = int(input("Enter the size for the matrix: "))
39+
mat = []
40+
for r in range(size):
41+
mat.append([0] * size)
42+
for c in range(size):
43+
mat[r][c] = random.randint(0, 1)
44+
45+
print_matrix(mat)
46+
47+
a = find_in_row(mat, 0)
48+
b = find_in_row(mat, 1)
49+
if a is None and b is None:
50+
print("No same numbers in a row")
51+
else:
52+
if a is not None:
53+
print("All 0s on row ", end='')
54+
for i in a:
55+
print(i, end=' ')
56+
print()
57+
58+
if b is not None:
59+
print("All 1s on row ", end='')
60+
for i in b:
61+
print(i, end=' ')
62+
63+
print()
64+
65+
# transpose matrix
66+
mat_traspose = [[row[i] for row in mat] for i in range(len(mat[0]))]
67+
68+
a = find_in_row(mat_traspose, 0)
69+
b = find_in_row(mat_traspose, 1)
70+
if a is None and b is None:
71+
print("No same numbers in a column")
72+
else:
73+
if a is not None:
74+
print("All 0s on column ", end='')
75+
for i in a:
76+
print(i, end=' ')
77+
print()
78+
79+
if b is not None:
80+
print("All 1s on column ", end='')
81+
for i in b:
82+
print(i, end=' ')
83+
84+
print()
85+
86+
c = find_in_diagonal(mat, 0)
87+
d = find_in_diagonal(mat, 1)
88+
if c is None and d is None:
89+
print("No same numbers in the major diagonal")
90+
else:
91+
if c is not None:
92+
print("All 0s on the diagonal")
93+
94+
elif d is not None:
95+
print("All 1s on the diagonal")
96+
97+
print()

‎CH11/EX11.15.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 11.15 (Geometry: same line?) Exercise 6.19 gives a function for testing whether three
2+
# points are on the same line. Write the following function to test whether all the
3+
# points in the points list are on the same line:
4+
# def sameLine(points):
5+
# Write a program that prompts the user to enter five points and displays whether
6+
# they are on the same line.
7+
8+
def sameLine(points):
9+
x0 = points[0]
10+
y0 = points[1]
11+
x4 = points[-2]
12+
y4 = points[-1]
13+
for i in range(2, len(points) - 1, 2):
14+
x = points[i]
15+
y = points[i + 1]
16+
d = (x - x0) * (y4 - y0) - (x4 - x0) * (y - y0)
17+
if d != 0:
18+
return False
19+
return True
20+
21+
22+
points = input("Enter five points: ").split()
23+
points = [eval(x) for x in points]
24+
if sameLine(points):
25+
print("The five points are on the same line")
26+
else:
27+
print("The five points are not on the same line")

‎CH11/EX11.16.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 11.16 (Sort a list of points on y-coordinates) Write the following function to sort a list
2+
# of points on their y-coordinates. Each point is a list of two values for x- and ycoordinates.
3+
# # Returns a new list of points sorted on the y-coordinates
4+
# def sort(points):
5+
# For example, the points [[4, 2], [1, 7], [4, 5], [1, 2], [1, 1], [4, 1]] will be sorted
6+
# to [[1, 1], [4, 1], [1, 2], [4, 2], [4, 5], [1, 7]]. Write a test program that displays
7+
# the sorted result for points [[4, 34], [1, 7.5], [4, 8.5], [1, -4.5], [1, 4.5],
8+
# [4, 6.6]] using print(list).
9+
10+
# Returns a new list of points sorted on the y-coordinates
11+
def sort(points): # insertion sort
12+
for i in range(1, len(points)):
13+
current = points[i]
14+
j = i - 1
15+
while j >= 0 and current[1] < points[j][1]:
16+
points[j + 1] = points[j]
17+
j -= 1
18+
points[j + 1] = current
19+
20+
21+
points = [[4, 34], [1, 7.5], [4, 8.5], [1, -4.5], [1, 4.5], [4, 6.6]]
22+
23+
sort(points)
24+
print(points)

0 commit comments

Comments
(0)

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