# MessageBird Hacker-Rank interviews.
# Question 1.
gridOfNodes = [
[1, 1, 1],
[0, 1, 0],
[0, 0, 0],
[1, 1, 0],
]
# Find the number of connections.
# If a spot is a 1(not a zero) it must connect to
# any other spot that is a 1 in any FIRST
# subsequent NEXT level.
# In the grid above, the number of connections is 5.
def numberOfConnections(gridOfNodes):
len_grid = len(gridOfNodes)
no_connections = 0
for i in range(0, len_grid):
if i == len_grid - 1:
# we are at the end
break
nodes_in_this_level = sum([x for x in gridOfNodes[i] if x == 1])
if nodes_in_this_level == 0:
continue
else:
nodes_in_next_level = 0
yet_to_find_next_level = True
while yet_to_find_next_level:
for j in range(i + 1, len_grid):
nodes_in_next_level = sum([y for y in gridOfNodes[j] if y == 1])
if nodes_in_next_level > 0:
yet_to_find_next_level = False
break
if j == len_grid - 1:
yet_to_find_next_level = False
break
no_connections = no_connections + (nodes_in_this_level * nodes_in_next_level)
return no_connections
assert numberOfConnections(gridOfNodes) == 5
gridOfNodes2 = [
[1, 1, 1],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
assert numberOfConnections(gridOfNodes2) == 0
# Question 2.
# Find the number of characters that are in between a string that contains a programmer strings.
# A string is a programmer string if its characters can be re-arranged to spell the word `programmer`
# For example the string s="programmerXYZprozmerqgram" has 3chars between programmer strings.
# ie x= ["programmer", "XYZ", "prozmerqgram"]
# x[0] already spells `programmer`
# x[2] can be arranged to spell `programmer zq`
# and hence len(x[1])==3 is the result.
# I was NOT able to answer this.
# Qustion 3.
# https://helloacm.com/how-to-determine-the-type-of-tree-nodes-using-sql/