1

I'm solving a problem where you convert a base-10 positive integer between 0 and 1000000000 to a balanced ternary form as part of Google's code challenge. It seems to work for various cases I've tested under the constraint, but it fails tests 4 and 5 for some reason. Can you spot what's wrong with this?

def answer(binary):

    # Convert to ternary
    ternary = convertToTernary(binary)

    # Conver to balanced ternary
    balancedTernary = balance(ternary)

    return balancedTernary

def balance(tForm):
    tForm.append(0)
    bTernary = tForm

    i = 0;
    N = len(tForm)
    for i in range(N - 1):
        if tForm[i] == 2:
            tForm[i] = 'L'  # Convert to custom convention now
            tForm[i+1] += 1
        elif tForm[i] > 2:
            tForm[i] = 0
            tForm[i+1] += 1

    # Apply custom convention to the rest of the coefficients
    for i in range(len(tForm)):
        if tForm[i] == 0:
            tForm[i] = '-'
        elif tForm[i] == 1:
            tForm[i] = 'R'
        else:
            tForm[i] = 'L'

    return bTernary

def convertToTernary(binary):
    tForm = []

    while binary > 0:
        tForm.append(binary % 3)
        binary /= 3

    return tForm
laketuna
  • 3,832
  • 14
  • 59
  • 104
  • 6
    Tell us when this challenge is over, so that we don't spoil it. – fjardon Sep 11 '15 at 07:13
  • Please include the failing test cases you mention, or at the very least link to them! – AakashM Sep 11 '15 at 08:07
  • There's no indication of exactly what failed but only that it failed tests 4 and 5 out of 5 total. – laketuna Sep 11 '15 at 08:08
  • 1
    You should include the problem details in your post, especially when (as here) the site is not publicly accessible – AakashM Sep 11 '15 at 09:02
  • I've tested it against my 10 liner and it gives the same answers. Maybe the problem lies in the output format? It seems that you precede the meaningful answer with 0's or -'s. – Piotr Jaszkowski Sep 11 '15 at 09:30
  • for the record, it not a base-10 int... it's "just" an integer but you sniffed right it's about converting to balanced ternary – Nas Banov Mar 24 '16 at 21:07
  • Point us to where we can challenge the challenge –  Jan 21 '17 at 05:10

0 Answers0