talgorithm.nim 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. discard """
  2. targets: "c js"
  3. matrix: "--mm:refc; --mm:orc"
  4. output:'''@["3", "2", "1"]
  5. '''
  6. """
  7. #12928,10456
  8. import std/[sequtils, algorithm, json, sugar]
  9. import std/assertions
  10. proc test() =
  11. try:
  12. let info = parseJson("""
  13. {"a": ["1", "2", "3"]}
  14. """)
  15. let prefixes = info["a"].getElems().mapIt(it.getStr()).sortedByIt(it).reversed()
  16. echo prefixes
  17. except:
  18. discard
  19. test()
  20. block:
  21. # Tests for lowerBound
  22. var arr = @[1, 2, 3, 5, 6, 7, 8, 9]
  23. doAssert arr.lowerBound(0) == 0
  24. doAssert arr.lowerBound(4) == 3
  25. doAssert arr.lowerBound(5) == 3
  26. doAssert arr.lowerBound(10) == 8
  27. arr = @[1, 5, 10]
  28. doAssert arr.lowerBound(4) == 1
  29. doAssert arr.lowerBound(5) == 1
  30. doAssert arr.lowerBound(6) == 2
  31. # Tests for isSorted
  32. var srt1 = [1, 2, 3, 4, 4, 4, 4, 5]
  33. var srt2 = ["iello", "hello"]
  34. var srt3 = [1.0, 1.0, 1.0]
  35. var srt4: seq[int]
  36. doAssert srt1.isSorted(cmp) == true
  37. doAssert srt2.isSorted(cmp) == false
  38. doAssert srt3.isSorted(cmp) == true
  39. doAssert srt4.isSorted(cmp) == true
  40. var srtseq = newSeq[int]()
  41. doAssert srtseq.isSorted(cmp) == true
  42. # Tests for reversed
  43. var arr1 = @[0, 1, 2, 3, 4]
  44. doAssert arr1.reversed() == @[4, 3, 2, 1, 0]
  45. for i in 0 .. high(arr1):
  46. doAssert arr1.reversed(0, i) == arr1.reversed()[high(arr1) - i .. high(arr1)]
  47. doAssert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i]
  48. block:
  49. var list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  50. let list2 = list.rotatedLeft(1 ..< 9, 3)
  51. let expected = [0, 4, 5, 6, 7, 8, 1, 2, 3, 9, 10]
  52. doAssert list.rotateLeft(1 ..< 9, 3) == 6
  53. doAssert list == expected
  54. doAssert list2 == @expected
  55. var s0, s1, s2, s3, s4, s5 = "xxxabcdefgxxx"
  56. doAssert s0.rotateLeft(3 ..< 10, 3) == 7
  57. doAssert s0 == "xxxdefgabcxxx"
  58. doAssert s1.rotateLeft(3 ..< 10, 2) == 8
  59. doAssert s1 == "xxxcdefgabxxx"
  60. doAssert s2.rotateLeft(3 ..< 10, 4) == 6
  61. doAssert s2 == "xxxefgabcdxxx"
  62. doAssert s3.rotateLeft(3 ..< 10, -3) == 6
  63. doAssert s3 == "xxxefgabcdxxx"
  64. doAssert s4.rotateLeft(3 ..< 10, -10) == 6
  65. doAssert s4 == "xxxefgabcdxxx"
  66. doAssert s5.rotateLeft(3 ..< 10, 11) == 6
  67. doAssert s5 == "xxxefgabcdxxx"
  68. block product:
  69. doAssert product(newSeq[seq[int]]()) == newSeq[seq[int]](), "empty input"
  70. doAssert product(@[newSeq[int](), @[], @[]]) == newSeq[seq[int]](), "bit more empty input"
  71. doAssert product(@[@[1, 2]]) == @[@[1, 2]], "a simple case of one element"
  72. doAssert product(@[@[1, 2], @[3, 4]]) == @[@[2, 4], @[1, 4], @[2, 3], @[1,
  73. 3]], "two elements"
  74. doAssert product(@[@[1, 2], @[3, 4], @[5, 6]]) == @[@[2, 4, 6], @[1, 4, 6],
  75. @[2, 3, 6], @[1, 3, 6], @[2, 4, 5], @[1, 4, 5], @[2, 3, 5], @[1, 3, 5]], "three elements"
  76. doAssert product(@[@[1, 2], @[]]) == newSeq[seq[int]](), "two elements, but one empty"
  77. block lowerBound:
  78. doAssert lowerBound([1, 2, 4], 3, system.cmp[int]) == 2
  79. doAssert lowerBound([1, 2, 2, 3], 4, system.cmp[int]) == 4
  80. doAssert lowerBound([1, 2, 3, 10], 11) == 4
  81. block upperBound:
  82. doAssert upperBound([1, 2, 4], 3, system.cmp[int]) == 2
  83. doAssert upperBound([1, 2, 2, 3], 3, system.cmp[int]) == 4
  84. doAssert upperBound([1, 2, 3, 5], 3) == 3
  85. block fillEmptySeq:
  86. var s = newSeq[int]()
  87. s.fill(0)
  88. block testBinarySearch:
  89. var noData: seq[int]
  90. doAssert binarySearch(noData, 7) == -1
  91. let oneData = @[1]
  92. doAssert binarySearch(oneData, 1) == 0
  93. doAssert binarySearch(oneData, 7) == -1
  94. let someData = @[1, 3, 4, 7]
  95. doAssert binarySearch(someData, 1) == 0
  96. doAssert binarySearch(someData, 7) == 3
  97. doAssert binarySearch(someData, -1) == -1
  98. doAssert binarySearch(someData, 5) == -1
  99. doAssert binarySearch(someData, 13) == -1
  100. let moreData = @[1, 3, 5, 7, 4711]
  101. doAssert binarySearch(moreData, -1) == -1
  102. doAssert binarySearch(moreData, 1) == 0
  103. doAssert binarySearch(moreData, 5) == 2
  104. doAssert binarySearch(moreData, 6) == -1
  105. doAssert binarySearch(moreData, 4711) == 4
  106. doAssert binarySearch(moreData, 4712) == -1
  107. # merge
  108. proc main() =
  109. block:
  110. var x = @[1, 7, 8, 11, 21, 33, 45, 99]
  111. var y = @[6, 7, 9, 12, 57, 66]
  112. var merged: seq[int]
  113. merged.merge(x, y)
  114. doAssert merged.isSorted
  115. doAssert merged == sorted(x & y)
  116. block:
  117. var x = @[111, 88, 76, 56, 45, 31, 22, 19, 11, 3]
  118. var y = @[99, 85, 83, 82, 69, 64, 48, 42, 33, 31, 26, 13]
  119. var merged: seq[int]
  120. merged.merge(x, y, (x, y) => -system.cmp(x, y))
  121. doAssert merged.isSorted((x, y) => -system.cmp(x, y))
  122. doAssert merged == sorted(x & y, SortOrder.Descending)
  123. block:
  124. var x: seq[int] = @[]
  125. var y = @[1]
  126. var merged: seq[int]
  127. merged.merge(x, y)
  128. doAssert merged.isSorted
  129. doAssert merged.isSorted(SortOrder.Descending)
  130. doAssert merged == @[1]
  131. block:
  132. var x = [1, 3, 5, 5, 7]
  133. var y: seq[int] = @[]
  134. var merged: seq[int]
  135. merged.merge(x, y)
  136. doAssert merged.isSorted
  137. doAssert merged == @x
  138. block:
  139. var x = [1, 3, 5, 5, 7]
  140. var y: seq[int] = @[]
  141. var merged: seq[int] = @[1, 2, 3, 5, 6, 56, 99, 2, 34]
  142. merged.merge(x, y)
  143. doAssert merged == @[1, 2, 3, 5, 6, 56, 99, 2, 34, 1, 3, 5, 5, 7]
  144. block:
  145. var x: array[0, int]
  146. var y = [1, 4, 6, 7, 9]
  147. var merged: seq[int]
  148. merged.merge(x, y)
  149. doAssert merged.isSorted
  150. doAssert merged == @y
  151. block:
  152. var x: array[0, int]
  153. var y: array[0, int]
  154. var merged: seq[int]
  155. merged.merge(x, y)
  156. doAssert merged.isSorted
  157. doAssert merged.len == 0
  158. block:
  159. var x: array[0, int]
  160. var y: array[0, int]
  161. var merged: seq[int] = @[99, 99, 99]
  162. merged.setLen(0)
  163. merged.merge(x, y)
  164. doAssert merged.isSorted
  165. doAssert merged.len == 0
  166. block:
  167. var x: seq[int]
  168. var y: seq[int]
  169. var merged: seq[int]
  170. merged.merge(x, y)
  171. doAssert merged.isSorted
  172. doAssert merged.len == 0
  173. block:
  174. type
  175. Record = object
  176. id: int
  177. proc r(id: int): Record =
  178. Record(id: id)
  179. proc cmp(x, y: Record): int =
  180. if x.id == y.id: return 0
  181. if x.id < y.id: return -1
  182. result = 1
  183. var x = @[r(-12), r(1), r(3), r(8), r(13), r(88)]
  184. var y = @[r(4), r(7), r(12), r(13), r(77), r(99)]
  185. var merged: seq[Record] = @[]
  186. merged.merge(x, y, cmp)
  187. doAssert merged.isSorted(cmp)
  188. doAssert merged.len == 12
  189. block:
  190. type
  191. Record = object
  192. id: int
  193. proc r(id: int): Record =
  194. Record(id: id)
  195. proc ascendingCmp(x, y: Record): int =
  196. if x.id == y.id: return 0
  197. if x.id < y.id: return -1
  198. result = 1
  199. proc descendingCmp(x, y: Record): int =
  200. if x.id == y.id: return 0
  201. if x.id < y.id: return 1
  202. result = -1
  203. var x = @[r(-12), r(1), r(3), r(8), r(13), r(88)]
  204. var y = @[r(4), r(7), r(12), r(13), r(77), r(99)]
  205. var merged: seq[Record]
  206. merged.setLen(0)
  207. merged.merge(x, y, ascendingCmp)
  208. doAssert merged.isSorted(ascendingCmp)
  209. doAssert merged == sorted(x & y, ascendingCmp)
  210. reverse(x)
  211. reverse(y)
  212. merged.setLen(0)
  213. merged.merge(x, y, descendingCmp)
  214. doAssert merged.isSorted(descendingCmp)
  215. doAssert merged == sorted(x & y, ascendingCmp, SortOrder.Descending)
  216. reverse(x)
  217. reverse(y)
  218. merged.setLen(0)
  219. merged.merge(x, y, proc (x, y: Record): int = -descendingCmp(x, y))
  220. doAssert merged.isSorted(proc (x, y: Record): int = -descendingCmp(x, y))
  221. doAssert merged == sorted(x & y, ascendingCmp)
  222. var x: seq[(int, int)]
  223. x.merge([(1,1)], [(1,2)], (a,b) => a[0] - b[0])
  224. doAssert x == @[(1, 1), (1, 2)]
  225. static: main()
  226. main()