Skip to content

Instantly share code, notes, and snippets.

@DavidBuchanan314
Created June 27, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBuchanan314/a9bee9505a5b45aa7b2ed57bf6dc02a2 to your computer and use it in GitHub Desktop.
Save DavidBuchanan314/a9bee9505a5b45aa7b2ed57bf6dc02a2 to your computer and use it in GitHub Desktop.
import inspect
# helper function to visualise the results
def print_table(width, height, function):
grid = [[" "]*(width + 1) for _ in range(10)]
for n in range(1, 118+1):
y, x = function(n)
grid[y][x] = str(n).ljust(3)
for x in range(1, width+1):
grid[0][x] = str(x).ljust(3)
for y in range(1, height+1):
grid[y][0] = str(y).ljust(3)
print("")
print("")
print(inspect.getsource(function))
print("")
for row in grid:
print("|".join(row))
print("")
# map n to (y, x) coordinates on a 32-wide grid. The choice of 32
# will become apparent later
# the -1 and +1 are to account for the 1-indexed inputs and outputs
def coords_0(n):
y, x = divmod(n - 1, 32)
# the divmod is equivalent to y, x = ((n-1)//32), ((n-1)%32)
return y + 1, x + 1
print_table(32, 9, coords_0)
# note: first row and first column are just labels
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32
1 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32
2 |33 |34 |35 |36 |37 |38 |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50 |51 |52 |53 |54 |55 |56 |57 |58 |59 |60 |61 |62 |63 |64
3 |65 |66 |67 |68 |69 |70 |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 |84 |85 |86 |87 |88 |89 |90 |91 |92 |93 |94 |95 |96
4 |97 |98 |99 |100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118| | | | | | | | | |
5 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
6 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
7 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
"""
# doing arithmetic on a bool is treated like an int, where True=1 and False=0
# we use this to create the first "gap" in the numbers (between hydrogen and helium)
def coords_1(n):
n += (n>1)*30 # this expression evaluates to 30 when n>1, and 0 otherwise
y, x = divmod(n - 1, 32)
return y + 1, x + 1
print_table(32, 9, coords_1)
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32
1 |1 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |2
2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |34
3 |35 |36 |37 |38 |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50 |51 |52 |53 |54 |55 |56 |57 |58 |59 |60 |61 |62 |63 |64 |65 |66
4 |67 |68 |69 |70 |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 |84 |85 |86 |87 |88 |89 |90 |91 |92 |93 |94 |95 |96 |97 |98
5 |99 |100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118| | | | | | | | | | | |
6 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
7 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
"""
# same concept is applied to create the next 2 gaps
def coords_2(n):
n += (n>1)*30 + ((n>4)+(n>12))*24 # this last expression is equivalent to (n>4)*24 + (n>12)*24
y, x = divmod(n - 1, 32)
return y + 1, x + 1
print_table(32, 9, coords_2)
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32
1 |1 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |2
2 |3 |4 | | | | | | | | | | | | | | | | | | | | | | | | |5 |6 |7 |8 |9 |10
3 |11 |12 | | | | | | | | | | | | | | | | | | | | | | | | |13 |14 |15 |16 |17 |18
4 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |34 |35 |36 |37 |38 |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50
5 |51 |52 |53 |54 |55 |56 |57 |58 |59 |60 |61 |62 |63 |64 |65 |66 |67 |68 |69 |70 |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82
6 |83 |84 |85 |86 |87 |88 |89 |90 |91 |92 |93 |94 |95 |96 |97 |98 |99 |100|101|102|103|104|105|106|107|108|109|110|111|112|113|114
7 |115|116|117|118| | | | | | | | | | | | | | | | | | | | | | | | | | | |
8 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
"""
# aaand the next 2 gaps
# At this point we have the "full" table.
# But, by convention, we will need to relocate the Lanthanides and Actinides
def coords_3(n):
n += (n>1)*30 + ((n>4)+(n>12))*24 + ((n>20)+(n>38))*14
y, x = divmod(n - 1, 32)
return y + 1, x + 1
print_table(32, 9, coords_3)
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32
1 |1 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |2
2 |3 |4 | | | | | | | | | | | | | | | | | | | | | | | | |5 |6 |7 |8 |9 |10
3 |11 |12 | | | | | | | | | | | | | | | | | | | | | | | | |13 |14 |15 |16 |17 |18
4 |19 |20 | | | | | | | | | | | | | | |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |34 |35 |36
5 |37 |38 | | | | | | | | | | | | | | |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50 |51 |52 |53 |54
6 |55 |56 |57 |58 |59 |60 |61 |62 |63 |64 |65 |66 |67 |68 |69 |70 |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 |84 |85 |86
7 |87 |88 |89 |90 |91 |92 |93 |94 |95 |96 |97 |98 |99 |100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118
8 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
"""
# python has a useful shorthand for checking if a number is in some range - (1<x<16)
# We use this to shift the Lanthanides and Actinides down by 2 rows
def coords_4(n):
n += (n>1)*30 + ((n>4)+(n>12))*24 + ((n>20)+(n>38))*14
y, x = divmod(n - 1, 32)
return y + (1<x<16)*2 + 1, x + 1
print_table(32, 9, coords_4)
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32
1 |1 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |2
2 |3 |4 | | | | | | | | | | | | | | | | | | | | | | | | |5 |6 |7 |8 |9 |10
3 |11 |12 | | | | | | | | | | | | | | | | | | | | | | | | |13 |14 |15 |16 |17 |18
4 |19 |20 | | | | | | | | | | | | | | |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |34 |35 |36
5 |37 |38 | | | | | | | | | | | | | | |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50 |51 |52 |53 |54
6 |55 |56 | | | | | | | | | | | | | | |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 |84 |85 |86
7 |87 |88 | | | | | | | | | | | | | | |103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118
8 | | |57 |58 |59 |60 |61 |62 |63 |64 |65 |66 |67 |68 |69 |70 | | | | | | | | | | | | | | | |
9 | | |89 |90 |91 |92 |93 |94 |95 |96 |97 |98 |99 |100|101|102| | | | | | | | | | | | | | | |
"""
# and finally, shift the whole right side into position
def coords_4(n):
n += (n>1)*30 + ((n>4)+(n>12))*24 + ((n>20)+(n>38))*14
y, x = divmod(n - 1, 32)
return y + (1<x<16)*2 + 1, x - (x>15)*14 + 1
print_table(18, 9, coords_4)
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18
1 |1 | | | | | | | | | | | | | | | | |2
2 |3 |4 | | | | | | | | | | |5 |6 |7 |8 |9 |10
3 |11 |12 | | | | | | | | | | |13 |14 |15 |16 |17 |18
4 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |34 |35 |36
5 |37 |38 |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50 |51 |52 |53 |54
6 |55 |56 |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 |84 |85 |86
7 |87 |88 |103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118
8 | | |57 |58 |59 |60 |61 |62 |63 |64 |65 |66 |67 |68 |69 |70 | |
9 | | |89 |90 |91 |92 |93 |94 |95 |96 |97 |98 |99 |100|101|102| |
"""
# now we're done - we can rearrange things a bit, to be even more concise:
def c(n):y,x=divmod(n-1+(n>1)*30+((n>4)+(n>12))*24+((n>20)+(n>38))*14,32);return y+(1<x<16)*2+1,x-(x>15)*14+1
print_table(18, 9, c)
"""
|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18
1 |1 | | | | | | | | | | | | | | | | |2
2 |3 |4 | | | | | | | | | | |5 |6 |7 |8 |9 |10
3 |11 |12 | | | | | | | | | | |13 |14 |15 |16 |17 |18
4 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |34 |35 |36
5 |37 |38 |39 |40 |41 |42 |43 |44 |45 |46 |47 |48 |49 |50 |51 |52 |53 |54
6 |55 |56 |71 |72 |73 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 |84 |85 |86
7 |87 |88 |103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118
8 | | |57 |58 |59 |60 |61 |62 |63 |64 |65 |66 |67 |68 |69 |70 | |
9 | | |89 |90 |91 |92 |93 |94 |95 |96 |97 |98 |99 |100|101|102| |
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment