On Tuesday, April 2, 2019 at 9:19:27 AM UTC-7, taegyun kim wrote:
> Hello Everyone,
> I am facing wrong answer in the Saving the Universe again test set 2. I can't 
> find out why. Please help me. My code is written in Python 3
> 
> Here is my code:
> 
> for j in range(int(input())):
>     result = 0
>     d, inst = input().split()
>     d = int(d)
>     inst = list(inst)
>     num_s = inst.count('S')
>     
>     # Impossible case
>     if num_s > d:
>         print("Case #{}: IMPOSSIBLE".format(j+1))
>         continue
>     
>     # Possible case
>     dic = [0]
>     i = 0
>     total = 0
> 
>     for char in inst:
>         if char == 'S':
>             total += (2**i)
>             dic[i]+=1
>         else:
>             i+=1
>             dic.append(0)
>     
>     score = total
>     while d < score:
>         if dic[-1] ==0:
>             dic.pop()
>         result +=1
>         score -= (2**(len(dic)-2))
>         dic[-1] -= 1
>         dic[-2] += 1
> 
>     print("Case #{}: {}".format(j+1,result))

Suppose there is a continuous charging without shooting in the end of the 
string, like "SCSCSCCC", the dic list you created would be [1,1,1,0,0,0]. In 
your last while loop, each loop you see the tail of the list being 0 you pop it 
ONLY ONCE without checking if the tail is still 0, which can lead to the case 
in which you still do the operation of "moving" when the last digit is 0.

My suggestion: Move the last 4 lines of the last while loop to an 'else' branch.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/e341b6c4-62ec-45df-9258-1fefc94794ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to