-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
executable file
·95 lines (85 loc) · 3.1 KB
/
cube.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
"""
Author: Koen van den Berg
Description: Creates a cube of N size and finds subcubes of n size
"""
# Imports
import random
# Definitions
class Cube():
"""A cube is a NxN matrice with random 0 and 1 values
"""
def __init__(self, N):
"""Initialization function
"""
self.cube = self._createCubeData(N)
self.cubeSize = N
self.transposed_cube = self._transposeCube()
self.largestSubCube = {}
def _createCubeData(self, N):
"""Creates the cube of size N
N: int, height and width of cube
"""
cube = []
for x in range(N):
cube_row = []
for y in range(N):
randnumber = random.randint(0,1)
cube_row.append(randnumber)
cube.append(cube_row)
return(cube)
def _transposeCube(self):
"""Transposes the cube
"""
t_cube = []
for x in range(len(self.cube)):
cube_row = []
for y in range(len(self.cube)):
t_value = self.cube[y][x]
cube_row.append(t_value)
t_cube.append(cube_row)
return(t_cube)
def printCube(self):
"""Prints the cube nicely
"""
print('This is the cube\n------------------------------')
for row in self.cube:
for j in row:
print(f'{j}', end='')
print()
print('\nThis is the transposed cube\n------------------------------')
for row in self.transposed_cube:
for j in row:
print(f'{j}', end='')
print()
def findSubCube(self):
"""Finds largest subcube of 1s in the cube
"""
subCubeLoc = []
subCubeSize = 0
isSearching = False
for x in range(len(self.cube)):
for y in range(len(self.cube[x])):
if self.cube[x][y] == 1:
this_size = 1
isSearching = True
while isSearching == True:
try:
next_part = self.cube[x+this_size][y:y+this_size+1]
next_part_transpose = self.transposed_cube[y+this_size][x:x+this_size+1]
if next_part == [1 for i in range(this_size+1)] and next_part_transpose == [1 for i in range(this_size+1)]:
this_size += 1
isSearching = True
if this_size > subCubeSize:
subCubeSize = this_size
subCubeLoc = [x,y]
else:
isSearching = False
except(IndexError):
isSearching = False
self.largestSubCube["Size"] = subCubeSize
self.largestSubCube["Loc"] = subCubeLoc
if __name__=="__main__":
test = Cube(50)
test.printCube()
test.findSubCube()
print(test.largestSubCube)