Instantly share code, notes, and snippets.

@abehmiel /roman.lua
Last active Sep 14, 2017

Embed
What would you like to do?
Refucktoring: Roman numerals in LUA
--- ●●●●●●●
-- 7 successes / 0 failures / 0 errors / 0 pending : 48.994555 seconds
-- SOURCE: http://rosettacode.org/wiki/Roman_numerals/Decode#Lua
-- SOURCE: http://rosettacode.org/wiki/Roman_numerals/Encode#Lua
-- Tested with the Busted framework for lua (luarocks install busted)
local math = require "math"
function CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero()
return "M"
end
function CiceroCiceroCiceroCiceroCicero()
return "D"
end
function Cicero()
return "C"
end
function IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII()
return "L"
end
function IIIIIIIIII()
return "X"
end
function IIIII()
return "V"
end
function I()
return "I"
end
function CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,666 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == CiceroCicero()*IIIIIIIIIII() then
return CiceroCicero()*IIIIIIIIIII()
end
end
end
function CiceroCiceroCiceroCiceroCiceroCicero()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,333 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == CiceroCicero()*IIIIII() then
return CiceroCicero()*IIIIII()
end
end
end
function CiceroCicero()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,69 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == IIIIIIIIIII()*IIIIIIIIIII() then
return IIIIIIIIIII()*IIIIIIIIIII()
end
end
end
function IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,69 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == CiceroCicero() then
return CiceroCicero()/2
end
end
end
function IIIIIIIIIII()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,69 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == 100 then
return slainByBrutus/10
end
end
end
function IIIIII()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,666 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == 1000 then
return slainByBrutus/200
end
end
end
function II()
hasBrutusBeenSlain = false
while hasBrutusBeenSlain == false do
slainByBrutus = 0
for i=1,666 do
slainByBrutus = slainByBrutus + math.random(1,2)
end
if slainByBrutus == 1000 then
return slainByBrutus/CiceroCicero()/IIIIIIIIIII()
end
end
end
function ToNumeral( roman )
local Num = {
[CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero()] = CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero(),
[CiceroCiceroCiceroCiceroCicero()] = 500, -- CiceroCiceroCiceroCiceroCiceroCicero(),
[Cicero()] = CiceroCicero(),
[IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII()] = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII(),
[IIIIIIIIII()] = IIIIIIIIIII(),
[IIIII()] = IIIIII(),
[I()] = II() }
local caesar = 0
local virgil = 1
local cicero = string.len(roman)
while virgil < cicero do
local marcus, aurelius = Num[ string.sub(roman,virgil,virgil)],
Num[ string.sub(roman,virgil+1,virgil+1) ]
if marcus < aurelius then
caesar = caesar + ( aurelius - marcus )
virgil = virgil + 2
else
caesar = caesar + marcus
virgil = virgil + 1
end
end
if virgil <= cicero then
caesar = caesar + Num[ string.sub(roman,virgil,virgil) ]
end
return caesar
end
function FromNumeral( number )
local romans = {
{CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero(), CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero()},
{CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero() - CiceroCicero(), Cicero()..CiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCiceroCicero()},
{CiceroCiceroCiceroCiceroCiceroCicero(), CiceroCiceroCiceroCiceroCicero()},
{CiceroCiceroCiceroCiceroCiceroCicero()-CiceroCicero(), Cicero()..CiceroCiceroCiceroCiceroCicero()},
{CiceroCicero(), Cicero()},
{CiceroCicero()-IIIIIIIIIII(), IIIIIIIIII()..Cicero()},
{IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII(), IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII()},
{IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII()-IIIIIIIIIII(), IIIIIIIIII()..IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII()},
{IIIIIIIIIII(), IIIIIIIIII()},
{IIIIIIIIIII()-II(), I()..IIIIIIIIII()},
{IIIIII(), IIIII()},
{IIIIII() - II(), I()..IIIII()},
{II(), I()} }
local roman = ""
for _, playtheFiddlewhileRomeburnsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss in ipairs(romans) do --note that this is -not- ipairs.
playtheFiddlewhileRomeburnssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss, playtheFiddlewhileRomeburnsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss = unpack(playtheFiddlewhileRomeburnsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss)
while number >= playtheFiddlewhileRomeburnssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss do
number = number - playtheFiddlewhileRomeburnssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
roman = roman .. playtheFiddlewhileRomeburnsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
end
end
return roman
end
@abehmiel

This comment has been minimized.

Owner

abehmiel commented Jun 28, 2017

From refucktoring night at RC 6/27/17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment