diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2023-07-31 15:49:22 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2023-07-31 15:49:22 +0800 |
| commit | e7b6313261ef4541d4dcc303c46ef0d886649b70 (patch) | |
| tree | 8cff819a2036dd8d0172b6343b0a199d7b81721a /challenge-227/roger-bell-west/lua/ch-2.lua | |
| parent | 4fda4a4a398e64921020704733556a2ec6dae78a (diff) | |
| parent | e511966ce2280dbedb2c916d9e6254708800639e (diff) | |
| download | perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.tar.gz perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.tar.bz2 perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-227/roger-bell-west/lua/ch-2.lua')
| -rwxr-xr-x | challenge-227/roger-bell-west/lua/ch-2.lua | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/challenge-227/roger-bell-west/lua/ch-2.lua b/challenge-227/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..75d6ed9884 --- /dev/null +++ b/challenge-227/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,240 @@ +#! /usr/bin/lua + +function roman2int(roman) + if roman == "" then + return 0 + end + for _, s in ipairs({ + { 'M', 1000 }, + { 'CM', 900 }, + { 'D', 500 }, + { 'CD', 400 }, + { 'C', 100 }, + { 'XC', 90 }, + { 'L', 50 }, + { 'XL', 40 }, + { 'X', 10 }, + { 'IX', 9 }, + { 'V', 5 }, + { 'IV', 4 }, + { 'I', 1 } + }) do + if string.find(roman, s[1], 1, true) == 1 then + return s[2] + roman2int(string.sub(roman, #s[1] + 1)) + end + end + return 0 +end + +function int2roman(n0) + local number = n0 + local mn = "" + for _, s in ipairs({ + { 'M', 1000 }, + { 'CM', 900 }, + { 'D', 500 }, + { 'CD', 400 }, + { 'C', 100 }, + { 'XC', 90 }, + { 'L', 50 }, + { 'XL', 40 }, + { 'X', 10 }, + { 'IX', 9 }, + { 'V', 5 }, + { 'IV', 4 }, + { 'I', 1 } + }) do + while s[2] <= number do + mn = mn .. s[1] + number = number - s[2] + end + end + return mn +end + +-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua +function split(inputstr, sep) + sep=sep or '%s' + local t={} + for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do + table.insert(t,field) + if s=="" then + return t + end + end +end + +function romanmaths(ax) + local elems = split(ax, " ") + local a = roman2int(elems[1]) + local b = roman2int(elems[3]) + local c = -1 + if elems[2] == "+" then + c = a + b + elseif elems[2] == "-" then + c = a - b + elseif elems[2] == "*" then + c = a * b + elseif elems[2] == "/" then + if a % b == 0 then + c = a // b + end + elseif elems[2] == "**" then + c = a ^ b + end + if c > 3999 or c < 0 then + return "non potest" + elseif c == 0 then + return "nulla" + else + return int2roman(c) + end +end + +if roman2int("I") == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if roman2int("II") == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if roman2int("IV") == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if roman2int("IX") == 9 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if roman2int("XXX") == 30 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if roman2int("MDCLXVI") == 1666 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if int2roman(1) == "I" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if int2roman(2) == "II" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if int2roman(4) == "IV" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if int2roman(9) == "IX" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if int2roman(30) == "XXX" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if int2roman(1666) == "MDCLXVI" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("IV + V") == "IX" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("M - I") == "CMXCIX" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("X / II") == "V" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("XI * VI") == "LXVI" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("VII ** III") == "CCCXLIII" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("V - V") == "nulla" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("V / II") == "non potest" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("MMM + M") == "non potest" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if romanmaths("V - X") == "non potest" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + |
