-
Notifications
You must be signed in to change notification settings - Fork 0
/
konlo_home_chap6_A.py
130 lines (92 loc) · 2.94 KB
/
konlo_home_chap6_A.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def f2(n):
nPrintValue = 1
for PrintCount in range(n):
for i in range(0, PrintCount+1):
print(nPrintValue, end = " ")
nPrintValue += 1
print()
#f2(3)
#f2(0)
#f2(5)
def f4(n):
nPrintValue = 1
for PrintCount in range(n):
for i in range(0, PrintCount+1):
print(nPrintValue, end = " ")
nPrintValue += 1
print()
for PrintCount in range(n-1,0,-1):
for i in range(0, PrintCount):
print(nPrintValue, end = " ")
nPrintValue += 1
print()
#f4(3)
def f6(maxtrix):
for i in range(len(maxtrix[0])):
print(maxtrix[i][i])
#f6([[1,0],[0,1]])
#f6([[1,2,3],[4,5,6],[7,8,9]])
#f6([[1]])
def f8(matrix):
sum = 0
for row in matrix:
for value in row:
sum += value
return sum
#print(f8([[1,0],[0,1]]))
#print(f8([[1,2,3],[4,5,6]]))
#print(f8([[1],[2],[3],[4]]))
def f10(matrix):
for row in matrix:
for value in row:
if value % 2 == 1:
print(value, end = " ")
print()
#f10([[1,0],[0,1]])
#f10([[1,2,3],[4,5,6]])
#f10([[1],[2],[3],[4]])
def f12(matrix1, matrix2):
result_row_len = len(matrix1)
result_column_len = len(matrix2[0])
sum_len = len(matrix2)
resultValue = []
for i in range(result_row_len):
rows = []
for j in range(result_column_len):
rows.append(0)
resultValue.append(rows)
#print("{0} x {1}, {2} ".format(result_row_len,result_column_len, sum_len))
for i in range(result_row_len):
for j in range(result_column_len):
total_sum = 0
for k in range(sum_len):
# column 수만큼 합이 발생함
sum = matrix1[i][k] * matrix2[k][j]
total_sum += sum
resultValue[i][j] = total_sum
print(resultValue)
f12([[1,0],[0,1]],[[1,0],[0,1]])
f12([[1,2,3],[4,5,6]],[[-1,-1],[-1,-1],[-1,-1]])
f12([[4,3,2,1]],[[1],[2],[3],[4]])
def f14(rows, cols):
returnValue = []
for row in range(rows):
adjact_value = []
for col in range(cols):
adjacent_count = 0
#check above
if row - 1 >= 0 :
adjacent_count += 1
#check below
if row + 1 < rows :
adjacent_count += 1
#check left
if col - 1 >= 0:
adjacent_count += 1
#check right
if col + 1 < cols :
adjacent_count += 1
adjact_value.append(adjacent_count)
returnValue.append(adjact_value)
return returnValue
#print(f14(3,3))