Created
October 19, 2016 15:17
-
-
Save ryantuck/755de52f6cbef3b53a8e457b4b5d4f47 to your computer and use it in GitHub Desktop.
stackin' ipads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def num_ipads(rows): | |
return sum(i+1 for i in range(rows)) | |
def num_rows(ipads): | |
running_total = 0 | |
i = 0 | |
while ipads > 0: | |
i += 1 | |
ipads -= i | |
if ipads != 0: | |
print 'warning, remainder left :( {}'.format(ipads) | |
return i | |
def load(row, row_pos): | |
if row == 1: | |
return 0 | |
if row_pos in [0, row-1]: | |
return (load(row-1, 0) + 1) / 2.0 | |
return (load(row-1, row_pos-1) + load(row-1, row_pos)) / 2.0 + 1.0 | |
if __name__ == '__main__': | |
ipads = 276 | |
rows = num_rows(ipads) | |
for row in range(2,rows+1): | |
outer_load = load(row, 0) | |
inner_load = load(row, row/2) | |
ratio = inner_load / outer_load | |
print '%i | %2f / %2f = %2f' % (row, inner_load, outer_load, ratio) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment