# Aiuto per Swapping Digits (ois\_swappingdigits)

**URL:** <https://forum.olinfo.it/t/aiuto-per-swapping-digits-ois-swappingdigits/9998>\
**Category:** Aiuto\
**Created:** [11 Giugno 2026, 9:48am UTC](https://forum.olinfo.it/t/aiuto-per-swapping-digits-ois-swappingdigits/9998 "2026-06-11T09:48:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GNCNK](https://forum.olinfo.it/user_avatar/forum.olinfo.it/gncnk/32/2751_2.png) [@GNCNK](https://forum.olinfo.it/u/GNCNK)\
**Post date:** [11 Giugno 2026, 9:48am UTC](https://forum.olinfo.it/t/aiuto-per-swapping-digits-ois-swappingdigits/9998/1 "2026-06-11T09:48:37Z")

</div>

Ciao a tutti, ho bisogno di aiuto per risolvere il problema [Swapping Digits](https://training.olinfo.it/task/ois_swappingdigits).

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:

```python
#!/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!

---

<div class="post-metadata">

**Author:** ![BestCrazyNoob](https://forum.olinfo.it/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@BestCrazyNoob](https://forum.olinfo.it/u/BestCrazyNoob)\
**Post date:** [11 Giugno 2026, 9:34pm UTC](https://forum.olinfo.it/t/aiuto-per-swapping-digits-ois-swappingdigits/9998/2 "2026-06-11T21:34:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GNCNK](https://forum.olinfo.it/user_avatar/forum.olinfo.it/gncnk/32/2751_2.png) [@GNCNK](https://forum.olinfo.it/u/GNCNK)\
**Post date:** [12 Giugno 2026, 7:28am UTC](https://forum.olinfo.it/t/aiuto-per-swapping-digits-ois-swappingdigits/9998/3 "2026-06-12T07:28:24Z")

</div>

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