-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day_19.py
181 lines (138 loc) · 2.42 KB
/
Day_19.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
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
#!/usr/bin/env python
# coding: utf-8
# # Question 75
#
# ### **Question**
#
# > **_Please write a program to randomly print a integer number between 7 and 15 inclusive._**
#
# ---
#
# ### Hints
#
# > **_Use random.randrange() to a random integer in a given range._**
#
# ---
#
# **Solution:**
# In[1]:
import random
print(random.randrange(7, 16))
# ---
#
# # Question 76
#
# ### **Question**
#
# > **_Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!"._**
#
# ---
#
# ### Hints
#
# > **_Use zlib.compress() and zlib.decompress() to compress and decompress a string._**
#
# ---
#
# **Solution:**
# In[2]:
import zlib
t = zlib.compress(b'hello world!hello world!hello world!hello world!')
print(t)
print(zlib.decompress(t))
# ---
#
# # Question 77
#
# ### **Question**
#
# > **_Please write a program to print the running time of execution of "1+1" for 100 times._**
#
# ---
#
# ### Hints
#
# > **_Use timeit() function to measure the running time._**
#
# ---
#
#
#
# **Solutions:**
# In[3]:
import datetime
before = datetime.datetime.now()
for i in range(100):
x = 1 + 1
after = datetime.datetime.now()
execution_time = after - before
print(execution_time.microseconds)
# **OR**
# In[4]:
import time
before = time.time()
for i in range(100):
x = 1 + 1
after = time.time()
execution_time = after - before
print(execution_time)
# ---
#
# # Question 78
#
# ### **Question**
#
# > **_Please write a program to shuffle and print the list [3,6,7,8]._**
#
# ---
#
# ### Hints
#
# > **_Use shuffle() function to shuffle a list._**
#
# ---
#
#
#
# **Solutions:**
# In[5]:
import random
lst = [3, 6, 7, 8]
random.shuffle(lst)
print(lst)
# **OR**
# In[6]:
import random
# shuffle with a chosen seed
lst = [3, 6, 7, 8]
seed = 7
random.Random(seed).shuffle(lst)
print(lst)
# ---
#
# # Question 79
#
# ### **Question**
#
# > **_Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"]._**
#
# ---
#
# ### Hints
#
# > **_Use list[index] notation to get a element from a list._**
#
# ---
#
#
#
# **Solutions:**
# In[7]:
subjects = ["I", "You"]
verbs = ["Play", "Love"]
objects = ["Hockey", "Football"]
for sub in subjects:
for verb in verbs:
for obj in objects:
print("{} {} {}".format(sub, verb, obj))
# ---