Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Drawing all paths from (0, 0) to (n, n) moving one unit right or up

This question is just the same as this one made for Mathematica. I saw it and I was trying to reproduce it in Sage just for fun, but it's getting longer than I like and I would love to know your approach in Sage. I think it's a great way to learn. This is half of my try:

n=3
A=sum(line([(j, i), (n, i)]) for j in range(n+1) for i in range(n+1))
B=sum(line([(i, j), (i, n)]) for j in range(n+1) for i in range(n+1))
G = Graphics()
G += A + B
G.show(figsize=[4,4], axes=False)
result = []
combinations = [bin(i)[2:] for i in range(1, int('111111', 2))]
for num in combinations:
    valid = ''.join(['0']*(6-len(num))) + num
    zeros = valid.count('0')
    ones = valid.count('1')
    if zeros == 3 and ones == 3:
        result.append(str(valid))
#At this point all the paths are stored in the variable 'results' in binary form.
#For example '010101' means right, left, right, left, right, left
paths = [[]]
for element in result:
    path = []
    for index, direction in enumerate(list(element)):
        if direction == '0':
            path.append((index, index - 1))
        else:
            path.append((index - 1, index))
    paths.append(path)

At this point the list of list called paths is not well constructed. I realized I would have to put some if statements to make it work but I'm losing motivation in my solution because it's getting ugly and I don't think is very efficient.

How would you do it?

click to hide/show revision 2
No.2 Revision

Drawing all paths from (0, 0) to (n, n) moving one unit right or up

This question is just the same as this one made for Mathematica. I saw it and I was trying to reproduce it in Sage just for fun, but it's getting longer than I like and I would love to know your approach in Sage. I think it's a great way to learn. This is half of my try:

n=3
A=sum(line([(j, i), (n, i)]) for j in range(n+1) for i in range(n+1))
B=sum(line([(i, j), (i, n)]) for j in range(n+1) for i in range(n+1))
G = Graphics()
G += A + B
G.show(figsize=[4,4], axes=False)
result = []
combinations = [bin(i)[2:] for i in range(1, int('111111', 2))]
for num in combinations:
    valid = ''.join(['0']*(6-len(num))) + num
    zeros = valid.count('0')
    ones = valid.count('1')
    if zeros == 3 and ones == 3:
        result.append(str(valid))
#At this point all the paths are stored in the variable 'results' in binary form.
#For example '010101' means right, left, right, left, right, left
paths = [[]]
for element in result:
    path = []
    for index, direction in enumerate(list(element)):
        if direction == '0':
            path.append((index, index - 1))
        else:
            path.append((index - 1, index))
    paths.append(path)

At this point the list of list called paths is not well constructed. I realized I would have to put some if statements to make it work but I'm losing motivation in my solution because it's getting ugly and I don't think is very efficient.

How would you do it?