Home > Enterprise >  Google kickstart 2022 why does line 11 of my code have an error
Google kickstart 2022 why does line 11 of my code have an error

Time:03-21

Problem Ada gives John a positive integer N. She challenges him to construct a new number (without leading zeros), that is a multiple of 9, by inserting exactly one digit (0 … 9) anywhere in the given number N. It is guaranteed that N does not have any leading zeros.

As John prefers smaller numbers, he wants to construct the smallest such number possible. Can you help John?

Input The first line of the input gives the number of test cases, T. T test cases follow.

Each test case has a single line containing a positive integer N: the number Ada gives John.

Output For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the new number constructed by John. As mentioned earlier, y cannot have leading zeros.

In line 11 of my code below, there seems to be an error but I cannot fix it, pls help

T=int(input())
for x in range(T):
    num=input()
    nums=num.split()
    alpha=num.split()
    no=len(nums)
    values=[]
    digit=0
    for z in range(1,10):
        alpha.append(z)
        gamma=int("".join(alpha))
        alpha=alpha.[:-1]
        if gamma%9==0:
            digit=z
    for i in range(no):
        beta=nums.insert(i 1, digit)
        values.append(beta)
    print("Case#" str(x 1) ": " min(values))  ``` 

CodePudding user response:

alpha.[:-1] is causing the error, the '.' shouldn't be there. it should be alpha[:-1] instead. Also, you can find the number you have to add by 9-(num%9), you don't need lists to begin with, then you can determine it's position by putting it just before the number strictly greater than it, if no greater number is found, place it last. Like so

T=int(input())
for x in range(T):
    num=input();
    tobeadded = 9 - ( int(num) % 9 );
    for i in range(len(num)):
        if tobeadded < int(num[i]):
            num = num[:i]   str(tobeadded)   num[i:];
            break;
        elif i == len(num) - 1:
            num =str(tobeadded);
    print("Case#",x 1,":",num);
  • Related