Skip to content

Instantly share code, notes, and snippets.

@tristanpendergrass
Created May 10, 2016 20:33
Show Gist options
  • Select an option

  • Save tristanpendergrass/9a3a5cb68e52da02811120f997a867f4 to your computer and use it in GitHub Desktop.

Select an option

Save tristanpendergrass/9a3a5cb68e52da02811120f997a867f4 to your computer and use it in GitHub Desktop.
# PYTHON VERSION
# https://www.reddit.com/r/dailyprogrammer
# [2016-05-09] Challenge #266 [Easy] Basic Graph Statistics: Node Degrees
# assumes data does not duplicate nodes (ie. if there is a '4 6', there is NOT a '6 4')
# sample input data, turned into space delimited string
inputData = ("16 1 2 1 3 2 3 1 4 3 4 1 5 2 5 1 6 2 6 3 6 3 7 5 7 6 7 3 8 4 8 6 " +
"8 7 8 2 9 5 9 6 9 2 10 9 10 6 11 7 11 8 11 9 11 10 11 1 12 6 12 7 12 8 12 11 12 6 " +
"13 7 13 9 13 10 13 11 13 5 14 8 14 12 14 13 14 1 15 2 15 5 15 9 15 10 15 11 15 12 " +
"15 13 15 1 16 2 16 5 16 6 16 11 16 12 16 13 16 14 16 15 16")
inputList = inputData.split(" ")
inputList.pop(0) # get rid of opening '16' - not needed
result = [0] * 17
first, second = 0, 0
i = 0
while i < len(inputList):
first = int(inputList[i])
second = int(inputList[i+1])
result[first] = result[first] + 1
result[second] = result[second] + 1
i = i + 2
i = 1
while i < len(result):
print "Node ", i, "has a degree of", result[i]
i +=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment