k.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package kata
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "unicode"
  7. )
  8. type count map[rune]int
  9. func (c count) String() (s string) {
  10. var sl []string
  11. for r := 'a'; r <= 'z'; r++ {
  12. n, ok := c[r]
  13. if ok {
  14. sl = append(sl, fmt.Sprintf("%c: %d", r, n))
  15. }
  16. }
  17. s = "[" + strings.Join(sl, ", ") + "]"
  18. return s
  19. }
  20. func (c count) ascending() []string {
  21. res := make([]string, len(c))
  22. i := 0
  23. for r, n := range c {
  24. res[i] = string(rune(n)) + string(r)
  25. i++
  26. }
  27. sort.Strings(res)
  28. return res
  29. }
  30. func newCount() count {
  31. c := make(count, 26)
  32. for r := 'a'; r <= 'z'; r++ {
  33. c[r] = 0
  34. }
  35. return c
  36. }
  37. func (c count) ingest(s string) {
  38. // Count letters.
  39. for _, r := range s {
  40. if unicode.IsLower(r) {
  41. c[r]++
  42. }
  43. }
  44. // Remove non-repeated letters and order.
  45. for r := 'a'; r <= 'z'; r++ {
  46. occurrences := c[r]
  47. if occurrences <= 1 {
  48. delete(c, r)
  49. continue
  50. }
  51. }
  52. }
  53. type hitList map[int]count
  54. func (h hitList) descendingKeys() []int {
  55. keys:= make([]int, len(h))
  56. i := 0
  57. for k := range h {
  58. keys[i] = k
  59. i++
  60. }
  61. sort.Sort(sort.Reverse(sort.IntSlice(keys)))
  62. return keys
  63. }
  64. func (h hitList) String() string {
  65. var sl []string
  66. for n, c := range h {
  67. sl = append(sl, fmt.Sprintf("%d: %s", n, c))
  68. }
  69. res := "[" + strings.Join(sl, ", ") + "]"
  70. return res
  71. }
  72. // Cull removes from each count entries not at least equal to the one in the other count.
  73. func cull(c1, c2 count) (r1, r2 count) {
  74. r1, r2 = count{}, count{}
  75. for r, n1 := range c1 {
  76. n2, ok := c2[r]
  77. if !ok || n1 >= n2 {
  78. r1[r] = n1
  79. }
  80. }
  81. for r, n2 := range c2 {
  82. n1, ok := c1[r]
  83. if !ok || n2 >= n1 {
  84. r2[r] = n2
  85. }
  86. }
  87. return
  88. }
  89. // Invert build an inverse count, assuming already culled counts.
  90. func invert(c1, c2 count) hitList {
  91. union := hitList{}
  92. for r, n := range c1 {
  93. if _, ok := union[n]; !ok {
  94. union[n] = count{}
  95. }
  96. // On the first string, every letter is missing, so just initialize.
  97. union[n][r] = '1'
  98. }
  99. for r, n := range c2 {
  100. if _, ok := union[n]; !ok {
  101. union[n] = count{}
  102. }
  103. // On any subsequent string, letters may exist or not, so be careful.
  104. _, ok := union[n][r]
  105. if !ok {
  106. union[n][r] = '2'
  107. } else {
  108. union[n][r] = '='
  109. }
  110. }
  111. return union
  112. }
  113. func Mix(s1, s2 string) string {
  114. c1, c2 := newCount(), newCount()
  115. c1.ingest(s1)
  116. c2.ingest(s2)
  117. c1, c2 = cull(c1, c2)
  118. union := invert(c1, c2)
  119. var sl []string
  120. for _, n := range union.descendingKeys() {
  121. for _, hits := range union[n].ascending() {
  122. source, r := hits[0], hits[1]
  123. sl = append(sl, fmt.Sprintf("%c:%s", source, strings.Repeat(string(r), n)))
  124. }
  125. }
  126. s := strings.Join(sl, "/")
  127. return s
  128. }