-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoding_Practice.py
More file actions
323 lines (259 loc) · 7.07 KB
/
Coding_Practice.py
File metadata and controls
323 lines (259 loc) · 7.07 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Two sum
def twoSum (nums, target ):
d = {}
for i , n in enumerate ( nums ):
if n in d : return [ d [ n ], i ]
d [ target - n ]= i
twoSum([3,2,4],6)
# Binary Flip
def bin_flip(n):
return bin(255 - int(n,2))[2:]
bin_flip('01010101')
# longest common prefix
class Solution:
def longestCommonPrefix(self, m):
if not m: return ''
#since list of string will be sorted and retrieved min max by alphebetic order
s1 = min(m)
s2 = max(m)
for i, c in enumerate(s1):
if c != s2[i]:
return s1[:i] #stop until hit the split index
return s1
# no of bits
def hammingWeight(self, n):
result = 0
while n:
n &= n - 1
result += 1
return result
# Count Largest Group
import functools
import collections
from collections import defaultdict
# method 1:
dict = {ele : [] for ele in range(1,11)}
# method 2:
dict = defaultdict(list)
for i in range(1,2+1):
k = functools.reduce(lambda x, y: x+y,list(map(int,list(str(i)))))
#dict[k].append(i)
dict[k].append(i)
len([ele for ele in dict.values() if len(ele) == len(max([ele for ele in dict.values()], key = len))])
# Check If a String Can Break Another String
s1 = 'abc'
s2 = 'xya'
all([x <= y for x,y in zip(*sorted([sorted(s1),sorted(s2)]))])
# pascal triangle
# pascal = [[1]*(i+1) for i in range(numRows)]
n = 5
res = [[1],[1,1]]
for i in range(2,n):
temp = []
temp.append(1)
for j in range(1,i):
print(i,j)
temp.append(res[i-1][j-1] + res[i-1][j])
temp.append(1)
res.append(temp)
res
row = [1]
n = 3
for _ in range(n):
row = [x + y for x,y in zip([0]+row, row+[0])]
row
# Best Time to Buy and Sell Stock
class Solution(object):
def maxProfit(self, prices):
max_profit, min_price = 0, float("inf")
for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)
return max_profit
# Shortest Unsorted Continuous Subarray
nums = [2, 6, 4, 8, 10, 15]
res = [i for (i, (a, b)) in enumerate(zip(nums, sorted(nums))) if a != b]
[0 if not res else res[-1] - res[0] + 1]
# Sort Array By Parity
class Solution:
def sortArrayByParity(self, A):
beg, end = 0, len(A) - 1
while beg <= end:
if A[beg] % 2 == 0:
beg += 1
else:
A[beg], A[end] = A[end], A[beg]
end -= 1
return A
# Binary Prefix Divisible By 5
n = [0,1,1,1,1,1]
res = []
for i in range(len(n)):
no = int(str('0b'+''.join([ele for ele in map(str,n[:i+1])])),base=0)
if no%5 == 0:
res.append(True)
else:
res.append(False)
res
def prefixesDivBy5(self, A: List[int]) -> List[bool]:
ans, b = [], 0
for a in A:
b = b << 1 | a
ans.append(b % 5 == 0)
return ans
# Sum of Digits in the Minimum Number
import functools
n = [34,23,1,24,75,33,54,8]
if functools.reduce(lambda x,y: x + y, list(map(int,str(min(n))))) % 2 == 0:
print(1)
else:
print(0)
# Decompress Run-Length Encoded List
nums = [1,2,3,4]
[x for a,b in zip(nums[0::2], nums[1::2]) for x in [b]*a]
# Running Sum of 1d Array
def runningSum(self, A):
return list(itertools.accumulate(A))
# Can Make Arithmetic Progression From Sequence
class Solution(object):
def canMakeArithmeticProgression(self, arr):
"""
:type arr: List[int]
:rtype: bool
"""
arr.sort()
k = arr[1] - arr[0]
for i in range(1, len(arr) - 1):
if arr[i+1] - arr[i] != k:
return False
return True
# Add binary
a = "11"
b = "1"
res = [0] * (max(len(a),len(b))+1)
if len(a) != len(b):
a = a.zfill(max(len(a),len(b)))
b = b.zfill(max(len(a),len(b)))
pos = len(res) - 1
for i in range(len(a)):
res[pos] = res[pos] + (int(a[~i]) + int(b[~i]))
res[pos-1] = int(res[pos]/2)
res[pos] = res[pos]%2
pos -= 1
res
# Repeated String Match
a = "abc"
b = "wxyz"
cnt = 0
temp = ''
thr = int((len(b)/len(a))+1)
while thr > 0:
temp = temp + a
cnt += 1
if b in temp:
print(cnt)
break
thr -= 1
if thr == 0:
print(-1)
# Unique Morse Code Words
words = ["gin", "zen", "gig", "msg"]
morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
alp = [chr(i) for i in range(97,97+26,1)]
dic = {alp:morse for alp,morse in zip(alp,morse)}
dic
ot = []
for w in words:
res = ''
for a in w:
res = res + dic[a]
ot.append(res)
len(set(ot))
def uniqueMorseRepresentations(self, words):
d = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
return len({''.join(d[ord(i) - ord('a')] for i in w) for w in words})
# Armstrong Number
n = 123
def armstr_number(n):
l = len(str(n))
s = 0
for i in str(n):
s = s + pow(int(i), l)
if n == s:
print(True)
else:
print(False)
armstr_number(n)
# Shallow copies
# with nested objects, modifying on level 2 or deeper does affect shallow copy
import copy
1. list_b = copy.copy(list_a)
2. list_b = list(list_a)
3. list_b = list_a[:]
4. list_b = list_a.copy()
# Newton's method root finding
def newton(f,Df,x0,epsilon,max_iter):
'''Approximate solution of f(x)=0 by Newton's method.
Parameters
----------
f : function
Function for which we are searching for a solution f(x)=0.
Df : function
Derivative of f(x).
x0 : number
Initial guess for a solution f(x)=0.
epsilon : number
Stopping criteria is abs(f(x)) < epsilon.
max_iter : integer
Maximum number of iterations of Newton's method.
Returns
-------
xn : number
Implement Newton's method: compute the linear approximation
of f(x) at xn and find x intercept by the formula
x = xn - f(xn)/Df(xn)
Continue until abs(f(xn)) < epsilon and return xn.
If Df(xn) == 0, return None. If the number of iterations
exceeds max_iter, then return None.
Examples
--------
>>> f = lambda x: x**2 - x - 1
>>> Df = lambda x: 2*x - 1
>>> newton(f,Df,1,1e-8,10)
Found solution after 5 iterations.
1.618033988749989
'''
xn = x0
for n in range(0,max_iter):
fxn = f(xn)
if abs(fxn) < epsilon:
print('Found solution after',n,'iterations.')
return xn
Dfxn = Df(xn)
if Dfxn == 0:
print('Zero derivative. No solution found.')
return None
xn = xn - fxn/Dfxn
print('Exceeded maximum iterations. No solution found.')
return None
# lake perimeter
from itertools import chain, groupby
grid = [[1,0,1],
[1,1,1]]
s = sum(list(chain(*grid))) * 4
cnt = 0
for i in zip(*grid):
print(i)
for j in range(len(i)-1):
if i[j] == i[j+1] == 1:
cnt += 1
cnt1 = 0
for i in grid:
print(i)
for j in range(len(i)-1):
if i[j] == i[j+1] == 1:
cnt1 += 1
cnt1
res = s - (cnt + cnt1)*2
res