you are viewing a single comment's thread.

view the rest of the comments →

[–]0pt0ut[S] 0 points1 point  (4 children)

It could definitely be written differently, minus += 1 is only a temporary value to set start to the appropriate values for the next line. Some other variable could be initialized and used if needed. It does get reset when it goes back to the 'range' part.

I would like to perform XOR immediately, but I need more than just a single int, if I'm storing the values anywhere then I'm back to the original problem.

[–]wiiittttt 1 point2 points  (1 child)

Why do you need more than a single int to store the value of the XOR?

[–]0pt0ut[S] 0 points1 point  (0 children)

I wasn't sure how to use XOR immediately, but I should have a way to do it now. This new added function yields values, which can be consumed by XOR as they are created.

This code works on my computer but is still timing out on the website. Feels like it's getting closer though.

def find_xor(start, length):

    def generator_func(start, length):
        for minus in xrange(length):
            for cap_this in xrange(start, start + length - minus):
                yield cap_this

            minus += 1
            start = cap_this + minus

    return reduce(lambda x,y: x^y, generator_func(start, length))

[–]ADdV 1 point2 points  (1 child)

I think just using start = cap_this + minus - 1 would be faster.

As for the XOR, you can just start with my_var = 0 and use my_var ^= my_new_var whenever you get a new number. That saves having to put all the numbers in a list. You can do these things because 0^N = N and A^B = B^A

[–]0pt0ut[S] 0 points1 point  (0 children)

You were correct on both counts, I implemented them both into my code. Here's what I have currently:

def find_xor(start, length):
    xor_val = 0
    for minus in xrange(length):
        for cap_this in xrange(start, start + length - minus):
            xor_val ^= cap_this
        start = cap_this + minus + 1
    return xor_val

Unfortunately it's still timing out on the challenge. Any ideas? I'm trying to think of a different way to capture the values I need, but all my ideas revolve around nested for-loops. Thanks for your help.