1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/opt/local/bin/lua
--
-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-004
--
--
-- Run as: lua ch-2.lua -f FILE < input-file
--
--
-- Parse option, and exit if incorrect
--
local filename = ""
if #arg == 2 and arg [1] == "-f"
then filename = arg [2]
end
if file == ""
then io . stderr : write ("Requires a '-f FILE' option\n")
os . exit (1)
end
--
-- Given a file name, and a set of letters, print
-- the words from the file which can be made with the letters.
--
function extract_words (filename, letters)
for word in io . lines (filename) do
copy = word : gsub ("\n", "") : lower ()
for i = 1, letters : len () do
--
-- Remove each of the characters of 'letters' from
-- 'copy', after lowercasing, and only once.
--
copy = copy : gsub (letters : sub (i, i) : lower (), "", 1)
end
--
-- If we end up with the empty string, we have a winner.
--
if (copy : len () == 0)
then
print (word)
end
end
end
for line in io . lines () do
extract_words (filename, line : gsub ("\n", ""))
end
|