From 41b8410a6bb150dab662127c1e89705ed9b3889d Mon Sep 17 00:00:00 2001 From: Jeremy Grifski Date: Thu, 15 Oct 2020 19:22:39 -0400 Subject: [PATCH] Added Lua Tests (#2229) * Added Lua Testing * Added missing extension * Fixed Baklava in Lua * Update testinfo.yml * Update capitalize.lua * Fixed capitalize * Update rot-13.lua * Update bubble-sort.lua * Rename roman-numeral-conversion.lua to roman-numeral.lua * Update factorial.lua * Updated isPrime to Match Testing * Update prime-number.lua * Update rot-13.lua * Update rot-13.lua * Added citation * Update rot-13.lua * Update bubble-sort.lua * Update file-io.lua --- archive/l/lua/README.md | 3 +++ archive/l/lua/baklava.lua | 4 +-- archive/l/lua/bubble-sort.lua | 6 ++--- archive/l/lua/capitalize.lua | 7 +++--- archive/l/lua/factorial.lua | 4 +-- archive/l/lua/file-io.lua | 7 +++--- archive/l/lua/prime-number.lua | 18 +++++++------ ...meral-conversion.lua => roman-numeral.lua} | 2 +- archive/l/lua/rot-13.lua | 25 +++++++++---------- archive/l/lua/testinfo.yml | 8 ++++++ 10 files changed, 49 insertions(+), 35 deletions(-) rename archive/l/lua/{roman-numeral-conversion.lua => roman-numeral.lua} (98%) create mode 100644 archive/l/lua/testinfo.yml diff --git a/archive/l/lua/README.md b/archive/l/lua/README.md index 7eb19549e..4ff397e03 100644 --- a/archive/l/lua/README.md +++ b/archive/l/lua/README.md @@ -17,6 +17,7 @@ Welcome to Sample Programs in Lua! - [Prime Number in Lua][11] - [Reverse a String in Lua][5] - [ROT-13 in Lua][10] + - Solution based on [Yonaba's Caesar Cipher solution][yonaba-solution] - [Roman Numeral Conversion][12] ## Fun Facts @@ -30,6 +31,8 @@ Welcome to Sample Programs in Lua! - [Lua Wiki][3] - [Lua Docs][4] +[yonaba-solution]: https://github.com/kennyledet/Algorithm-Implementations/blob/master/ROT13_Cipher/Lua/Yonaba/rot13.lua + [1]: https://github.com/TheRenegadeCoder/sample-programs/issues/444 [2]: https://therenegadecoder.com/code/hello-world-in-lua/ [3]: https://en.wikipedia.org/wiki/Lua_(programming_language) diff --git a/archive/l/lua/baklava.lua b/archive/l/lua/baklava.lua index ddd72bc3d..0a2da05bc 100644 --- a/archive/l/lua/baklava.lua +++ b/archive/l/lua/baklava.lua @@ -4,11 +4,11 @@ function diamondStarPattern(height) for j = 1, (2*i-1) do io.write("*") end -- to print the * for the upper half io.write("\n") -- to move pointer to next line end - for i = height,1,-1 do -- loop for printing the lower half of the baklava + for i = height - 1,1,-1 do -- loop for printing the lower half of the baklava for j = i, height - 1 do io.write(" ") end -- to print the spaces for j = 1, (2*i-1) do io.write("*") end -- to print the * for the lower half io.write("\n") -- to move pointer to next line end end -diamondStarPattern(21) \ No newline at end of file +diamondStarPattern(11) diff --git a/archive/l/lua/bubble-sort.lua b/archive/l/lua/bubble-sort.lua index e64004db7..598d49549 100644 --- a/archive/l/lua/bubble-sort.lua +++ b/archive/l/lua/bubble-sort.lua @@ -3,13 +3,13 @@ nums = {} -- Exit if no list is entered or list is empty if arg[1] == nil or #arg[1] == 0 then - print('Usage: please provide a list of at least two integers to sort in the format “1, 2, 3, 4, 5”') + print('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"') return end -- Exit if list has a single entry or is not formatted with commas if (not string.match(arg[1], " ")) or (not string.match(arg[1], ",")) then - print('Usage: please provide a list of at least two integers to sort in the format “1, 2, 3, 4, 5”') + print('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"') return end @@ -43,4 +43,4 @@ for k,v in pairs(nums) do end end -print() \ No newline at end of file +io.write("\n") diff --git a/archive/l/lua/capitalize.lua b/archive/l/lua/capitalize.lua index 90d8d0050..01f2820c6 100644 --- a/archive/l/lua/capitalize.lua +++ b/archive/l/lua/capitalize.lua @@ -1,11 +1,12 @@ -if (#arg < 1) +if (#arg < 1 or arg[1] == '') then - print('Usage: provide a string') + print('Usage: please provide a string') else str = {...} s = "" for i,v in pairs(str) do s = s .. v .. " " end - print(s:gsub("^%l", string.upper)) + s, _ = s:gsub("^%l", string.upper) + print(s) end diff --git a/archive/l/lua/factorial.lua b/archive/l/lua/factorial.lua index 9d55ff320..a2a607adc 100644 --- a/archive/l/lua/factorial.lua +++ b/archive/l/lua/factorial.lua @@ -21,7 +21,7 @@ main = function(input) maxInput = 20 usage = "Usage: please input a non-negative integer" - if not (input == nil) + if not (input == nil or input == "") then inputValidation = input:gsub('[0-9]','') if inputValidation:len() == 0 @@ -43,4 +43,4 @@ main = function(input) end -- Run the script -main(arg[1]) \ No newline at end of file +main(arg[1]) diff --git a/archive/l/lua/file-io.lua b/archive/l/lua/file-io.lua index e374c3d97..ff2d17120 100644 --- a/archive/l/lua/file-io.lua +++ b/archive/l/lua/file-io.lua @@ -1,7 +1,8 @@ function writer() file_to_be_written = io.open("output.txt","w+") + io.output(file_to_be_written) io.write("text to be written into output.txt") - io.close() + io.close(file_to_be_written) end @@ -9,8 +10,8 @@ function reader() file_to_be_read = io.open("output.txt","r") io.input(file_to_be_read) print(io.read()) - io.close() - end + io.close(file_to_be_read) +end writer() reader() diff --git a/archive/l/lua/prime-number.lua b/archive/l/lua/prime-number.lua index 12f92ffb7..71846f57b 100644 --- a/archive/l/lua/prime-number.lua +++ b/archive/l/lua/prime-number.lua @@ -4,29 +4,31 @@ function isPrime(n) prime = "Prime" usage = "Usage: please input a non-negative integer" local n = tonumber(n) - --catch nil, 0, 1, negative and non int numbers - if not n or n<2 or (n % 1 ~=0) then + --catch nil, negative and non int numbers + if not n or n < 0 or (n % 1 ~= 0) then print(usage) + --catch 0 and 1 + elseif n < 2 then + print(comp) --catch even number above 2 - elseif n>2 and (n % 2 == 0) then + elseif n > 2 and (n % 2 == 0) then print(comp) - --primes over 5 end in 1,3,7 or 9 --catch numbers that end in 5 or 0 (multiples of 5) elseif n>5 and (n % 5 ==0) then print(comp) --now check for prime else --only do the odds + result = prime for i = 3, math.sqrt(n), 2 do --did it divide evenly if (n % i == 0) then - print(comp) + result = comp end end --can defeat optimus - print(prime) + print(result) end end - -isPrime(arg[1]) \ No newline at end of file +isPrime(arg[1]) diff --git a/archive/l/lua/roman-numeral-conversion.lua b/archive/l/lua/roman-numeral.lua similarity index 98% rename from archive/l/lua/roman-numeral-conversion.lua rename to archive/l/lua/roman-numeral.lua index 35e34370c..c5bec17cd 100755 --- a/archive/l/lua/roman-numeral-conversion.lua +++ b/archive/l/lua/roman-numeral.lua @@ -66,4 +66,4 @@ for i = 1, #input do end -- Print converted value -print(convertToDecimal(input)) \ No newline at end of file +print(convertToDecimal(input)) diff --git a/archive/l/lua/rot-13.lua b/archive/l/lua/rot-13.lua index 2411e5c33..86b55b142 100644 --- a/archive/l/lua/rot-13.lua +++ b/archive/l/lua/rot-13.lua @@ -1,20 +1,19 @@ --- See: https://stackoverflow.com/questions/9695697/lua-replacement-for-the-operator/20858039#20858039 -function mod(a, b) - return a - (math.floor(a/b)*b) +local function ascii_base(s) + return s:lower() == s and ('a'):byte() or ('A'):byte() end -if (#arg < 1) +function caesar_cipher(str, key) + return (str:gsub('%a', function(s) + local base = ascii_base(s) + return string.char(((s:byte() - base + key) % 26) + base) + end)) +end + +if (#arg < 1 or arg[1] == "") then - print('Usage: provide a string') + print('Usage: please provide a string to encrypt') else - str = {...} - for i,v in pairs(str) do - for k = 1, #v do - local c = v:sub(k,k) - io.write(string.char(mod(string.byte(c) - 97 + 13, 26) + 97)) - end - io.write(" ") - end + io.write(caesar_cipher(arg[1], 13)) end io.write("\n") diff --git a/archive/l/lua/testinfo.yml b/archive/l/lua/testinfo.yml new file mode 100644 index 000000000..c7a8585db --- /dev/null +++ b/archive/l/lua/testinfo.yml @@ -0,0 +1,8 @@ +folder: + extension: ".lua" + naming: "hyphen" + +container: + image: "nickblah/lua" + tag: "5.4-alpine" + cmd: "lua {{ source.name }}{{ source.extension }}"