fork download
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. )
  8.  
  9. func bogosort(scrambled, sorted string) int {
  10. attempts := 0
  11. ch := make(chan int)
  12.  
  13. sort_routine := func(ch chan int) {
  14. rand.Seed(time.Now().UnixNano())
  15. sort_attempt := ""
  16. perm := rand.Perm(len(scrambled))
  17.  
  18. for _, rand_pos := range perm {
  19. sort_attempt += string(scrambled[rand_pos])
  20. }
  21.  
  22. if sort_attempt == sorted {
  23. ch <- 1
  24. } else {
  25. ch <- 0
  26. }
  27. }
  28.  
  29. go sort_routine(ch)
  30. for result := range ch {
  31. if result == 0 {
  32. attempts++
  33. go sort_routine(ch)
  34. } else {
  35. close(ch)
  36. }
  37. }
  38. return attempts
  39. }
  40.  
  41. func main() {
  42. attempts := bogosort("lolhe", "hello")
  43. fmt.Printf("%v iterations", attempts)
  44. }
Success #stdin #stdout 0s 420672KB
stdin
Standard input is empty
stdout
55 iterations