Guest User

Untitled

a guest
Feb 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. )
  7.  
  8. type RuneSlice []rune
  9.  
  10. func (p RuneSlice) Len() int { return len(p) }
  11. func (p RuneSlice) Less(i, j int) bool { return p[i] < p[j] }
  12. func (p RuneSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  13.  
  14. func isPermutation(str1, str2 string) bool {
  15. // if lengths are not equal - false
  16. if len(str1) == len(str2) {
  17. return false
  18. }
  19.  
  20. // sort both strings/runes
  21. var rune1 RuneSlice = []rune(str1)
  22. var rune2 RuneSlice = []rune(str2)
  23.  
  24. sort.Sort(rune1)
  25. sort.Sort(rune2)
  26.  
  27. //fmt.Println(string(rune1[:]))
  28. //fmt.Println(string(rune2[:]))
  29.  
  30. // compare rune1 and rune 2 by indexes
  31. for i := 0; i < len(rune1); i++ {
  32. if rune1[i] != rune2[i] {
  33. return false
  34. }
  35. }
  36.  
  37. return true
  38. }
  39.  
  40. func main() {
  41. fmt.Println("18,19,20,21 and 21,20,18,20,19,18,20 is permutation of each other : ", isPermutation("18,19,20,21", "21,20,18,20,19,18,20"))
  42. fmt.Println("18,19,20,21 and 21,20,18,20,18,20 is permutation of each other : ", isPermutation("18,19,20,21", "21,20,18,20,18,20"))
  43.  
  44. }
  45.  
  46. // A pumpung is a permutation of consecutive integers, possibly with repeated items.
  47. // For example, [21, 20, 18, 20, 19, 18, 20] is pumpung
  48. // since it is a permutation of [18, …, 21] with 18 and 20 repeated.
  49. // However, [21, 20, 18, 20, 18, 20] is not.
  50.  
  51. package main
  52.  
  53. import "fmt"
  54.  
  55. const (
  56. maxInt = int(^uint(0) >> 1)
  57. minInt = -maxInt - 1
  58. )
  59.  
  60. func isPumpung(a []int) (min, max int, pumpung bool) {
  61. min, max = maxInt, minInt
  62. p := make(map[int]bool)
  63. for _, e := range a {
  64. p[e] = true
  65. if min > e {
  66. min = e
  67. }
  68. if max < e {
  69. max = e
  70. }
  71. }
  72. pumpung = max-min+1 == len(p)
  73. if !pumpung {
  74. min, max = 0, 0
  75. }
  76. return min, max, pumpung
  77. }
  78.  
  79. func isPumpungEqual(a1, a2 []int) bool {
  80. min1, max1, pumpung1 := isPumpung(a1)
  81. if !pumpung1 {
  82. return false
  83. }
  84. min2, max2, pumpung2 := isPumpung(a2)
  85. if !pumpung2 {
  86. return false
  87. }
  88. return min1 == min2 && max1 == max2
  89. }
  90.  
  91. func main() {
  92. a1 := []int{18, 19, 20, 21}
  93. fmt.Print(a1, " ")
  94. fmt.Println(isPumpung(a1))
  95. a2 := []int{21, 20, 18, 20, 18, 20}
  96. fmt.Print(a2, " ")
  97. fmt.Println(isPumpung(a2))
  98. a3 := []int{21, 20, 18, 20, 19, 18, 20}
  99. fmt.Print(a3, " ")
  100. fmt.Println(isPumpung(a3))
  101.  
  102. fmt.Println()
  103. fmt.Println(a1, a2, isPumpungEqual(a1, a2))
  104. fmt.Println(a1, a3, isPumpungEqual(a1, a3))
  105. }
  106.  
  107. [18 19 20 21] 18 21 true
  108. [21 20 18 20 18 20] 0 0 false
  109. [21 20 18 20 19 18 20] 18 21 true
  110.  
  111. [18 19 20 21] [21 20 18 20 18 20] false
  112. [18 19 20 21] [21 20 18 20 19 18 20] true
Add Comment
Please, Sign In to add comment