Anonymous user
Best shuffle: Difference between revisions
→{{header|Groovy}}
(→{{header|Groovy}}: added error template) |
|||
Line 1,238:
=={{header|Groovy}}==
<lang groovy>def shuffle(text) {
def
for (sourceIndex in 0..<text.size()) {
for (destinationIndex in 0..<text.size()) {
[original: text, shuffled: list.join(""), score: score(text, list)]▼
if (shuffled[sourceIndex] != shuffled[destinationIndex] && shuffled[sourceIndex] != text[destinationIndex] && shuffled[destinationIndex] != text[sourceIndex]) {
char tmp = shuffled[sourceIndex];
shuffled[sourceIndex] = shuffled[destinationIndex];
shuffled[destinationIndex] = tmp;
}▼
}▼
}
Line 1,253 ⟶ 1,261:
}
score
▲ best = newTry
▲ }
▲ }
▲ best
}
["abracadabra", "seesaw", "elk", "grrrrrr", "up", "a"].each { text ->
def result =
println "${result.original}, ${result.shuffled}, (${result.score})"
}</lang>▼
▲</lang>
Output:
<pre>
abracadabra,
seesaw,
elk,
grrrrrr,
up, pu, (0)
a, a, (1)
|