Last active
December 18, 2015 08:59
-
-
Save ds300/5758662 to your computer and use it in GitHub Desktop.
JuggleGraph tips
This file contains hidden or 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
# let's start with this: | |
siteswaplist = [] | |
while len(siteswap) > 0: | |
if siteswap[0] == "a": | |
siteswaplist.append(10) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "b": | |
siteswaplist.append(11) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "c": | |
siteswaplist.append(12) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "d": | |
siteswaplist.append(13) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "e": | |
siteswaplist.append(14) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "f": | |
siteswaplist.append(15) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "g": | |
siteswaplist.append(16) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "h": | |
siteswaplist.append(17) | |
siteswap = siteswap[1:] | |
if siteswap[0] == "0" or siteswap[0] == "1" or siteswap[0] == "2" or siteswap[0] == "3" or siteswap[0] == "4" or siteswap[0] == "5" or siteswap[0] == "6" or siteswap[0] == "7" or siteswap[0] == "8" or siteswap[0] == "9": | |
siteswaplist.append(int(siteswap[0])) | |
siteswap = siteswap[1:] | |
# there's a few things wrong here. | |
# first, if you've got a big load of if-statements and only one will be true, | |
# then you should use else-if-statements because otherwise all the ones that | |
# come after the true one get processed too, which wastes the computer's time. | |
# in python, you can do that with elif | |
# example: | |
siteswaplist = [] | |
while len(siteswap) > 0: | |
if siteswap[0] == "a": | |
siteswaplist.append(10) | |
siteswap = siteswap[1:] | |
elif siteswap[0] == "b": | |
siteswaplist.append(11) | |
siteswap = siteswap[1:] | |
elif siteswap[0] == "c": | |
siteswaplist.append(12) | |
siteswap = siteswap[1:] | |
# ... and so on. | |
# secondly, that huge if-statement at the bottom (the one where you check | |
# if the character is a digit) can be reduced to: | |
if siteswap[0].isdigit(): | |
# python is pretty great for that kind of jazz | |
# thirdly, you don't need all the different if-statements for each individual | |
# letter case. it turns out that characters are represented in-memory as numbers, | |
# and you can use the ord() function to find out what those numbers are. | |
# e.g. ord("a") is 97. Even more fortuitous is that the numbers are assigned in | |
# alphabetical order, so ord("b") is 98, ord("c") is 99, and so on. | |
# in addition to all this goodness, you can compare strings in if-statements, so | |
# it is possible to turn that enormous sequence of if-statments into just: | |
if "a" <= siteswap[0] <= "h": | |
siteswaplist.append(ord(siteswap[0]) - 87) | |
siteswap = siteswap[1:] | |
elif siteswap[0].isdigit(): | |
siteswaplist.append(int(siteswap[0])) | |
siteswap = siteswap[1:] | |
# lastly, rather than removing the first character from the siteswap string when | |
# you're done, you can just iterate over the characters in the string usinng a | |
# for loop. so the whole thing becomes: | |
siteswaplist = [] | |
for s in siteswap: | |
if "a" <= s <= "h": | |
siteswaplist.append(ord(s) - 87) | |
elif s.isdigit(): | |
siteswaplist.append(int(s)) | |
# see how much smaller and easier-to-read that is? | |
# now to this bit: | |
count = 0 #takes the siteswap list and iterates it 10 times | |
while count < 80: | |
sslcount = 0 | |
while sslcount < siteswaplength: | |
siteswaplist.append(siteswaplist[sslcount]) | |
sslcount = sslcount + 1 | |
count = count + 1 | |
# for starters, you're actually looping the list 80 times. | |
# but most importantly, you can do this using the .extend method of list objects. | |
# .extend takes a list and adds all its elements to the end. | |
# e.g. | |
cheese = [1,2,3] | |
cheese.extend([4,5,6]) | |
print(cheese) | |
# => [1,2,3,4,5,6] | |
# also, if you want to do something n times, where n doesn't depend on what happens | |
# while you're doing that something, then it is far easier and more readable to | |
# use a for-loop. So I would change your code to: | |
for i in range(10): # or 80, whatever | |
siteswaplist.extend(siteswaplist[:siteswaplength]) | |
# as for the color assiging stuff... I'm sorry but that's just a big mess and | |
# deserves to be completely rethought. | |
# I will say, however, that something like: | |
while siteswaplist[count] == 0 or colorlist[count] == "blue" or colorlist[count] == "red" or colorlist[count] == "yellow" or colorlist[count] == "green": | |
# can be replaced by: | |
while siteswaplist[count] == 0 or colorlist[count] in ("blue", "red", "yellow", "green"): | |
# so let's rethink the problem of assiging colors to throws. | |
# first off, what have we got so far? | |
# siteswaplist - a bunch of numbers representing throw heights | |
# ballcount - how many balls are involved in the siteswap | |
# each ball needs a different color so let's create a list of 9 colors to begin | |
colors = ["red", "blue", "yellow", "green", "pink", "purple", "orange", "black", "gold"] | |
# now we can model each ball as just being a color: | |
balls = colors[:ballcount] | |
# now we need to assign balls to throws, rather than colors. That's easy peasy when | |
# we understand how siteswap works. Just take a ball B and a throw numbered T with | |
# height H, then assign B to T and then to the throw numbered T+H with height H2, and | |
# then the throw numbered T+H+H2 and so on and so on. | |
# we'll modify siteswaplist in place, replacing throw heights with (height, ball) | |
# tuples to represent an assigned ball, but leave throws with height 0 alone, | |
# since they don't involve balls. | |
# so iterate over our balls: | |
for ball in balls: | |
# start at throw 0 | |
throw = 0 | |
# skip over any throws that have already been assigned a ball, or are height 0, | |
while siteswaplist[throw] == 0 or type(siteswaplist[throw]) is tuple: | |
throw = throw + 1 | |
# now siteswaplist[throw] is the height of the first unassigned throw | |
while throw < len(siteswaplist): | |
height = siteswaplist[throw] | |
# so let's assign the ball to this throw | |
siteswaplist[throw] = (height, ball) | |
# now we increment throw by height and assign the ball to that throw and so on | |
throw = throw + height | |
# and that's it. | |
# now the bit to draw the graph looks like this: | |
for i in range(len(siteswaplist)): | |
if siteswaplist[i] != 0: | |
# not 0 so we know it's a tuple (given a well-formed siteswap) | |
throw_height, ball_color = siteswaplist[i] | |
xy = i*20, 800+(throw_height**2)*5, i*20+throw_height*20, 800-(throw_height**2)*5 | |
canvas.create_arc(xy, start=0, extent=180, width=3, outline=ball_color) | |
canvas.create_text(i*20-2, 810, anchor=W, text=throw_height) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment