Best shuffle: Difference between revisions
Content added Content deleted
(Added Wren) |
Drkameleon (talk | contribs) (Added Arturo implementation) |
||
Line 94: | Line 94: | ||
up, pu, ( 0 ) |
up, pu, ( 0 ) |
||
a, a, ( 1 )</pre> |
a, a, ( 1 )</pre> |
||
=={{header|Arturo}}== |
|||
<lang rebol> |
|||
count: function [s1 s2][ |
|||
res: 0 |
|||
loop.with:'i s1 'c [ |
|||
if c = s2\[i] -> res: res + 1 |
|||
] |
|||
return res |
|||
] |
|||
shuff: function [str]-> |
|||
join shuffle split str |
|||
bestShuffle: function [s][ |
|||
shuffled: shuff s |
|||
loop 0..dec size shuffled 'i [ |
|||
if shuffled\[i] <> s\[i] -> continue |
|||
loop 0..dec size shuffled 'j [ |
|||
if all? @[ |
|||
shuffled\[i] <> shuffled\[j] |
|||
shuffled\[i] <> s\[j] |
|||
shuffled\[j] <> s\[i] |
|||
] [ |
|||
tmp: shuffled\[i] |
|||
shuffled\[i]: shuffled\[j] |
|||
shuffled\[j]: tmp |
|||
break |
|||
] |
|||
] |
|||
] |
|||
return shuffled |
|||
] |
|||
words: ["abracadabra" "seesaw" "grrrrrr" "pop" |
|||
"up" "a" "antidisestablishmentarianism"] |
|||
loop words 'w [ |
|||
sf: bestShuffle w |
|||
print [w "->" sf "| count:" count w sf] |
|||
]</lang> |
|||
{{out}} |
|||
<pre>abracadabra -> caabararadb | count: 0 |
|||
seesaw -> esawse | count: 0 |
|||
grrrrrr -> rgrrrrr | count: 5 |
|||
pop -> opp | count: 1 |
|||
up -> pu | count: 0 |
|||
a -> a | count: 1 |
|||
antidisestablishmentarianism -> mesansrntbiissmtailihdaneait | count: 0</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |