Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ycchen00
GitHub Repository: ycchen00/Introduction-to-Data-Science-in-Python
Path: blob/main/quiz/quiz1.ipynb
3223 views
Kernel: Python 3

Q1

%E5%9B%BE%E7%89%87.png

import re string = 'bat, lat, mat, bet, let, met, bit, lit, mit, bot, lot, mot' result = re.findall('b[ao]t', string) print(result)
['bat', 'bot']

Q2

%E5%9B%BE%E7%89%87.png

import numpy as np a=np.zeros((20,20)) b=np.ones((20,20)) def l2_dist(a, b): result = ((a - b) * (a - b)).sum() result = result ** 0.5 return result
np.reshape(a,(20*20)).shape
(400,)
np.reshape(b,(20*20,1)).shape
(400, 1)
a=np.zeros((20,20)) b=np.ones((20,20)) l2_dist(np.reshape(a,(20*20)), np.reshape(b,(20*20,1)))
400.0
a=np.zeros((20,20)) b=np.ones((20,20)) l2_dist(np.reshape(a,(20*20)), np.reshape(b,(20*20)))
20.0
a=np.zeros((20,20)) b=np.ones((20,20)) l2_dist(a.T,b.T)
20.0
a=np.zeros((20,20)) b=np.ones((20,20)) l2_dist(a,b)
20.0

Q3

%E5%9B%BE%E7%89%87.png

a1 = np.random.rand(4) a2 = np.random.rand(4, 1) a3 = np.array([[1, 2, 3, 4]]) a4 = np.arange(1, 4, 1) a5 = np.linspace(1 ,4, 4)
a5.shape==a1.shape
True
# a4.ndim()==1 print("'int' object is not callable")
'int' object is not callable
a3.shape==a4.shape
False
a1.shape==a2.shape
False

Q4

%E5%9B%BE%E7%89%87.png

import numpy as np old = np.array([[1, 1, 1], [1, 1, 1]]) new = old new[0, :2] = 0 print(old)
[[0 0 1] [1 1 1]]

Q5

%E5%9B%BE%E7%89%87.png

import numpy as np r=np.ones((6,6)) r[2:4,2:4].shape
(2, 2)

Q6

%E5%9B%BE%E7%89%87.png

import re s = 'ACBCAC'
re.findall('^AC',s)
['AC']

Q7

%E5%9B%BE%E7%89%87.png

import re s = 'ACAABAACAAAB' result = re.findall('A{1,2}', s) L = len(result) L
5
result
['A', 'AA', 'AA', 'AA', 'A']

Q8

%E5%9B%BE%E7%89%87.png

q8='Office of Research Administration: (734) 647-6333 | 4325 North Quad\ Office of Budget and Financial Administration: (734) 647-8044 | 309 Maynard, Suite 205\ Health Informatics Program: (734) 763-2285 | 333 Maynard, Suite 500\ Office of the Dean: (734) 647-3576 | 4322 North Quad\ UMSI Engagement Center: (734) 763-1251 | 777 North University\ Faculty Adminstrative Support Staff: (734) 764-9376 | 4322 North Quad'
pattern='[(]\d{3}[)]\s\d{3}[-]\d{4}' # pattern='[(]\d{3}[)]\d{3}[-]\d{4}' # pattern='\d{3}[-]\d{3}[-]\d{4}' # pattern='\d{3}\s[-]\d{3}[-]\d{4}' re.findall(pattern, q8)
['(734) 647-6333', '(734) 647-8044', '(734) 763-2285', '(734) 647-3576', '(734) 763-1251', '(734) 764-9376']

Q9

%E5%9B%BE%E7%89%87.png

q9='I refer to https://google.com and I never refer http://www.baidu.com if I have to search anything'
pattern='(?<=[https]:\/\/)([A-Za-z0-9.]*)' # pattern='(?<=https:\/\/)([A-Za-z0-9]*)' # pattern='(?<=https:\/\/)([A-Za-z0-9.]*)' # pattern='(?<=https:\/\/)([.]*)' re.findall(pattern, q9)
['google.com', 'www.baidu.com']

Q10

%E5%9B%BE%E7%89%87.png

text=r'''Everyone has the following fundamental freedoms: (a) freedom of conscience and religion; (b) freedom of thought, belief, opinion and expression, including freedom of the press and other media of communication; (c) freedom of peaceful assembly; and (d) freedom of association. ''' import re X='\(.\)' # X='freedom' # X='(.)' # X='[a-d]' pattern = X print(len(re.findall(pattern,text)))
4