Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Best-shuffle/Groovy/best-shuffle.groovy
Normal file
29
Task/Best-shuffle/Groovy/best-shuffle.groovy
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
def shuffle(text) {
|
||||
def shuffled = (text as List)
|
||||
for (sourceIndex in 0..<text.size()) {
|
||||
for (destinationIndex in 0..<text.size()) {
|
||||
if (shuffled[sourceIndex] != shuffled[destinationIndex] && shuffled[sourceIndex] != text[destinationIndex] && shuffled[destinationIndex] != text[sourceIndex]) {
|
||||
char tmp = shuffled[sourceIndex];
|
||||
shuffled[sourceIndex] = shuffled[destinationIndex];
|
||||
shuffled[destinationIndex] = tmp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
[original: text, shuffled: shuffled.join(""), score: score(text, shuffled)]
|
||||
}
|
||||
|
||||
def score(original, shuffled) {
|
||||
int score = 0
|
||||
original.eachWithIndex { character, index ->
|
||||
if (character == shuffled[index]) {
|
||||
score++
|
||||
}
|
||||
}
|
||||
score
|
||||
}
|
||||
|
||||
["abracadabra", "seesaw", "elk", "grrrrrr", "up", "a"].each { text ->
|
||||
def result = shuffle(text)
|
||||
println "${result.original}, ${result.shuffled}, (${result.score})"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue