Skip to content

Commit 41b8410

Browse files
authored
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
1 parent f41974c commit 41b8410

10 files changed

+49
-35
lines changed

archive/l/lua/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Welcome to Sample Programs in Lua!
1717
- [Prime Number in Lua][11]
1818
- [Reverse a String in Lua][5]
1919
- [ROT-13 in Lua][10]
20+
- Solution based on [Yonaba's Caesar Cipher solution][yonaba-solution]
2021
- [Roman Numeral Conversion][12]
2122

2223
## Fun Facts
@@ -30,6 +31,8 @@ Welcome to Sample Programs in Lua!
3031
- [Lua Wiki][3]
3132
- [Lua Docs][4]
3233

34+
[yonaba-solution]: https://github.com/kennyledet/Algorithm-Implementations/blob/master/ROT13_Cipher/Lua/Yonaba/rot13.lua
35+
3336
[1]: https://github.com/TheRenegadeCoder/sample-programs/issues/444
3437
[2]: https://therenegadecoder.com/code/hello-world-in-lua/
3538
[3]: https://en.wikipedia.org/wiki/Lua_(programming_language)

archive/l/lua/baklava.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ function diamondStarPattern(height)
44
for j = 1, (2*i-1) do io.write("*") end -- to print the * for the upper half
55
io.write("\n") -- to move pointer to next line
66
end
7-
for i = height,1,-1 do -- loop for printing the lower half of the baklava
7+
for i = height - 1,1,-1 do -- loop for printing the lower half of the baklava
88
for j = i, height - 1 do io.write(" ") end -- to print the spaces
99
for j = 1, (2*i-1) do io.write("*") end -- to print the * for the lower half
1010
io.write("\n") -- to move pointer to next line
1111
end
1212
end
1313

14-
diamondStarPattern(21)
14+
diamondStarPattern(11)

archive/l/lua/bubble-sort.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ nums = {}
33

44
-- Exit if no list is entered or list is empty
55
if arg[1] == nil or #arg[1] == 0 then
6-
print('Usage: please provide a list of at least two integers to sort in the format 1, 2, 3, 4, 5')
6+
print('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"')
77
return
88
end
99

1010
-- Exit if list has a single entry or is not formatted with commas
1111
if (not string.match(arg[1], " ")) or (not string.match(arg[1], ",")) then
12-
print('Usage: please provide a list of at least two integers to sort in the format 1, 2, 3, 4, 5')
12+
print('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"')
1313
return
1414
end
1515

@@ -43,4 +43,4 @@ for k,v in pairs(nums) do
4343
end
4444
end
4545

46-
print()
46+
io.write("\n")

archive/l/lua/capitalize.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
if (#arg < 1)
1+
if (#arg < 1 or arg[1] == '')
22
then
3-
print('Usage: provide a string')
3+
print('Usage: please provide a string')
44
else
55
str = {...}
66
s = ""
77
for i,v in pairs(str) do
88
s = s .. v .. " "
99
end
10-
print(s:gsub("^%l", string.upper))
10+
s, _ = s:gsub("^%l", string.upper)
11+
print(s)
1112
end

archive/l/lua/factorial.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ main = function(input)
2121
maxInput = 20
2222
usage = "Usage: please input a non-negative integer"
2323

24-
if not (input == nil)
24+
if not (input == nil or input == "")
2525
then
2626
inputValidation = input:gsub('[0-9]','')
2727
if inputValidation:len() == 0
@@ -43,4 +43,4 @@ main = function(input)
4343
end
4444

4545
-- Run the script
46-
main(arg[1])
46+
main(arg[1])

archive/l/lua/file-io.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
function writer()
22
file_to_be_written = io.open("output.txt","w+")
3+
io.output(file_to_be_written)
34
io.write("text to be written into output.txt")
4-
io.close()
5+
io.close(file_to_be_written)
56
end
67

78

89
function reader()
910
file_to_be_read = io.open("output.txt","r")
1011
io.input(file_to_be_read)
1112
print(io.read())
12-
io.close()
13-
end
13+
io.close(file_to_be_read)
14+
end
1415

1516
writer()
1617
reader()

archive/l/lua/prime-number.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ function isPrime(n)
44
prime = "Prime"
55
usage = "Usage: please input a non-negative integer"
66
local n = tonumber(n)
7-
--catch nil, 0, 1, negative and non int numbers
8-
if not n or n<2 or (n % 1 ~=0) then
7+
--catch nil, negative and non int numbers
8+
if not n or n < 0 or (n % 1 ~= 0) then
99
print(usage)
10+
--catch 0 and 1
11+
elseif n < 2 then
12+
print(comp)
1013
--catch even number above 2
11-
elseif n>2 and (n % 2 == 0) then
14+
elseif n > 2 and (n % 2 == 0) then
1215
print(comp)
13-
--primes over 5 end in 1,3,7 or 9
1416
--catch numbers that end in 5 or 0 (multiples of 5)
1517
elseif n>5 and (n % 5 ==0) then
1618
print(comp)
1719
--now check for prime
1820
else
1921
--only do the odds
22+
result = prime
2023
for i = 3, math.sqrt(n), 2 do
2124
--did it divide evenly
2225
if (n % i == 0) then
23-
print(comp)
26+
result = comp
2427
end
2528
end
2629
--can defeat optimus
27-
print(prime)
30+
print(result)
2831
end
2932
end
3033

31-
32-
isPrime(arg[1])
34+
isPrime(arg[1])

archive/l/lua/roman-numeral-conversion.lua renamed to archive/l/lua/roman-numeral.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ for i = 1, #input do
6666
end
6767

6868
-- Print converted value
69-
print(convertToDecimal(input))
69+
print(convertToDecimal(input))

archive/l/lua/rot-13.lua

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
-- See: https://stackoverflow.com/questions/9695697/lua-replacement-for-the-operator/20858039#20858039
2-
function mod(a, b)
3-
return a - (math.floor(a/b)*b)
1+
local function ascii_base(s)
2+
return s:lower() == s and ('a'):byte() or ('A'):byte()
43
end
54

6-
if (#arg < 1)
5+
function caesar_cipher(str, key)
6+
return (str:gsub('%a', function(s)
7+
local base = ascii_base(s)
8+
return string.char(((s:byte() - base + key) % 26) + base)
9+
end))
10+
end
11+
12+
if (#arg < 1 or arg[1] == "")
713
then
8-
print('Usage: provide a string')
14+
print('Usage: please provide a string to encrypt')
915
else
10-
str = {...}
11-
for i,v in pairs(str) do
12-
for k = 1, #v do
13-
local c = v:sub(k,k)
14-
io.write(string.char(mod(string.byte(c) - 97 + 13, 26) + 97))
15-
end
16-
io.write(" ")
17-
end
16+
io.write(caesar_cipher(arg[1], 13))
1817
end
1918

2019
io.write("\n")

archive/l/lua/testinfo.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
folder:
2+
extension: ".lua"
3+
naming: "hyphen"
4+
5+
container:
6+
image: "nickblah/lua"
7+
tag: "5.4-alpine"
8+
cmd: "lua {{ source.name }}{{ source.extension }}"

0 commit comments

Comments
 (0)