-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDay_17.py
200 lines (161 loc) · 2.98 KB
/
Day_17.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# coding: utf-8
# # Question 65
#
# ### **Question**
#
# > **_Please write assert statements to verify that every number in the list [2,4,6,8] is even._**
#
# ---
#
# ### Hints
#
# > **_Use "assert expression" to make assertion._**
#
# ---
#
#
#
# **Solutions:**
# In[1]:
data = [2, 4, 5, 6]
for i in data:
assert i % 2 == 0, "{} is not an even number".format(i)
# ---
#
# # Question 66
#
# ### **Question**
#
# > **_Please write a program which accepts basic mathematic expression from console and print the evaluation result._**
#
# > **_Example:
# > If the following n is given as input to the program:_**
#
#
# > 35 + 3
#
#
# > **_Then, the output of the program should be:_**
#
#
# > 38
#
#
# ---
#
# ### Hints
#
# > **_Use eval() to evaluate an expression._**
#
# ---
#
#
#
# **Solutions:**
# In[ ]:
expression = input()
ans = eval(expression)
print(ans)
# ---
#
# # Question 67
#
# ### **Question**
#
# > **_Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list._**
#
# ---
#
# ### Hints
#
# > **_Use if/elif to deal with conditions._**
#
# ---
#
# **Solutions:**
#
# ### Solution by AasaiAlangaram
# In[ ]:
def binary_search_Ascending(array, target):
lower = 0
upper = len(array)
print("Array Length:", upper)
while lower < upper:
x = (lower + upper) // 2
print("Middle Value:", x)
value = array[x]
if target == value:
return x
elif target > value:
lower = x
elif target < value:
upper = x
Array = [1, 5, 8, 10, 12, 13, 55, 66, 73, 78, 82, 85, 88, 99]
print("The Value Found at Index:", binary_search_Ascending(Array, 82))
# ---
# **OR**
#
# ### Solution by yuan1z: Python
# In[ ]:
idx = 0
def bs(num, num_list):
global idx
if len(num_list) == 1:
if num_list[0] == num:
return idx
else:
return "No exit in the list"
elif num in num_list[: len(num_list) // 2]:
return bs(num, num_list[: len(num_list) // 2])
else:
idx += len(num_list) // 2
return bs(num, num_list[len(num_list) // 2 :])
print(bs(66, [1, 5, 8, 10, 12, 13, 55, 66, 73, 78, 82, 85, 88, 99, 100]))
# ---
#
# # Question 68
#
# ### **Question**
#
# > **_Please generate a random float where the value is between 10 and 100 using Python module._**
#
# ---
#
# ### Hints
#
# > **_Use random.random() to generate a random float in [0,1]._**
#
# ---
#
#
#
# **Solutions:**
# In[ ]:
import random
rand_num = random.uniform(10, 100)
print(rand_num)
# ---
#
# # Question 69
#
# ### **Question**
#
# > **_Please generate a random float where the value is between 5 and 95 using Python module._**
#
# ---
#
# ### Hints
#
# > **_Use random.random() to generate a random float in [0,1]._**
#
# ---
#
#
#
# **Solutions:**
# In[ ]:
import random
rand_num = random.uniform(5, 95)
print(rand_num)
# ---