diff options
Diffstat (limited to 'challenge-111')
| -rwxr-xr-x | challenge-111/stuart-little/lua/ch-1.lua | 28 | ||||
| -rwxr-xr-x | challenge-111/stuart-little/lua/ch-2.lua | 24 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-111/stuart-little/lua/ch-1.lua b/challenge-111/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..4799dea99f --- /dev/null +++ b/challenge-111/stuart-little/lua/ch-1.lua @@ -0,0 +1,28 @@ +#!/usr/bin/env lua + +function bSearch(x,t) + if #t==0 then return end + local mid = math.max(1,math.floor(#t/2)) + if x==t[mid] then return x end + if x < t[mid] then return bSearch(x,table.pack(table.unpack(t,1,mid-1))) end + if x > t[mid] then return bSearch(x,table.pack(table.unpack(t,mid+1))) end +end + +local nrs={} +for l in io.lines(arg[2]) do + for nr in l:gmatch("-?%d+") do table.insert(nrs,tonumber(nr)) end +end + +print(bSearch(tonumber(arg[1]), nrs) and 1 or 0) + +--[[ +run <script> <number> <file containing matrix> + +for instance, copy the matrix below to an input file + +[ 1, 2, 3, 5, 7 ] +[ 9, 11, 15, 19, 20 ] +[ 23, 24, 25, 29, 31 ] +[ 32, 33, 39, 40, 42 ] +[ 45, 47, 48, 49, 50 ] +--]] diff --git a/challenge-111/stuart-little/lua/ch-2.lua b/challenge-111/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..e4cd610499 --- /dev/null +++ b/challenge-111/stuart-little/lua/ch-2.lua @@ -0,0 +1,24 @@ +#!/usr/bin/env lua + +-- run <script> <file> + +local t={ln=0} + +function sorted(w) + local wt={} + for i in w:gmatch(".") do table.insert(wt,i) end + table.sort(wt) + return w==table.concat(wt) +end + +for l in io.lines(arg[1]) do + local w = l:match("%w+") + if sorted(w:lower()) and w:len() >= t['ln'] then + if w:len() > t['ln'] then t={ln=w:len()} end + table.insert(t,w) + end +end + +for _,v in ipairs(t) do + print(v) +end |
