Task

Given a jumbled list of the numbers   1   to   9   that are definitely   not   in
ascending order.

Show the list,   and 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 player's input does not need extra validation.



Related tasks

   Sorting algorithms/Pancake sort
   Pancake sorting.
   Topswops


 
SIZE = 9
ordered = (1..SIZE).to_a
shuffled = (1..SIZE).to_a
 
while shuffled == ordered
  shuffled.shuffle!
end
 
score = 0
until shuffled == ordered
  print "#{shuffled} Enter items to reverse: "
 
  next unless guess = gets
  next unless num = guess.to_i?
  next if num < 2 || num > SIZE
 
  shuffled[0, num] = shuffled[0, num].reverse
  score += 1
end
 
puts "#{shuffled} Your score: #{score}"
 