Straddling checkerboard: Difference between revisions
Content added Content deleted
(→{{header|PHP}}: added PHP) |
|||
Line 1,559: | Line 1,559: | ||
Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935 |
Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935 |
||
Decoded: ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre> |
Decoded: ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre> |
||
=={{header|Phix}}== |
|||
{{trans|C}} |
|||
<lang Phix>function read_table(string cb) |
|||
sequence encode = repeat("",128), |
|||
decode = repeat(0,128), |
|||
row = {0} |
|||
if length(cb)!=30 then crash("table wrong length") end if |
|||
for i=1 to 30 do |
|||
integer c = cb[i] |
|||
if c=' ' then |
|||
row &= i-1 |
|||
else |
|||
integer code = row[floor((i-1)/10)+1]*10+mod((i-1),10) |
|||
encode[c] = sprintf("%d",code) |
|||
decode[code+1] = c |
|||
end if |
|||
end for |
|||
return {encode, decode} |
|||
end function |
|||
function encipher(sequence encode, string in, bool strip=false) |
|||
string out = "" |
|||
for i=1 to length(in) do |
|||
integer c = upper(in[i]), code |
|||
if c>='0' and c<='9' then |
|||
out &= encode['.']&c |
|||
elsif c>='A' and c<='Z' then |
|||
out &= encode[c] |
|||
elsif not strip or c='/' then -- (see note) |
|||
out &= encode['/'] |
|||
end if |
|||
end for |
|||
return out |
|||
end function |
|||
function decipher(sequence decode, string in, bool strip=false) |
|||
string out = "" |
|||
integer i = 1 |
|||
while i<=length(in) do |
|||
integer c = in[i]-'0'+1 |
|||
if decode[c]=0 then |
|||
i += 1 |
|||
c = c*10+in[i]-'9' |
|||
end if |
|||
integer d = decode[c] |
|||
if d='.' then |
|||
i += 1 |
|||
d = in[i] |
|||
elsif d='/' and not strip then -- (see note) |
|||
d = ' ' |
|||
end if |
|||
out &= d |
|||
i += 1 |
|||
end while |
|||
return out |
|||
end function |
|||
constant cb = "ET AON RIS"& |
|||
"BCDFGHJKLM"& |
|||
"PQ/UVWXYZ." |
|||
sequence {encode,decode} = read_table(cb) |
|||
-- Note there is a subtle difference in space handling, to exactly match other outputs try |
|||
-- {encode,decode} = read_table("HOL MES RTABCDFGIJKNPQUVWXYZ/.") instead of |
|||
-- {encode,decode} = read_table("HOL MES RTABCDFGIJKNPQUVWXYZ./"), and |
|||
-- {encode,decode} = read_table("ET AON RISBCDFGHJKLMPQ.UVWXYZ/") instead of |
|||
-- {encode,decode} = read_table("ET AON RISBCDFGHJKLMPQ/UVWXYZ.") |
|||
string msg = "In the winter 1965/we were hungry/just barely alive" |
|||
printf(1,"message: %s\n", {msg}) |
|||
string enc = encipher(encode, msg), |
|||
dec = decipher(decode, enc) |
|||
printf(1,"encoded: %s\n", {enc}) |
|||
printf(1,"decoded: %s\n", {dec}) |
|||
printf(1,"\nNo spaces:\n") |
|||
enc = encipher(encode, msg, true) |
|||
dec = decipher(decode, enc, true) |
|||
printf(1,"encoded: %s\n", {enc}) |
|||
printf(1,"decoded: %s\n", {dec})</lang> |
|||
{{out}} |
|||
<pre> |
|||
message: In the winter 1965/we were hungry/just barely alive |
|||
encoded: 85621250626585107626916996966956265062650706225635247676226639162203702867623288640 |
|||
decoded: IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE |
|||
No spaces: |
|||
encoded: 851250658510769169969669562650650702563524767622663912037028673288640 |
|||
decoded: INTHEWINTER1965/WEWEREHUNGRY/JUSTBARELYALIVE |
|||
</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |