Jump to content

Best shuffle: Difference between revisions

m (→‎{{header|REXX}}: added/changed whitespace and comments, changed indentations.)
Line 1,264:
#define extensions.
 
#class(extension)op
#symbol shuffle = (:aLiteral)
{
[
#method shuffled
#var aLength := aLiteral length.
#var aShuffled := convertor toArray:aLiteral.
 
control seek &int:0 &int:(aLength-1) &every: (&index:i)
[
#var aCharanOriginal := aLiteral@iself toArray.
(aChar#var =aShuffled := (aShuffled@i))self ?toArray.
].
control run &int: 0 &intto:(anOriginal length - 1) &forEverydoEach: (&index:i)
[
control run &int:0 &intto:(aLengthanOriginal length - 1) &forEverydoEach: (&index:j)
[
#var((i anAltChar!= 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.
aShuffled~indexable exchange:i:j].
^ true.
].
].
].
^ false.
].
 
^ control run:aShuffled &forEach:(Summing new:(String new)) literal.
].
 
#symbol scoreShuffle = (:anOriginal:aShuffled)
[
#var aScore := Integer new.
^ control run:aShuffled &forEach:(Summing newsummarize:(String new)) literal.
control run &int:0 &int:(anOriginal length - 1) &forEvery: (&index:i)
]
[ ((anOriginal @ i) == (aShuffled @ i)) ? [ aScore += 1. ]. ].
#method score : anOriginalText
^ aScore value.
[
].
#var aShuffled := convertorself toArray:aLiteral.
#var anOriginal := anOriginalText toArray.
#var aScore := Integer new.
 
0 to:(anOriginal length - 1) &doEach: (:i)
[ ((anOriginal @ i) == (aShuffled @ i)) ? [ aScore += 1. ]. ].
^ falseaScore value.
]
}
 
#symbol program =
[
control run:("abracadabra", "seesaw", "grrrrrr", "pop", "up", "a") run &forEacheach: aWord
[
#var aShuffled := shuffle:aWord shuffled.
 
consoleExconsole writeLine:"The best shuffle of ":aWord:" is ":aShuffled:"(":(scoreShuffleaShuffled score:aWord:aShuffled):")".
].
 
console readChar.
].</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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.