Skip to content

Commit e3f827e

Browse files
Add Linear Search in Lua (#5171)
1 parent f243bd4 commit e3f827e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

archive/l/lua/linear-search.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
local args = {...}
3+
4+
local nums_str = args[1]
5+
6+
local key = tonumber(args[2])
7+
8+
local numbers = {}
9+
10+
local found = false
11+
12+
if #args ~= 2 then
13+
print('Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")')
14+
return
15+
end
16+
17+
if not key then
18+
19+
print('Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")')
20+
return
21+
end
22+
23+
if #nums_str < 1 then
24+
print('Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")')
25+
return
26+
end
27+
28+
for num in string.gmatch(nums_str, '([^,]+)') do
29+
table.insert(numbers, tonumber(num))
30+
end
31+
32+
33+
34+
35+
for i = 1, #numbers do
36+
37+
if numbers[i] == key then
38+
found = true
39+
40+
break
41+
end
42+
end
43+
44+
print(found and "true" or "false")

0 commit comments

Comments
 (0)