-
Notifications
You must be signed in to change notification settings - Fork 0
/
2DArray.py
163 lines (117 loc) · 3.65 KB
/
2DArray.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
# 2d array written by sequence
# ex :
# [ [1,2,3 4 5], incrementing x axis until end of array. then you increment y axis til end of array
# [4,5,6 4 5]. then you decrement x axis. Then you decrement y until you stop at 1
# [7,8,9 4 5]
# ]
# height and width
# while row/col > 0
import math
def spiralArr(matrix):
traversedArr = []
counterX = 0
counterY = 0
length = len(matrix[0]) - 1
counterLeft = length
while counterX <= length:
traversedArr.append(matrix[0][counterX])
counterX += 1
while counterY <= length:
traversedArr.append(matrix[counterY][length])
counterY += 1
while counterLeft >= 0:
print(matrix[length-1])
traversedArr.append(matrix[length][counterLeft])
counterLeft -= 1
print(traversedArr)
arr1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
# 0,0 to 0 2, then 0,1 to 2, 2 then 2,2 to 2,0, then 2,0 to 0,0
coord = [0, 0], [0, 1], [0, 2]
[1, 0], [1, 1], [1, 2]
[2, 0], [2, 1], [2, 2]
# matrix[x][y]
# if y == 0:
# y+= 1
# spiralArr(matrix, x, y)
spiralArr(arr1)
def isValidSudoku(self, board):
return (self.is_row_valid(board) and
self.is_col_valid(board) and
self.is_square_valid(board))
def is_row_valid(self, board):
for row in board:
if not self.is_unit_valid(row):
return False
return True
def is_col_valid(self, board):
for col in zip(*board):
if not self.is_unit_valid(col):
return False
return True
# used to calculate the sub grids to ensure theyre unique.
def is_square_valid(self, board):
for i in (0, 3, 6): # iterates each element 0 to 3 to 6
for j in (0, 3, 6): # iterates each element 0 to 3 to 6
square = [board[x][y]
for x in range(i, i + 3) for y in range(j, j + 3)]
# this is equal to the above,
# square = for x in range(i,i +3):
# for y in range(j,j+3):
# square = board[x][y]
#
if not self.is_unit_valid(square):
return False
return True
def is_unit_valid(self, unit):
unit = [i for i in unit if i != '.']
return len(set(unit)) == len(unit)
# [-14, 10, 2 , 108, 243 , 285 ,285, 285 ,401]
# if given key 285 should return first found idx.
# should return -1
# method --> sorted arr, with a key
# pivot = len(arr) / 2 floored
def findKey(arr, target, side=[]):
pivotidx = math.floor(len(arr) / 2)
leftSide = side[0:pivot]
rightSide = side[pivot:len(arr)]
# while loop would iterate k times. (Depending on amount of duplicates)
if arr[pivot] == target:
i = 0
while arr[pivot] == arr[pivot + i]:
i += 1
return (pivot + i)
if arr[pivot] > target:
findKey(arr, target, leftSide)
if arr[pivot] < target:
findKey(arr, target, rightSide)
def findKey(arr, target, leftSide=0, rightSide=-1):
pivotIdx = math.floor(len(arr[leftSide:rightSide]) / 2)
if arr[pivot] > target:
rightSide = pivot
findKey(arr, target, leftSide, rightSide)
if arr[pivot] < target:
leftSide = pivot
findKey(arr, target, leftSide, rightSide)
# Given two sparse matrices A and B, return the result of AB.
# You may assume that A's column number is equal to B's row number.
# Example:
# Input:
A = [
[1, 0, 0],
[-1, 0, 3]
]
B = [
[7, 0, 0],
[0, 0, 0],
[0, 0, 1]
]
# Output:
# | 1 0 0 | | 7 0 0 | | 7 0 0 |
# AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
# | 0 0 1 |
def calcMatrix(a, b):
result = [[0 for i in range(len(b))] for i in range(len(a))]
print(result)