Number reversal game: Difference between revisions

Line 3,333:
goto [loop]
end</lang>
 
=={{header|Rust}}==
{{libheader|rand 0.7.3}}
<lang Rust>use rand::prelude::*;
use std::io::stdin;
 
fn is_sorted(seq: &[impl PartialOrd]) -> bool {
if seq.len() < 2 {
return true;
}
// Look for any instance where the previous number is larger than the following
!seq.iter()
.zip(seq[1..].iter())
.any(|(prev, foll)| prev > foll)
}
 
fn main() {
println!(
"Number reversal game:
Given a jumbled list of the numbers 1 to 9, put the numbers in order.
Each attempt you can reverse up to n digits starting from the left.
The score is the count of the reversals needed to attain the ascending order."
);
let mut rng = thread_rng();
 
let mut sequence: Vec<u8> = (1..10).collect();
while is_sorted(&sequence) {
sequence.shuffle(&mut rng);
}
 
let mut attempt = 1;
while !is_sorted(&sequence) {
println!(
"Attempt {}: {:?} - How many numbers do you want to flip?",
attempt, sequence
);
let flip = {
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
input.trim().parse().unwrap()
};
sequence[..flip].reverse();
attempt += 1;
}
println!(
"Congrats! It took you {} attempts to sort the sequence.",
attempt - 1 // Remove additionally counted attempt
);
}</lang>
{{out}}
<pre>Number reversal game:
Given a jumbled list of the numbers 1 to 9, put the numbers in order.
Each attempt you can reverse up to n digits starting from the left.
The score is the count of the reversals needed to attain the ascending order.
Attempt 1: [7, 1, 6, 2, 3, 4, 5, 8, 9] - How many numbers do you want to flip?
7
Attempt 2: [5, 4, 3, 2, 6, 1, 7, 8, 9] - How many numbers do you want to flip?
5
Attempt 3: [6, 2, 3, 4, 5, 1, 7, 8, 9] - How many numbers do you want to flip?
6
Attempt 4: [1, 5, 4, 3, 2, 6, 7, 8, 9] - How many numbers do you want to flip?
2
Attempt 5: [5, 1, 4, 3, 2, 6, 7, 8, 9] - How many numbers do you want to flip?
5
Attempt 6: [2, 3, 4, 1, 5, 6, 7, 8, 9] - How many numbers do you want to flip?
3
Attempt 7: [4, 3, 2, 1, 5, 6, 7, 8, 9] - How many numbers do you want to flip?
4
Congrats! It took you 7 attempts to sort the sequence.</pre>
 
 
 
=={{header|Scala}}==
Anonymous user