def string_segment_test(s, l_words):
for i in range(1, len(s)+ 1):
first = s[0:i]
if first in dictionary:
return True
else:
second = s[i:]
if second in dictionary or string_segment_test(second, dictionary):
return True
return False
s = "hellonow"
dictionary= set(["hello","hell","on","now"])
if string_segment_test(s, dictionary):
print("String Can be Segmented")
else:
print("String Can NOT be Segmented")
String Can NOT be Segmented
array_rand = set([1, 3, 5, 6, 2, 8])
def find_Missing(array_rand):
l = []
for i in range(min(array_rand), max(array_rand)+1):
if i in array_rand:
continue
else:
l.append(i)
return l
set(find_Missing(array_rand))
array_rand = set([1, 3, 5, 6, 2, 8])
[i for i in range(min(array_rand), max(array_rand)+ 1)]
max(array_rand)
numberMatrix = [[0,0,1,1,0,0], [1,0,1,1,0,0], [1,0,0,1,1,0], [0,1,0,0,0,1], [0,1,0,1,0,0]]
# Marker to avoid duplicated count
numberMatrix_Marker = [[0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0]]
sum_adj = 0
n = len(numberMatrix)
for i in range(n - 1):
for j in range(n - 1):
if numberMatrix_Marker[i][j] == 1:
# continue loop if marked
continue
if numberMatrix[i][j] == 0:
# conitnue if not 1
continue
if numberMatrix[i][j] == numberMatrix[i][j + 1] == 1:
# if current cell and next col is 1 count 1
sum_adj += 1
# mark two cells
numberMatrix_Marker[i][j] = 1
numberMatrix_Marker[i][j + 1] = 1
print('next col',i,j)
continue
if numberMatrix[i][j] == numberMatrix[i + 1][j] == 1:
sum_adj += 1
numberMatrix_Marker[i][j] = 1
numberMatrix_Marker[i+1][j] = 1
print('next row',i,j)
continue
print(sum_adj)
next col 0 2
next row 1 0
next col 1 2
next col 2 3
next row 3 1
5
def adjacent_counter(numberMatrix):
numberMatrix_Marker = []
sum_adj = 0
n = len(numberMatrix)
for i in range(n):
numberMatrix_Marker.append([0 for i in range(n)])
for i in range(n - 1):
for j in range(n - 1):
if numberMatrix_Marker[i][j] == 1:
# continue loop if marked
continue
if numberMatrix[i][j] == 0:
# conitnue if not 1
continue
if numberMatrix[i][j] == numberMatrix[i][j + 1] == 1:
# if current cell and next col is 1 count 1
sum_adj += 1
# mark two cells
numberMatrix_Marker[i][j] = 1
numberMatrix_Marker[i][j + 1] = 1
print('next col',i,j)
continue
if numberMatrix[i][j] == numberMatrix[i + 1][j] == 1:
sum_adj += 1
numberMatrix_Marker[i][j] = 1
numberMatrix_Marker[i+1][j] = 1
print('next row',i,j)
continue
return sum_adj
numberMatrix = [[0,0,1,1,0,0], [1,0,1,1,0,0], [1,0,0,1,1,0], [0,1,0,0,0,1], [0,1,0,1,0,0]]
adjacent_counter(numberMatrix)
next col 0 2
next row 1 0
next col 1 2
next col 2 3
next row 3 1