Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created January 9, 2015 08:40
Show Gist options
  • Save ryo1kato/169fb05df91fda0fbe60 to your computer and use it in GitHub Desktop.
Save ryo1kato/169fb05df91fda0fbe60 to your computer and use it in GitHub Desktop.
Calculate weight distribution of human-piramid
#!/usr/bin/python
import sys
NR_LAYER = 10
def get_n_plus_1(weights):
length = len(weights)
lower_weights = [[1 for col in range(length+1)] for row in range(length+1)]
print "---- %d -----" % len(weights)
for x in range(0,length):
for y in range(0,length):
#print weights[x][y]
qw = weights[x][y] / 4.0 # quarter of weight
lower_weights[x] [y] += qw
lower_weights[x+1][y] += qw
lower_weights[x] [y+1] += qw
lower_weights[x+1][y+1] += qw
return lower_weights
layers = []
layers.append([[1.0]])
for i in range(0,NR_LAYER):
layers.append( get_n_plus_1(layers[i]) )
i=1
for l in layers:
total = 0
for x in l:
for y in x:
sys.stdout.write("%.2f " % y)
total += y
sys.stdout.write("\n")
print "============ layer%d: %.2f =============" % (i, total)
i+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment