gpt4 book ai didi

python - 谷歌 Foobar : How to find edge cases and identify test cases. Python

转载 作者:行者123 更新时间:2023-12-05 09:18:03 25 4
gpt4 key购买 nike

<分区>

问题

Fuel Injection Perfection

Commander Lambda has asked for your help to refine the automatic quantum antimatter fuel injection system for her LAMBCHOP doomsday device. It's a great chance for you to get a closer look at the LAMBCHOP - and maybe sneak in a bit of sabotage while you're at it - so you took the job gladly.

Quantum antimatter fuel comes in small pellets, which is convenient since the many moving parts of the LAMBCHOP each need to be fed fuel one pellet at a time. However, minions dump pellets in bulk into the fuel intake. You need to figure out the most efficient way to sort and shift the pellets down to a single pellet at a time.

The fuel control mechanisms have three operations:

Add one fuel pellet Remove one fuel pellet Divide the entire group of fuel pellets by 2 (due to the destructive energy released when a quantum antimatter pellet is cut in half, the safety controls will only allow this to happen if there is an even number of pellets) Write a function called answer(n) which takes a positive integer as a string and returns the minimum number of operations needed to transform the number of pellets to 1. The fuel intake control panel can only display a number up to 309 digits long, so there won't ever be more pellets than you can express in that many digits.

For example: answer(4) returns 2: 4 -> 2 -> 1 answer(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1

Test cases

Inputs: (string) n = "4" Output: (int) 2

Inputs: (string) n = "15" Output: (int) 5

这是我的解决方案:

import math
import decimal
def answer(n):
n = long(n)
if n <= 1 or n >= long('9' * 309):
return 0
# 1. Find closest power of 2
round_threshold_adjust = math.log(3,2)- (1.5)
log_n = math.log(n, 2)
power = log_n - round_threshold_adjust

# Round power down if X.50000. If n is equally between two powers of 2,
# choose the lower power of 2. E.g. For 6, choose, 4 not 8
power2 = long(decimal.Decimal(power).quantize(0, decimal.ROUND_HALF_DOWN))

# 2. Calculate the difference, add to iterations
# 3. Take log 2 of that, add that to iteration
iters = abs(n - 2**power) + power
return(iters)

我的解决方案目前通过了 10 个测试用例中的 3 个。我相信其他测试用例是边缘用例。你能给我一些关于如何识别我的代码失败的指示吗? (我无权访问测试用例)

以下是我尝试过的一些测试用例:

assert answer(15) == 5
assert answer(4) == 2
assert answer(3) == 2
assert answer(2) == 1
assert answer(6) == 4
assert answer(7) == 4
assert answer(10) == 5
assert answer(1024) == 10
assert answer(1025) == 11
assert answer(1026) == 12
assert answer(1027) == 13
assert answer(768) == 256 + 9

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com