Skip to content

Commit

Permalink
insertion_sort, rotate matrix, dfs
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Mar 22, 2021
1 parent df005a9 commit 4afd76c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
43 changes: 32 additions & 11 deletions Coding Test/dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@
}

discovered = []


def recursive_dfs_global(v):
global discovered
discovered.append(v)

for next in graph[v]:
if next not in discovered:
discovered = recursive_dfs_global(next)

return discovered

def recursive_dfs_local(v, discovered = []):

def recursive_dfs_local(v, discovered=[]):
discovered.append(v)

for next in graph[v]:
if next not in discovered:
discovered = recursive_dfs_local(next, discovered)

return discovered


def stack_dfs(start):
stack = [start]
discovered = []
Expand All @@ -37,14 +41,31 @@ def stack_dfs(start):
discovered.append(v)
for next in graph[v]:
stack.append(next)

return discovered



print(recursive_dfs_local(1))
print(stack_dfs(1))

# print(recursive_dfs_local(1))
# print(stack_dfs(1))


# [1, 2, 5, 6, 7, 3, 4]
# [1, 4, 3, 5, 7, 6, 2]
# [1, 4, 3, 5, 7, 6, 2]


def stack_dfss(v):
stack = [v]
discovered = []

while stack:
top = stack.pop()

if top not in discovered:
discovered.append(top)
for next in graph[top]:
stack.append(next)

return discovered


print(stack_dfss(1))
24 changes: 24 additions & 0 deletions Coding Test/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import re
import collections
import itertools
import heapq
import bisect
import sys
import functools

A = [5,4,3,2,1]

def insertion_sort(arr):
i = 1
while i < len(arr):
j = i
while j > 0 and arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]
j -= 1
i += 1



insertion_sort(A)

print(A)
17 changes: 17 additions & 0 deletions rotate_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import copy
matrix = [[1,2,3], [4,5,6],[7,8,9]]

col = len(matrix[0])
new_matrix = [[0] * col for x in matrix]

for i in range(len(matrix)):
for j in range(len(matrix[0])):
new_matrix[i][j] = matrix[col-j-1][i]
matrix = new_matrix[:]

for i in range(len(matrix)):
for j in range(len(matrix[0])):
new_matrix[i][j] = matrix[col-j-1][i]


print(new_matrix)

0 comments on commit 4afd76c

Please sign in to comment.