WorksheetsQuiz 1 coursera python
Total questions: 10
Worksheet time: 5mins
What will be the output of the following code?
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'
['bat', 'bot']
'bat, bet, bit, bot'
['bat', 'bet', 'bit', 'bot']
Assume a and b are two (20, 20) numpy arrays. The L2-distance (defined above) between two equal dimension arrays can be calculated in python as follows:
def l2_dist(a, b):
result = ((a - b) * (a - b)).sum()
result = result ** 0.5
return result
l2_dist(a.T, b.T)
l2_dist(np.reshape(a, (20 20)), np.reshape(b, (20 20, 1)))
l2_dist(np.reshape(a, (20 20)), np.reshape(b, (20 20)))
l2_dist(a, b)
Consider the following variables in Python:
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)
Which of the following statements regarding these variables is correct?
a5. shape == a1. shape
a4. ndim() == 1
a3. shape == a4. shape
al. shape == a2. shape
Which of the following is the correct output for the code given below?
import numpy as np
old = np. array([[1, 1, 1], [1, 1, 1]])
new = old
new[0, : 2] = 0
print (old)
[[1 1 1] [1 1 1]]
[[0 0 1] [1 1 1]]
[[1 1 0][1 1 0]]
[[0 1 1][0 1 1]]
Given the 6x6 NumPy array r shown below, which of the following options would slice the shaded elements?
r[[2, 3], [2, 3]]
r [2: 4, 2: 4]
r[[2, 4], [2, 4]]
r[2: 3, 2: 3]
import re
s = ACBСAC"
For the given string, which of the following regular expressions can be used to check if the string starts with 'AC'?
re. findall(' [^A]C', s)
re. findall(' ^AC', s)
re. findall(' AC', s)
re.findall('^[AC]', s)
What will be the output of the variable L after the following code is executed?
import re
s = 'ACAABAACAAAB'
result = re.findall('A{1,2}', s)
L = len(result)
8
12
5
4
Which of the following is the correct regular expression to extract all the phone numbers from the following chunk of
text:
'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 Administrative Support Staff: (734) 764-9376 | 4322 North Quad'
[(]\d{3}[)]\s\d{3}[-]\d{4}
\d{3}[-]\d{3}[-]\d{4}
\d{3}\s\d{3}[-]\d{4}
[(]\d{3}[)]\d{3}[-]\d{4}
Which of the following regular expressions can be used to get the domain names (e.g. google.com, www.baidu.com) from the following sentence?
'I refer to https://google.com and I never refer http://www.baidu.com if I have to search anything'
(?<=https:\/\/)([.]*)
(?<=https:\/\/)([A-Za-z0-9.]*)
(?<=https:\/\/)([A-Za-z0-9]*)
(?<=[https]:\/\/)([A-Za-z0-9.]*)
The text from the Canadian Charter of Rights and Freedoms section 2 lists the fundamental freedoms afforded to everyone. Of the four choices provided to replace X in the code below, which would accurately count the number of fundamental freedoms that Canadians have?
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
pattern = X
print(len(re.findall(pattern,text)))
'freedom'
\(.\)
'[a-d]'
(.)
