python - 尝试完成 Google foo.bar 级别 3 (queue_to_do) 并继续超过时间限制

标签 python algorithm xor

<分区>

老实说,我不认为我的代码那么糟糕....这是我提出的第五个版本,我认为它是迄今为止最好的......但是当我运行测试时foo.bar 控制台显示“超出时间限制”

你看到我可以加快速度的方法了吗?我现在不知所措......

这是自述文件:

Queue To Do

You're almost ready to make your move to destroy the LAMBCHOP doomsday device, but the security checkpoints that guard the underlying systems of the LAMBCHOP are going to be a problem. You were able to take one down without tripping any alarms, which is great! Except that as Commander Lambda's assistant, you've learned that the checkpoints are about to come under automated review, which means that your sabotage will be discovered and your cover blown - unless you can trick the automated review system.

To trick the system, you'll need to write a program to return the same security checksum that the guards would have after they would have checked all the workers through. Fortunately, Commander Lambda's desire for efficiency won't allow for hours-long lines, so the checkpoint guards have found ways to quicken the pass-through rate. Instead of checking each and every worker coming through, the guards instead go over everyone in line while noting their security IDs, then allow the line to fill back up. Once they've done that they go over the line again, this time leaving off the last worker. They continue doing this, leaving off one more worker from the line each time but recording the security IDs of those they do check, until they skip the entire line, at which point they XOR the IDs of all the workers they noted into a checksum and then take off for lunch. Fortunately, the workers' orderly nature causes them to always line up in numerical order without any gaps.

For example, if the first worker in line has ID 0 and the security checkpoint line holds three workers, the process would look like this: 0 1 2 / 3 4 / 5 6 / 7 8 where the guards' XOR (^) checksum is 0^1^2^3^4^6 == 2.

Likewise, if the first worker has ID 17 and the checkpoint holds four workers, the process would look like: 17 18 19 20 / 21 22 23 / 24 25 26 / 27 28 29 / 30 31 32 which produces the checksum 17^18^19^20^21^22^23^25^26^29 == 14.

All worker IDs (including the first worker) are between 0 and 2000000000 inclusive, and the checkpoint line will always be at least 1 worker long.

With this information, write a function answer(start, length) that will cover for the missing security checkpoint by outputting the same checksum the guards would normally submit before lunch. You have just enough time to find out the ID of the first worker to be checked (start) and the length of the line (length) before the automatic review occurs, so your program must generate the proper checksum with just those two values.

Languages

To provide a Python solution, edit solution.py To provide a Java solution, edit solution.java

Test cases

Inputs: (int) start = 0 (int) length = 3 Output: (int) 2

Inputs: (int) start = 17 (int) length = 4 Output: (int) 14

Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

我的解决方案:

def answer(start, length):
    f = 0
    r = 0
    while f < length:
        for i in range(start, (start+length) - f):
            r ^= i
        f += 1
        start = range(start, start+length)[-1] + 1
    return r

最佳答案

提示1

XOR 5^6^7^8 等于 XOR(1^2^3^4^5^6^7^8)^(1^2^3^4)。

换句话说,你应该专注于找到一个函数,它是前 n 个自然数的异或,然后你可以使用这个函数来找到任何整数范围的异或。

提示2

要找到前 n 个自然数的异或,请考虑二进制表示:

000
001
010
011
100
101
110
111

专注于单个位的位置,并考虑在计算所有数字的异或时得到的模式。

关于python - 尝试完成 Google foo.bar 级别 3 (queue_to_do) 并继续超过时间限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45335975/

相关文章:

javascript - 为什么我的 MoveZeroes 代码不修改输入数组?

Swift Biginteger xor 加密/解密

python - Fiddler 可以更改 Python 请求模块的返回吗?

algorithm - 哈希表与树

python - 从带有时间戳的日志中提取信息

algorithm - 如何打开一个单词包装的文本?

c++ - 数组反向 - XOR 比使用临时对象切换慢

java - 简单来说,Graphics.setXORMode(Color) 的作用是什么?

python - Tesseract 是否在内部调整图像大小?

python - 如何仅提取时代细节并将其他内容留在 pandas 数据框中?