Best shuffle: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added/changed whitespace and comments, changed indentations.) |
|||
Line 1,264: | Line 1,264: | ||
#define extensions. |
#define extensions. |
||
#class(extension)op |
|||
#symbol shuffle = (:aLiteral) |
|||
{ |
|||
[ |
|||
#method shuffled |
|||
#var aLength := aLiteral length. |
|||
⚫ | |||
control seek &int:0 &int:(aLength-1) &every: (&index:i) |
|||
[ |
[ |
||
#var |
#var anOriginal := self toArray. |
||
#var aShuffled := self toArray. |
|||
⚫ | |||
⚫ | |||
[ |
[ |
||
0 to:(anOriginal length - 1) &doEach: (:j) |
|||
[ |
[ |
||
((i != j)and:[anOriginal@i != (aShuffled@j)]and:[anOriginal@j != (aShuffled@i)]) ? |
|||
⚫ | |||
((anAltChar != aChar)and:[ anAltChar != (aLiteral@i) ]and:[aChar != (aLiteral@j)]) ? |
|||
aShuffled exchange:i:j. |
|||
]. |
|||
^ true. |
|||
⚫ | |||
]. |
]. |
||
]. |
]. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
]. |
|||
#symbol scoreShuffle = (:anOriginal:aShuffled) |
|||
[ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
] |
|||
⚫ | |||
#method score : anOriginalText |
|||
^ aScore value. |
|||
[ |
|||
]. |
|||
⚫ | |||
#var anOriginal := anOriginalText toArray. |
|||
⚫ | |||
0 to:(anOriginal length - 1) &doEach: (:i) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
] |
|||
} |
|||
#symbol program = |
#symbol program = |
||
[ |
[ |
||
("abracadabra", "seesaw", "grrrrrr", "pop", "up", "a") run &each: aWord |
|||
[ |
[ |
||
#var aShuffled := |
#var aShuffled := aWord shuffled. |
||
console writeLine:"The best shuffle of ":aWord:" is ":aShuffled:"(":(aShuffled score:aWord):")". |
|||
]. |
]. |
||
console readChar. |
console readChar. |
||
].</lang> |
].</lang> |
||
{{out}} |
|||
<pre> |
|||
The best shuffle of abracadabra is caadrbabaar(0) |
|||
The best shuffle of seesaw is ewaess(0) |
|||
The best shuffle of grrrrrr is rgrrrrr(5) |
|||
The best shuffle of pop is opp(1) |
|||
The best shuffle of up is pu(0) |
|||
The best shuffle of a is a(1) |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |