Straddling checkerboard: Difference between revisions
m
→{{header|Julia}}
Line 1,152:
=={{header|Julia}}==
Unlike the
as an argument, recomputing the lookup table each run, which allows easier changes of keys without modifying the code.
<lang julia>
function straddlingcheckerboard(board, msg, doencode)
lookup = Dict()
reverselookup = Dict()
row2 = row3 = slash = -1
function encode(x)
s = ""
for ch in replace(replace(uppercase(x), r"([01-9])", s";=;\1"), r";=;", slash)
c = string(ch)
if haskey(lookup, c)
Line 1,182 ⟶ 1,179:
s *= reverselookup[c]
i += 1
if "$c$(x[i+1])" == slash
s *= string(x[i+2])
Line 1,190 ⟶ 1,187:
i += 2
end
end
end
s
end
for (i,c) in enumerate(board)
c = string(c)
if c == " "
if row2 == -1
row2 = i-1
else
Line 1,208 ⟶ 1,201:
else
if i < 11
lookup[c] = "$(i-1)"; reverselookup["$(i-1)"] = c
elseif i < 21
lookup[c] = "$row2$(i-11)"; reverselookup["$row2$(i-11)"] = c
else
lookup[c] = "$row3$(i-21)"; reverselookup["$row3$(i-21)"] = c
end
if c == "/"
Line 1,223 ⟶ 1,213:
end
end
btable = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
message = "Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour."
encoded = straddlingcheckerboard(btable, message, true)
|