aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-255/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-255/roger-bell-west/lua/ch-2.lua36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-255/roger-bell-west/lua/ch-2.lua b/challenge-255/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..f8374a001b
--- /dev/null
+++ b/challenge-255/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,36 @@
+#! /usr/bin/lua
+
+function mostfrequentword(para, banned)
+ local words = {}
+ for c in string.gmatch(para, "%a+") do
+ if words[c] == nil then
+ words[c] = 1
+ else
+ words[c] = words[c] + 1
+ end
+ end
+ words[banned] = nil
+ local m = 0
+ for k, v in pairs(words) do
+ m = math.max(m, v)
+ end
+ for k, v in pairs(words) do
+ if v == m then
+ return k
+ end
+ end
+end
+
+if mostfrequentword("Joe hit a ball, the hit ball flew far after it was hit.", "hit") == "ball" then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if mostfrequentword("Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", "the") == "Perl" then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")