eniallator

Bitonic Checker | Lua

Nov 15th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1. local function checkBitonic(inTbl)
  2.     if #inTbl < 3 or inTbl[1] > inTbl[2] then
  3.         return false
  4.     end
  5.  
  6.     decreasing = false
  7.  
  8.     for i, val in ipairs(inTbl) do
  9.         if i ~= 1 and i ~= #inTbl then
  10.             nextVal = inTbl[i + 1]
  11.             if val == nextVal or decreasing and val < nextVal then
  12.                 return false
  13.             elseif not decreasing then
  14.                 decreasing = val > nextVal
  15.             end
  16.         end
  17.     end
  18.  
  19.     return decreasing
  20. end
  21.  
  22. print('Should be true: ' .. tostring(checkBitonic({1, 2, 1})))
  23. print('Should be true: ' .. tostring(checkBitonic({1, 5, 7, 9, 8, 2})))
  24. print('Should be true: ' .. tostring(checkBitonic({3, 4, 5, 1})))
  25.  
  26. print('Should be false: ' .. tostring(checkBitonic({})))
  27. print('Should be false: ' .. tostring(checkBitonic({1, 2})))
  28. print('Should be false: ' .. tostring(checkBitonic({2, 1})))
  29. print('Should be false: ' .. tostring(checkBitonic({1, 3, 5, 7})))
  30. print('Should be false: ' .. tostring(checkBitonic({7, 5, 3, 1})))
  31. print('Should be false: ' .. tostring(checkBitonic({1, 5, 5, 2})))
  32. print('Should be false: ' .. tostring(checkBitonic({5, 2, 1, 3})))
Advertisement
Add Comment
Please, Sign In to add comment