Here is the reverse game where the object is to flip the initial numbers in a random sequence of 1..9 repeatedly until they are in order.

```
```import random

numbers = random.sample(range(1,10), 9)

steps = 0

**while** numbers != sorted(numbers):

**print** " ".join(map(str, numbers))

nflip = int(raw_input("Flip how many? "))

numbers[:nflip] = reversed(numbers[:nflip])

steps += 1

**print** "Finished in %d steps." % steps

""" sample run...

In [6]: run finished/game.py

2 3 4 5 9 7 6 1 8

Flip how many? 4

5 4 3 2 9 7 6 1 8

Flip how many? 6

7 9 2 3 4 5 6 1 8

Flip how many? 8

1 6 5 4 3 2 9 7 8

Flip how many? 5

3 4 5 6 1 2 9 7 8

Flip how many? 7

9 2 1 6 5 4 3 7 8

Flip how many? 9

8 7 3 4 5 6 1 2 9

Flip how many? 8

2 1 6 5 4 3 7 8 9

Flip how many? 3

6 1 2 5 4 3 7 8 9

Flip how many? 6

3 4 5 2 1 6 7 8 9

Flip how many? 3

5 4 3 2 1 6 7 8 9

Flip how many? 5

Finished in 11 steps.

"""

Advertisements

## Leave a Reply