Skip to content

Commit

Permalink
Added Lua Tests (#2229)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jrg94 authored Oct 15, 2020
1 parent f41974c commit 41b8410
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 35 deletions.
3 changes: 3 additions & 0 deletions archive/l/lua/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions archive/l/lua/baklava.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
diamondStarPattern(11)
6 changes: 3 additions & 3 deletions archive/l/lua/bubble-sort.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -43,4 +43,4 @@ for k,v in pairs(nums) do
end
end

print()
io.write("\n")
7 changes: 4 additions & 3 deletions archive/l/lua/capitalize.lua
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions archive/l/lua/factorial.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,4 +43,4 @@ main = function(input)
end

-- Run the script
main(arg[1])
main(arg[1])
7 changes: 4 additions & 3 deletions archive/l/lua/file-io.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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


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()
18 changes: 10 additions & 8 deletions archive/l/lua/prime-number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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])
isPrime(arg[1])
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ for i = 1, #input do
end

-- Print converted value
print(convertToDecimal(input))
print(convertToDecimal(input))
25 changes: 12 additions & 13 deletions archive/l/lua/rot-13.lua
Original file line number Diff line number Diff line change
@@ -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")
8 changes: 8 additions & 0 deletions archive/l/lua/testinfo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
folder:
extension: ".lua"
naming: "hyphen"

container:
image: "nickblah/lua"
tag: "5.4-alpine"
cmd: "lua {{ source.name }}{{ source.extension }}"

0 comments on commit 41b8410

Please sign in to comment.