Number reversal game: Difference between revisions

Content added Content deleted
(Mathematica)
(Updated D entry)
Line 704: Line 704:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.random, std.string, std.conv, std.algorithm;
<lang d>import std.stdio, std.random, std.string, std.conv, std.algorithm,
std.range;


void main() {
void main() {
auto data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
auto data = iota(1, 10).array();
do randomShuffle(data);
do data.randomShuffle();
while (isSorted(data));
while (data.isSorted());


int trial;
int trial;
while (!isSorted(data)) {
while (!data.isSorted()) {
writef("%d: %s How many numbers to flip? ", ++trial, data);
writef("%d: %s How many numbers to flip? ", ++trial, data);
data[0 .. to!int(strip(readln))].reverse;
data[0 .. readln().strip().to!size_t()].reverse();
}
}
writefln("\nYou took %d attempts.", trial);
writefln("\nYou took %d attempts.", trial);