-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
553 lines (399 loc) · 12.7 KB
/
test.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# from ast import List
# from typing import Counter
# name = {'name': 'jake'}
# name.values()
# # ["un","iq","ue"]
# def maxLength(arr):
# result = 0
# def checkDuplicate(a, b):
# freq = [0] * 26
# for char in b:
# idx = ord(char) - ord('a')
# if (freq[idx] > 0): return True
# freq[idx] += 1;
# for char in a:
# if freq[ord(char) - ord('a')] > 0: return True
# return False
# def dfs(word, leftArray, freq):
# for leftWord in leftArray:
# leftWordFreq = Counter(leftWord)
# #
# # for key, val in leftWordFreq.items():
# # if val == 1 and key in freq:
# # for idx in range(len(arr)):
# from heapq import heapify, heappush, heappop
# def testFunction():
# result = []
# def dfs(target, numBank):
# if target < 0:
# return None
# if target == 0:
# return []
# for num in numBank:
# newTarget = target - num
# targetCombi = dfs(newTarget, numBank)
# print(targetCombi)
# if targetCombi != None:
# fullCombination = [targetCombi, num]
# result.append(fullCombination)
# print(dfs(7, [2, 3, 6, 7]))
# print(result)
# testFunction()
# from collections import defaultdict
# from typing import List
# class Solution:
# def wordBreak(self, s: str, wordDict: List[str]) -> bool:
# memo = defaultdict(bool)
# def dfs(word, wordDict):
# if word == '': return True
# if(word in memo): return memo[word]
# for w in wordDict:
# wLen = len(w)
# if word[:wLen] == w:
# canConstructRes = dfs(word[wLen:], wordDict)
# if canConstructRes == True:
# memo[word] = True
# return True
# memo[word] = False
# return False
# return dfs(s, wordDict)
# obj = Solution()
# print(obj.wordBreak('leetcode', ["leet","code"] ))
# s = "leetcode", wordDict = ["leet","code"]
# nums = [1, 1, 2, 4, 9]
# heapify(nums)
# heappush(nums, 0)
# print(heappop(nums), heappop(nums))
# --------------------------------------------------------------------------------------------------
# Learning Graph DSA
# -------------------------------------------------
# from collections import UserString, deque
# graph = {
# 'a': ['b', 'c'],
# 'b': ['d'],
# 'c': ['e'],
# 'd': ['f'],
# 'e': [],
# 'f': []
# }
# def dfs(graph, source):
# print(source)
# for n in graph[source]:
# dfs(graph, n)
# def bfs(graph, source):
# queue = deque([source])
# while queue:
# currVal = queue.pop()
# print(currVal)
# for n in graph[currVal]:
# queue.append(n)
# dfs(graph, 'a')
# ------------------------------------------------------------------------------------------------------
# Problem - 1
# graph = {
# 'f': ['g', 'i'],
# 'g': ['h'],
# 'h': [],
# 'i': ['g', 'k'],
# 'j': ['i'],
# 'k': []
# }
# def hasPath(graph, start, end):
# stack = [start]
# while stack:
# currNode = stack.pop()
# print(currNode)
# for node in graph[currNode]:
# if node == end:
# return True
# stack.append(node)
# return False
# print(hasPath(graph, 'f', 'j'))
# ----------------------------------------------------------------------------------------------
# Problem - 2 - undirected path with cycle's
# from collections import defaultdict
# edgesList = [
# ['i', 'j'],
# ['k', 'i'],
# ['m', 'k'],
# ['k', 'l'],
# ['o', 'n'],
# ]
# def findPath(graph, start, end):
# adj = defaultdict(list)
# for edge in graph:
# fromNode = edge[0]
# toNode = edge[1]
# adj[fromNode].append(toNode)
# adj[toNode].append(fromNode)
# visited = set()
# # dfs
# def dfs(graph, src, dst, visited):
# if src == dst:
# return True
# if src in visited:
# return False
# visited.add(src)
# for nei in graph[src]:
# if dfs(graph, nei, dst, visited) == True:
# return True
# return False
# return dfs(adj, start, end, visited)
# # stack = [start]
# # seen = set()
# # while stack:
# # curr = stack.pop()
# # if curr == end:
# # return True
# # for edge in adj[curr]:
# # if edge not in seen:
# # stack.append(edge)
# # seen.add(edge)
# # return False
# print(findPath(edgesList, 'n', 'k'))
# --------------------------------------------------------------------------------------------------
# Problem - 3 - connected components count
# from collections import defaultdict
# edgesList = [
# ['i', 'j'],
# ['k', 'i'],
# ['m', 'k'],
# ['k', 'l'],
# ['o', 'n'],
# ['z', 'p'],
# ]
# ================== USING NO RETURN FROM DFS FUNCTION ======================
# def connectedComp(graph):
# adj = defaultdict(list)
# res = 0
# visited = set()
# # fill adj list
# for edge in graph:
# adj[edge[0]].append(edge[1])
# adj[edge[1]].append(edge[0])
# # traverse throw the graph stating on every unvisited node
# def graphDFS(graph, currNode, visited):
# if currNode in visited: return
# visited.add(currNode)
# for neg in graph[currNode]:
# graphDFS(graph, neg, visited)
# # iterate throw all edges and find components
# for edge in adj.keys():
# if not edge in visited:
# res += 1
# graphDFS(adj, edge, visited)
# return res
# ======================= USING RETURN THROW DFS FUNCTION ===================================
# def connectedComp(graph):
# adj = defaultdict(list)
# res = 0
# visited = set()
# # fill adj list
# for edge in graph:
# adj[edge[0]].append(edge[1])
# adj[edge[1]].append(edge[0])
# # try to traverse throw the graph stating on every node
# def graphDFS(graph, currNode, visited):
# if currNode in visited:
# return False
# visited.add(currNode)
# for neg in graph[currNode]:
# graphDFS(graph, neg, visited)
# return True
# # iterate throw all edges and find components
# for edge in adj.keys():
# if graphDFS(adj, edge, visited) == True:
# res += 1
# return res
# print(connectedComp(edgesList))
# --------------------------------------------------------------------------------------------------
# Problem - 4 - largest components
# from collections import defaultdict
# edgesList = [
# ['i', 'j'],
# ['k', 'i'],
# ['m', 'k'],
# ['k', 'l'],
# ['o', 'n'],
# ['z', 'p'],
# ]
# def largestComponent(edgesList):
# adj = defaultdict(list)
# for edge in edgesList:
# adj[edge[0]].append(edge[1])
# adj[edge[1]].append(edge[0])
# def dfs(graph, currNode, visited):
# if currNode in visited:
# return 0
# visited.add(currNode)
# count = 1
# for nei in graph[currNode]:
# count += dfs(graph, nei, visited)
# return count
# visited = set()
# res = -1
# for edge in adj.keys():
# if not edge in visited:
# res = max(dfs(adj, edge, visited), res)
# return res
# print(largestComponent(edgesList))
# --------------------------------------------------------------------------------------------------
# Problem - 5 - shortest path
from collections import defaultdict, deque
import heapq
# import heapq
# edgesList = [
# ['i', 'j'],
# ['k', 'i'],
# ['m', 'k'],
# ['k', 'l'],
# ['o', 'n'],
# ['z', 'p'],
# ]
# def findShortestPath(graph, start, end):
# adj = defaultdict(list)
# for node1, node2 in graph:
# adj[node1].append(node2)
# adj[node2].append(node1)
# # def bfs():
# # pass
# queue = deque([[start, 0]])
# visited = set()
# while queue:
# currNode, distance = queue.popleft()
# if currNode == end:
# return distance
# if not currNode in visited:
# visited.add(currNode)
# for nei in adj[currNode]:
# queue.append([nei, distance + 1])
# return -1
# print(findShortestPath(edgesList, 'i', 'n'))
# # --------------------------------------------------------------------------------------------------
# # Problem - 6 - Island Count
# graphMatrix = [
# [0, 1, 0, 0, 1, 0],
# [1, 1, 0, 0, 1, 0],
# [0, 1, 0, 0, 0, 0],
# [0, 0, 0, 1, 1, 0],
# [0, 1, 0, 1, 1, 0],
# [0, 0, 0, 0, 0, 1],
# ]
# # [-, 1, -, -, 1, -],
# # [1, 1, -, -, 1, -],
# # [-, 1, -, -, -, -],
# # [-, -, -, 1, 1, -],
# # [-, 1, -, 1, 1, -],
# # [-, -, -, -, -, -],
# def islandCount():
# row, col = len(graphMatrix), len(graphMatrix[0])
# visited = set()
# res = 0
# def exploreIsland(r, c, visited):
# # if row or col go outside of matrix then return
# if not 0 <= r < row or not 0 <= c < col or graphMatrix[r][c] == 0:
# return
# if (r, c) in visited:
# return
# visited.add((r, c))
# exploreIsland(r-1, c, visited) # UP
# exploreIsland(r+1, c, visited) # DOWN
# exploreIsland(r, c-1, visited) # LEFT
# exploreIsland(r, c+1, visited) # RIGHT
# for r in range(row):
# for c in range(col):
# currNode = graphMatrix[r][c]
# if currNode == 1 and not (r, c) in visited:
# exploreIsland(r, c, visited)
# res += 1
# print(visited)
# return res
# print(islandCount())
# --------------------------------------------------------------------------------------------------
# Problem - 7 - Min Island
# graphMatrix = [
# [0, 1, 0, 0, 1, 0],
# [1, 1, 0, 0, 1, 0],
# [0, 1, 0, 0, 0, 0],
# [0, 0, 0, 1, 1, 0],
# [0, 0, 0, 1, 1, 0],
# [0, 0, 0, 0, 0, 1],
# ]
# # [-, 1, -, -, 1, -],
# # [1, 1, -, -, 1, -],
# # [-, 1, -, -, -, -],
# # [-, -, -, 1, 1, -],
# # [-, 1, -, 1, 1, -],
# # [-, -, -, -, -, -],
# def islandCount():
# row, col = len(graphMatrix), len(graphMatrix[0])
# visited = set()
# res = float('inf')
# def exploreIsland(r, c, visited, count):
# # if row or col go outside of matrix then return
# if not 0 <= r < row or not 0 <= c < col or graphMatrix[r][c] == 0:
# return 0
# if (r, c) in visited:
# return 0
# visited.add((r, c))
# res = 1
# res += exploreIsland(r-1, c, visited, count) # UP
# res += exploreIsland(r+1, c, visited, count) # UP
# res += exploreIsland(r, c-1, visited, count) # UP
# res += exploreIsland(r, c+1, visited, count) # UP
# # exploreIsland(r+1, c, visited) # DOWN
# # exploreIsland(r, c-1, visited) # LEFT
# # exploreIsland(r, c+1, visited) # RIGHT
# return res
# for r in range(row):
# for c in range(col):
# currNode = graphMatrix[r][c]
# if currNode == 1 and not (r, c) in visited:
# res = min(exploreIsland(r, c, visited, 1), res)
# print(visited)
# return res
# print(islandCount())
# #
# #
# graph = [['O', 'O', 'O', 'O', 'O'],
# ['O', 'X', 'O', 'O', 'O'],
# ['O', 'X', 'X', 'O', 'O'],
# ['O', 'X', 'C', 'O', 'O'],
# ['O', 'X', 'X', 'O', 'O'],
# ['C', 'O', 'O', 'O', 'O']]
# def minSwaps(nums) -> int:
# freqOfOne = sum(nums)
# oneCount = nums[0]
# end = 0
# res = float('inf')
# for start in range(len(nums)):
# if start != 0:
# oneCount -= nums[start-1]
# while end - start + 1 < freqOfOne:
# end += 1
# oneCount += nums[end % len(nums)]
# res = min(res, freqOfOne - oneCount)
# return res
# print(minSwaps([0, 1, 1, 1, 0, 0, 1, 1, 0]))
# ---------------------------------------------- HEAP Test ----------------------------------
# heap = [2, 5, 8, 6, -7, 0]
# heap = [(-1*n) for n in heap]
# heapq.heapify(heap)
# k = 2
# while k:
# print(-1 * heapq.heappop(heap))
# # print(heapq.heappush(heap, 1))
# k -= 1
# print(heap)
# ---------------------------------- build all combinations -----------------------
nums = [10, 1, 2, 7, 6, 1, 5]
res = []
target = 8
for i in range(len(nums)):
for j in range(len(nums)):
curr = nums[i:j+1]
if sum(curr) == target:
res.append(curr)
print(curr)
print(res)
# print(curr if len(curr) > 0 else 0)