Best shuffle: Difference between revisions

Content added Content deleted
m (punctuation)
(Go solution)
Line 931: Line 931:
, , (0)
, , (0)
xxxxx, xxxxx, (5)</pre>
xxxxx, xxxxx, (5)</pre>

=={{header|Go}}==
{{trans|Icon and Unicon}}
<lang go>package main

import (
"fmt"
"rand"
"time"
)

var ts = []string{"abracadabra", "seesaw", "elk", "grrrrrr", "up", "a"}

func main() {
rand.Seed(time.Nanoseconds())
for _, s := range ts {
// create shuffled byte array of original string
t := make([]byte, len(s))
for i, r := range rand.Perm(len(s)) {
t[i] = s[r]
}
// algorithm of Icon solution
for i := 0; i < len(s); i++ {
for j := 0; j < len(s); j++ {
if i != j && t[i] != s[j] && t[j] != s[i] {
t[i], t[j] = t[j], t[i]
break
}
}
}
// count unchanged and output
var count int
for i, ic := range t {
if ic == s[i] {
count++
}
}
fmt.Printf("%s -> %s (%d)\n", s, string(t), count)
}
}</lang>
Output:
<pre>
abracadabra -> raaracbbaad (0)
seesaw -> asswee (0)
elk -> lke (0)
grrrrrr -> rgrrrrr (5)
up -> pu (0)
a -> a (1)
</pre>


=={{header|Haskell}}==
=={{header|Haskell}}==