Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Number-reversal-game/Erlang/number-reversal-game.erl
Normal file
23
Task/Number-reversal-game/Erlang/number-reversal-game.erl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
-module( number_reversal_game ).
|
||||
|
||||
-export( [task/0, start/1] ).
|
||||
|
||||
start( N ) when N > 1 ->
|
||||
io:fwrite( "Usage: ~s~n", [usage(N)] ),
|
||||
Targets = lists:seq( 1, N ),
|
||||
Jumbleds = [X||{_,X} <- lists:sort([ {random:uniform(), Y} || Y <- Targets])],
|
||||
Attempt = loop( Targets, Jumbleds, 0 ),
|
||||
io:fwrite( "Numbers sorted in ~p atttempts~n", [Attempt] ).
|
||||
|
||||
task() -> start( 9 ).
|
||||
|
||||
|
||||
|
||||
loop( Targets, Targets, Attempt ) -> Attempt;
|
||||
loop( Targets, Jumbleds, Attempt ) ->
|
||||
io:fwrite( "~p~n", [Jumbleds] ),
|
||||
{ok,[N]} = io:fread( "How many digits from the left to reverse? ", "~d" ),
|
||||
{Lefts, Rights} = lists:split( N, Jumbleds ),
|
||||
loop( Targets, lists:reverse(Lefts) ++ Rights, Attempt + 1 ).
|
||||
|
||||
usage(N) -> io_lib:format( "Given a jumbled list of the numbers 1 to ~p that are definitely not in ascending order, 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.", [N] ).
|
||||
Loading…
Add table
Add a link
Reference in a new issue