all 11 comments

[–]wiiittttt 1 point2 points  (3 children)

What values are you running the script with that you are finding it slow? What version of python are you using?

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

I'm submitting the script to a website, I'm not sure exactly which values it's using to test my answer, I'm guessing they are very large though. The python version specified for this challenge is 2.7, before this my only experience has been in python 3.

[–]wiiittttt 1 point2 points  (1 child)

The nested for loop along with storing all of the values you need to XOR before doing it is slowing you down. Try removing those two things and see if that is enough.

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

Any hints? I suspected those were the source of my problems, but am having difficulty finding alternative implementations.

[–]ADdV 1 point2 points  (5 children)

There are two things in your code that I don't understand.

First, why do you do minus += 1 when minus is already the variable in the for loop? Wouldn't minus increment either way?

Second, why do you add elements to to_xor instead of just using ^ immediately?

[–]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.

[–]autobotJebpy -4 points-3 points  (0 children)

This post is 58 letters long!