talgorithm.nim 7.4 KB

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