-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmapHoles.py
66 lines (55 loc) · 1.73 KB
/
bitmapHoles.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
# Bitmap_Holes --> CODERBYTE
'''
Have the function BitmapHoles(strArr) take the array of strings stored in strArr,
which will be a 2D matrix of 0 and 1's, and determine how many holes,
or contiguous regions of 0's, exist in the matrix.
A contiguous region is one where there is a connected group of 0's going in
one or more of four directions: up, down, left, or right.
For example: if strArr is ["10111", "10101", "11101", "11111"],
then this looks like the following matrix:
1 0 1 1 1
1 0 1 0 1
1 1 1 0 1
1 1 1 1 1
For the input above, your program should return 2 because there are
two separate contiguous regions of 0's, which create "holes" in the matrix.
You can assume the input will not be empty.
Example1:
Input: ["01111", "01101", "00011", "11110"]
Output: 3
Example2
Input: ["1011", "0010"]
Output: 2
'''
count = 0
def BitmapHoles(strArr):
newList = []
def horizontalCount(ourList):
global count
for each in ourList:
z = list(each)
index = 0
while index < len(z)-1:
if z[index] == '0' and z[index+1] == '0':
count += 1
break
index +=1
def transpose():
newStr = ""
i = 0
while i < len(strArr[0]):
for each in strArr:
newStr += each[i]
# print(newStr)
newList.append(newStr)
newStr = ""
i += 1
# print(f"Sample1: {strArr}")
horizontalCount(strArr)
# print(f"Initial count: {count}")
transpose()
# print(f"Transposed Sample: {newList}")
horizontalCount(newList)
# print(f"Final count: {count}")
return count
print(BitmapHoles(["01111", "01101", "00011", "11110"]))