Jump to content

Straddling checkerboard: Difference between revisions

m
Line 1,152:
 
=={{header|Julia}}==
Unlike the PicoLispprecomputed basedtable versions, this version takes a 30-character string specifying the 3 rows of the checkerboard
as an argument, recomputing the lookup table each run, which allows easier changes of keys without modifying the code.
checkerboard as an argument, automatically re-coding for the position of the blanks as well as letters,
to allow an easy change of coding or decoding keys without modifying the code.
<lang julia>
function straddlingcheckerboard(board, msg, doencode)
lookup = Dict()
reverselookup = Dict()
row2 = row3 = slash = -1
escapenum(x) = replace(replace(x, r"([01-9])", s";=;\1"), r";=;", slash)
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
elseif haskey(reverselookup, "$c$(x[i+1])")else
if "$c$(x[i+1])" == slash
s *= string(x[i+2])
Line 1,190 ⟶ 1,187:
i += 2
end
else
println("Decoding error at position $i")
i += 1
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
reverselookup["$(i-1)"] = c
elseif i < 21
lookup[c] = "$row2$(i-11)"; reverselookup["$row2$(i-11)"] = c
reverselookup["$row2$(i-11)"] = c
else
lookup[c] = "$row3$(i-21)"; reverselookup["$row3$(i-21)"] = c
reverselookup["$row3$(i-21)"] = c
end
if c == "/"
Line 1,223 ⟶ 1,213:
end
if doencode ? encode(msg) : decode(msg)
msg |> uppercase |> escapenum |> encode
else
msg |> decode
end
end
 
btable = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
 
message = "Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour."
encoded = straddlingcheckerboard(btable, message, true)
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.