Number reversal game: Difference between revisions

From Rosetta Code
Content added Content deleted
(New task and Python solution.)
 
(Add Factor)
Line 8: Line 8:


Note: Assume the players input does not need extra validation.
Note: Assume the players input does not need extra validation.

=={{header|Factor}}==
<lang factor>USING: formatting io kernel math math.parser math.ranges
namespaces random sequences strings ;
IN: rosetta.number-reversal

: make-jumbled-array ( -- sorted jumbled )
CHAR: 1 CHAR: 9 [a,b] [ 1string ] map dup clone randomize
[ 2dup = ] [ randomize ] while ;

SYMBOL: trials

: prompt ( jumbled -- n )
trials get "#%2d: " printf
", " join write
" Flip how many? " write flush
readln string>number ;

: ask ( jumbled -- n )
dup prompt [
dup 9 <= [ nip ] [ drop ask ] if
] [ ask ] if* ;

: game-loop ( sorted jumbled -- )
2dup = [
2drop trials get
"\nYou took %d attempts to put the digits in order!\n" printf
flush
] [
trials [ 1 + ] change
dup dup ask head-slice reverse! drop
game-loop
] if ;

: play ( -- )
0 trials set
make-jumbled-array game-loop ;</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 20:21, 3 April 2010

Task
Number reversal game
You are encouraged to solve this task according to the task description, using any language you may know.

Given a jumbled list of the numbers 1 to 9, show the list then ask the player how many digits from the left to reverse. Reverse those digits, then ask again, until all the digits end up in ascending order.

The score is the count of the reversals needed to attain the ascending order.

Note: Assume the players input does not need extra validation.

Factor

<lang factor>USING: formatting io kernel math math.parser math.ranges namespaces random sequences strings ; IN: rosetta.number-reversal

make-jumbled-array ( -- sorted jumbled )
   CHAR: 1 CHAR: 9 [a,b] [ 1string ] map dup clone randomize
   [ 2dup = ] [ randomize ] while ;

SYMBOL: trials

prompt ( jumbled -- n )
   trials get "#%2d: " printf
   ", " join write
   "   Flip how many? " write flush
   readln string>number ;
ask ( jumbled -- n )
   dup prompt [
       dup 9 <= [ nip ] [ drop ask ] if
   ] [ ask ] if* ;
game-loop ( sorted jumbled -- )
   2dup = [
       2drop trials get
       "\nYou took %d attempts to put the digits in order!\n" printf
       flush
   ] [
       trials [ 1 + ] change
       dup dup ask head-slice reverse! drop
       game-loop
   ] if ;
play ( -- )
   0 trials set
   make-jumbled-array game-loop ;</lang>

Python

<lang python> number reversal game

   Given a jumbled list of the numbers 1 to 9
   Show the list.
   Ask the player how many digits from the left to reverse.
   Reverse those digits then ask again.
   until all the digits end up in ascending order.

import random

print(__doc__) data, trials = list('123456789'), 0 while data == sorted(data):

   random.shuffle(data)

while data != sorted(data):

   trials += 1
   flip = int(input('#%2i: LIST: %r Flip how many?: ' % (trials, data)))
   data[:flip] = reversed(data[:flip])

print('\nYou took %2i attempts to put the digits in order!' % trials)</lang>

Sample output:

number reversal game
    Given a jumbled list of the numbers 1 to 9
    Show the list.
    Ask the player how many digits from the left to reverse.
    Reverse those digits then ask again.
    until all the digits end up in ascending order.


# 1: LIST: ['3', '6', '9', '2', '8', '1', '7', '4', '5'] Flip how many?: 6
# 2: LIST: ['1', '8', '2', '9', '6', '3', '7', '4', '5'] Flip how many?: 9
# 3: LIST: ['5', '4', '7', '3', '6', '9', '2', '8', '1'] Flip how many?: 7
# 4: LIST: ['2', '9', '6', '3', '7', '4', '5', '8', '1'] Flip how many?: 8
# 5: LIST: ['8', '5', '4', '7', '3', '6', '9', '2', '1'] Flip how many?: 5
# 6: LIST: ['3', '7', '4', '5', '8', '6', '9', '2', '1'] Flip how many?: 7
# 7: LIST: ['9', '6', '8', '5', '4', '7', '3', '2', '1'] Flip how many?: 5
# 8: LIST: ['4', '5', '8', '6', '9', '7', '3', '2', '1'] Flip how many?: 6
# 9: LIST: ['7', '9', '6', '8', '5', '4', '3', '2', '1'] Flip how many?: 3
#10: LIST: ['6', '9', '7', '8', '5', '4', '3', '2', '1'] Flip how many?: 4
#11: LIST: ['8', '7', '9', '6', '5', '4', '3', '2', '1'] Flip how many?: 2
#12: LIST: ['7', '8', '9', '6', '5', '4', '3', '2', '1'] Flip how many?: 3
#13: LIST: ['9', '8', '7', '6', '5', '4', '3', '2', '1'] Flip how many?: 9

You took 13 attempts to put the digits in order!