Aiuto per Swapping Digits (ois_swappingdigits)

Ciao a tutti, ho bisogno di aiuto per risolvere il problema Swapping Digits.

Ho provato a risolvere guardando le ultime due cifre se finiscono con 25, 50, 75 o 00. E se possibile comunque il qualche modo arrivarci e prendo il minor numero di mosse. Inoltre tengo conto anche che il primo numero non deve essere 0.

Ecco il mio codice:

#!/usr/bin/env python3
# NOTE: it is recommended to use this even if you don't understand the following code.

import sys

# uncomment the two following lines if you want to read/write from files
# sys.stdin = open('input.txt')
# sys.stdout = open('output.txt', 'w')
    
def isPossible(N, number):
    last = N[-2:]

    if last == number:
        return 0
    
    yes = [False, False]

    yes[1] = N[-1] == number[-1]
    yes[0] = N[-2] == number[-2]

    if N[-1] == number[-2] and N[-2] == number[-1]:
        return 1

    swap = 0

    for i in range(len(N)):
        use = False

        if N[i] == number[-2] and not yes[0]:
            if i == 0 and N[-2] == '0':
                pass
            else:
                swap += 1
                yes[0] = True

                use = True
        
        if N[i] == number[-1] and not yes[1] and not use:
            if i == 0 and N[-1] == '0':
                pass
            else:
                swap += 1
                yes[1] = True

    if yes[0] and yes[1]:
        return swap
    else:
        return 3
    

T = int(input().strip())
for test in range(1, T+1):
    N = input().strip()

    ans = -1

    if len(N) > 1:
        ans1 = isPossible(N, '25')
        ans2 = isPossible(N, '50')
        ans3 = isPossible(N, '75')
        ans4 = isPossible(N, '00')
        
        ans = min(ans1, min(ans2, min(ans3, ans4)))

        if ans > 2:
            ans = -1

    print(ans)

sys.stdout.close()

Grazie mille in anticipo!

Vedo due problemi:

  • Il primo è che hai letto male la condizione sugli zeri: non puoi inventarti zeri che non sono dati esplicitamente in input ma puoi invece spostare uno zero all’inizio (come fa nel testo: 20005 → 00025)
  • Il secondo te lo lascio capire da te: vedi cosa succede con il numero 101.

Questo è tutto quello che ho trovato, spero non ci sia altro.

grazie mille per l’aiuto. L’errore stava proprio nel caso ‘00’, c’era il rischio che usavo due volte lo stesso zero.