Guest User

Untitled

a guest
Jul 19th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. --look way down!
  2.  
  3. local function check(test)
  4.  
  5. local function uglytest(arr)
  6. local function nondecreasing(arr)
  7. for i = 1, #arr - 1 do
  8. if arr[i] > arr[i + 1] then
  9. return false
  10. end
  11. end
  12.  
  13. return true
  14. end
  15.  
  16. for j = 1, #arr do
  17. local val = table.remove(arr, j)
  18. if not nondecreasing(arr) then
  19. table.insert(arr, j, val)
  20. else
  21. return true
  22. end
  23. end
  24.  
  25. return false
  26. end
  27.  
  28. assert(test({ }) == true)
  29.  
  30. assert(test({ 0 }) == true)
  31.  
  32. assert(test({ 0, 0 }) == true)
  33. assert(test({ 0, 1 }) == true)
  34. assert(test({ 1, 0 }) == true)
  35.  
  36. for a = 0, 2 do for b = 0, 2 do for c = 0, 2 do
  37. print(a, b, c, test({ a, b, c }))
  38. assert(test({ a, b, c }) == uglytest({ a, b, c }))
  39. end end end
  40. for a = 0, 3 do for b = 0, 3 do for c = 0, 3 do for d = 0, 3 do
  41. print(a, b, c, d, test({ a, b, c, d }))
  42. assert(test({ a, b, c, d }) == uglytest({ a, b, c, d }))
  43. end end end end
  44.  
  45. for a = 0, 4 do for b = 0, 4 do for c = 0, 4 do for d = 0, 4 do for e = 0, 4 do
  46. print(a, b, c, d, e, test({ a, b, c, d, e }))
  47. assert(test({ a, b, c, d, e }) == uglytest({ a, b, c, d, e }))
  48. end end end end end
  49.  
  50. end
  51.  
  52. check(CanBeNonDecreasing) --pass your function to this
Add Comment
Please, Sign In to add comment