-
Notifications
You must be signed in to change notification settings - Fork 12
/
Python15_Numpy
281 lines (228 loc) · 6.87 KB
/
Python15_Numpy
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
## Python
## Numpy
## Arrays
import numpy
# # Option 1:
# def arrays(arr):
# return(numpy.array(arr[::-1], float))
# Option 2:
def arrays(arr):
arr.reverse()
arr_new = numpy.array(arr, float)
return arr_new
arr = input().strip().split(' ')
result = arrays(arr)
print(result)
## Shape and Reshape
# # Option 1
# import numpy as np
# print(np.array(input().split(),int).reshape(3,3))
# # Option 2
# import numpy as np
# arr = np.array(list(map(int, input().split())))
# arr.shape = (3,3)
# print(arr)
# Option 3
import numpy as np
arr = np.array(list(map(int, input().split())))
arr = np.array(arr).reshape(3,3)
print(arr)
## Transpose and Flatten
# # Option 1
# import numpy as np
# row, col = map(int, input().split())
# arr = np.array([input().split() for _ in range(row)], int)
# print (arr.transpose())
# print (arr.flatten())
# Option 2
import numpy as np
row, col = map(int, input().split())
arr = []
for _ in range(row):
inp = [input().split()]
arr.extend(inp)
arr = np.array(arr, int)
print (arr.transpose())
print (arr.flatten())
## Concatenate
# Option 1
import numpy as np
n, m, p = map(int,input().split())
arr1 = np.array([input().split() for _ in range(n)], int)
arr2 = np.array([input().split() for _ in range(m)], int)
print(np.concatenate( (arr1, arr2), axis=0) )
# # Option 2
# import numpy as np
# n, m, p = map(int, input().split())
# arr = np.array([input().split() for _ in range(n+m)], int)
# print(arr)
## Zeros and Ones
import numpy as np
nums = tuple(map(int, input().split()))
print (np.zeros(nums, dtype = np.int))
print (np.ones(nums, dtype = np.int))
## Eye and Identity
# # Option 1
# import numpy as np
# print(str(np.eye(*map(int,input().split()))).replace('1',' 1').replace('0',' 0'))
"""
use replace() function because there's a bug with the test cases, e.g. an error on the part of the problem creator, where there's too much whitespace in the expected output; in expected output every element is shifted one unit right
"""
# # Option 2
# import numpy as np
# np.set_printoptions(legacy='1.13')
# print(np.eye(*map(int,input().split())))
# Option 3
import numpy as np
np.set_printoptions(legacy='1.13')
n, m = map(int,input().split())
print(np.eye(n,m))
## Array Mathematics
# # Option 1
# import numpy as np
# n,m = map(int, input().split())
# a,b = (np.array([input().split() for _ in range(n)], dtype=int) for _ in range(2))
# print(a+b, a-b, a*b, a//b, a%b, a**b, sep='\n')
# Option 2
import numpy as np
n,m = map(int,input().split())
a = np.array([input().split() for _ in range(n)], dtype=int)
b = np.array([input().split() for _ in range(n)], dtype=int)
print(a+b)
print(a-b)
print(a*b)
print(a//b)
print(a%b)
print(a**b)
# # Option 3
# import numpy as np
# n,m = map(int,input().split())
# lst1=[]
# lst2=[]
# for _ in range(n):
# lst=list(map(int, input().split()))
# lst1.append(lst)
# a = np.array(lst1)
# for _ in range(n):
# lst=list(map(int, input().split()))
# lst2.append(lst)
# b = np.array(lst2)
# print(np.add(a,b))
# print(np.subtract(a, b))
# print(np.multiply(a, b))
# print(np.floor_divide(a, b))
# print(np.mod(a, b))
# print(np.power(a, b))
## Floor, Ceil and Rint
import numpy as np
np.set_printoptions(sign=' ') # output offset with a space
arr = np.array(input().split(), float)
print(np.floor(arr))
print(np.ceil(arr))
print(np.rint(arr))
## Sum and Prod
# Option 1
import numpy as np
N, M = map(int, input().split())
arr = np.array([input().split() for _ in range(N)], int)
total = np.sum(arr, axis=0)
print(np.prod(total, axis=0))
# # Option 2
# import numpy as np
# N, M = map(int, input().split())
# lis = [np.array(input().split(), int) for _ in range(N)]
# print(np.prod(np.sum(lis, axis = 0)))
"""
range(N): [0, 1, 2, .... N-1]
for x in range(N): loop x = 0, x = 1, ..., x = N-1
for _ in range(N): loop N times
input().split(): Split input by spaces and prepare list (fr ex 1 2 3 becomes ['1', '2', '3'])
input().split() for _ in range(N): Read n lines and prepare n lists
[input().split() for _ in range(N)]: Prepare a list from n lines input containing N times where each item itself is list of strings
numpy.array([input().split() for _ in range(N)],int): Prepare 2d array from n lines of input with each item of type int
A = numpy.array([input().split() for _ in range(N)],int): Assign the result to A
"""
## Min and Max
# # Option 1
# import numpy as np
# print(np.max(np.min(np.array([input().split() for _ in range(int(input().split()[0]))],int),axis=1)))
# Option 2
import numpy as np
# n, m = map(int, input().split())
n = int(input().split()[0])
arr = np.array([input().split() for _ in range(n)], int)
print(np.max(np.min(arr, axis=1)))
## Mean, Var, and Std
import numpy as np
np.set_printoptions(legacy='1.13')
# n,m = map(int, input().split())
n = int(input().split()[0])
arr = np.array([input().split() for _ in range(n)], int)
print(np.mean(arr, axis=1))
print(np.var(arr, axis=0))
print(np.std(arr, axis=None))
## Dot and Cross
# Option 1
import numpy as np
n = int(input())
A=np.array([list(map(int,input().split())) for _ in range(n)])
B=np.array([list(map(int,input().split())) for _ in range(n)])
print( np.dot(A,B) )
# # Option 2
# import numpy as np
# n = int(input())
# A,B = [np.array([input().split() for _ in range(n)], int) for _ in range(2)]
# print(np.dot(A,B))
## Inner and Outer
# # Option 1
# import numpy as np
# A,B = [np.array([input().split()],int) for _ in range(2)]
# print(np.inner(A,B)[0][0], np.outer(A,B), sep='\n')
# # Option 2
# import numpy as np
# A,B = [np.array(input().split(), int) for _ in range(2)]
# print(np.inner(A,B), np.outer(A,B), sep='\n')
# Option 3
import numpy as np
A=np.array(input().split(),int)
B=np.array(input().split(),int)
print(np.inner(A,B))
print(np.outer(A,B))
## Polynomials
"""
You are given the coefficients of a polynomial Y = a x^n + b x^(n-1) + … + k.
Your task is to find the value of Y at point x.
"""
# # Option 1
# print(__import__('numpy').polyval(list(map(float,input().split())),float(input())))
# Option 2
import numpy as np
coeff = list(map(float,input().split()))
x = int(input())
print( np.polyval(coeff,x) )
# # Option 3
# import numpy as np
# if __name__ == '__main__':
# coefficients = np.array(list(map(float, input().split())))
# poly = np.poly1d(coefficients)
# print(poly(int(input())))
## Linear Algebra
## output needs to be rounded to 2 decimal points
# # Option 1
# import numpy as np
# print(round(np.linalg.det(np.array([input().split() for _ in range(int(input()))],float)),2))
# Option 2
import numpy as np
n=int(input())
a=np.array([input().split() for _ in range(n)],float)
np.set_printoptions(legacy='1.13') #version issues in this exercise
print(np.linalg.det(a))
# # Option 3
# import numpy as np # importing numpy package
# n = int(input()) # nxn array.
# a = [] # init array
# for i in range(n):
# c = list(map(float, input().split()))
# a.append(c)
# print(round(np.linalg.det(a),2)) # determinant round to 2 places
## end ##