Skip to content

Instantly share code, notes, and snippets.

@arsalanses
Created January 4, 2021 09:11
Show Gist options
  • Save arsalanses/62e16765ef2cdf95620cfe597fc5529d to your computer and use it in GitHub Desktop.
Save arsalanses/62e16765ef2cdf95620cfe597fc5529d to your computer and use it in GitHub Desktop.
You’re trying to build an IoT mesh network.
# You’re trying to build an IoT mesh network.
# Signals can only travel the maximum of 5 units.
# You’re given coordinates for the switch, the light, and the mesh hubs (which capture and forward signals).
# Return true if the switch can successfully toggle the light.
# Example:
# let network = { switch: [0,1], hub: [[2,1], [2,5]], light: [1,6] }
# $ canToggle(network)
# $ true
def is_overlap(c0, c1, R0, R1):
f = (c0[0] - c1[0]) ** 2 + (c0[1] - c1[1]) ** 2
if (R0 - R1) ** 2 <= f:
if f <= (R0 + R1) ** 2:
return True
return False
def canToggle(network):
if not is_overlap(network['switch'], network['hub'][0] , 5, 5):
return False
for item in range(len(network['hub'])):
if item+1 != len(network['hub']) and not is_overlap(network['hub'][item], network['hub'][item + 1] , 5, 5):
break
else:
if is_overlap(network['hub'][-1], network['light'] , 5, 5):
return True
else:
return False
return False
network = { 'switch': [0, 1], 'hub': [[2, 1], [2, 5]], 'light': [1, 6] }
print(canToggle(network))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment