foob​​ar挑战特殊平衡的递归函数

时间:2016-05-27 00:41:46

标签: python recursion

所以我终于为foobar挑战写了一个递归函数"特殊的平衡"。它与我当地的蟒蛇运行良好,但foobar给了我"时间限制超过"错误。我以为我会分享这些代码并询问是否有人知道问题所在。

以下是说明和解决方案:

特殊的平衡

我们能救他们吗? Beta兔子试图进入一个包含唯一已知僵尸治疗方法的实验室 - 但这是一个障碍。只有正确解决了挑战,门才会打开。僵尸兔群的未来受到威胁,因此Beta读取了挑战:左侧有一个物体,其质量以一定数量的单位给出。可以预见,任务是平衡双方。但有一个问题:你只有这个特殊的重量集,有1,3,3,27 ......单位的质量。也就是说,每个3的幂都是一个。作为一个出色的数学家,Beta Rabbit很快发现任何数量的质量单位都可以使用这个集合进行精确平衡。

为了帮助Beta进入房间,编写一个名为answer(x)的方法,该方法输出一个表示权重放置位置的字符串列表,以便双方平衡,假设左边的权重有质量x单位。

输出列表的第一个元素应对应于1个单位的权重,第二个元素对应于3个单位的权重,依此类推。每个字符串都是以下之一:

" L" :把重量放在左手边 " R" :把重量放在右手边 " - " :不要使用重量

为了确保输出尽可能小,列表的最后一个元素不能是" - "。

x将始终为正整数,不大于1000000000。

语言

要提供Python解决方案,请编辑solution.py 要提供Java解决方案,请编辑solution.java

测试用例

输入:     (int)x = 2 输出:     (字符串列表)[" L"," R"]

输入:     (int)x = 8 输出:     (字符串列表)[" L"," - "," R"]

=============================================== ================

递归函数

import math

def calc_weights(x, A, B):
    # For a given x, A, B, use the closest integer which is a power of 3 and see if it works to balance the scale
    # Finds the closest power of 3 number to x, then subtracts it from x and calls itself again. x will reduce at each step
    # A keeps a list of numbers used e.g. [1, 9, 27]
    # B keeps a list of side for each element in A. e.g. ['R', 'L', 'R']
    # x, A, B are updated everytime this function is called
    if abs(x) > 1:
        xorig = x; Aorig=list(A); Borig=list(B);

        for lb in range(0, 2):      # For each x, we have 2 combinations. E.g. x=45: use 3**3=27 or 3**4=81 (lower bound, and higher bound). The code will examine one number, if it fails it goes for the second number
            #a = pow(3, int(math.log(abs(x), 3)))  * (3**(1-lb))  # if lb=0 then use lower bound. If lb=1 then use the higher bound.
            a = pow(3, int(math.log(abs(x), 3)))  * (3**(lb))

            while (a in A) and (a>0):   # if a is used already, then use another number
                a = int(a/3)
            if (a==0) and (lb==0):      # using the lower bound number didn't work
                continue
            if (a==0) and (lb==1):      # using the higher bound didn't work either
                A.append('0'); B.append('-')
                return 10000000001, A, B        # 10000000001 means it didn't work, return to previous step
            A.append(a) # Move forward with a which is a power of 3 number
            if (x>=0):
                x-=a
                B.append('R')
            else:
                x+=a
                B.append('L')
            [x, A, B] = calc_weights(x, A, B)       # Recursion

            if x==10000000001:       # The last value added, didn't work
                A = list(Aorig)
                B = list(Borig)
                x = xorig
            if x==0:
                return x, A, B
            else:
                if lb==1:
                    return 10000000001, A, B
    else:
        if x==0:
            return x, A, B
        else:
            if 1 in A:
                A.append(0)
                B.append('-')
                #print 'return 1'
                return 10000000001, A, B
            elif x==1:
                A.append(1)
                B.append('R')
                return 0, A, B
            elif x==-1:
                A.append(1)
                B.append('L')
                return 0, A, B



def answer(x):
    # your code here
    [xp, A, B] = calc_weights(x, [], [])
    if xp != 0:
        print '**************************************** BAD', xp, A, B
    i = 1
    j = 0
    out=[]
    while i <= max(A):
        out.append('-')
        if i in A:
            out[j] = B[A.index(i)]
        j += 1
        i = i * 3
    return out

# Out is the required output of the code. I check again to see if the functions worked fine. 
# If there's no input on screen, everything worked fine. Here I check x=1 to 20000    
for x in range(1, 20000):
    out = answer(x)
    xp = x
    for i in range(0, len(out)):
        if out[i]=='R':
            xp -= 3**i
        if out[i]=='L':
            xp += 3**i
        #print xp
    if (xp!=0):
        print x, xp, out

    #print out

0 个答案:

没有答案