Straddling checkerboard: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
m (syntax highlighting fixup automation)
Line 10: Line 10:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V t = [[String(‘79’), ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’],
<syntaxhighlight lang="11l">V t = [[String(‘79’), ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’],
[String(‘’), ‘H’, ‘O’, ‘L’, ‘’, ‘M’, ‘E’, ‘S’, ‘’, ‘R’, ‘T’],
[String(‘’), ‘H’, ‘O’, ‘L’, ‘’, ‘M’, ‘E’, ‘S’, ‘’, ‘R’, ‘T’],
[String(‘3’), ‘A’, ‘B’, ‘C’, ‘D’, ‘F’, ‘G’, ‘I’, ‘J’, ‘K’, ‘N’],
[String(‘3’), ‘A’, ‘B’, ‘C’, ‘D’, ‘F’, ‘G’, ‘I’, ‘J’, ‘K’, ‘N’],
Line 37: Line 37:
V O = ‘One night-it was on the twentieth of March, 1888-I was returning’
V O = ‘One night-it was on the twentieth of March, 1888-I was returning’
print(‘Encoded: ’straddle(O))
print(‘Encoded: ’straddle(O))
print(‘Decoded: ’unstraddle(straddle(O)).join(‘’))</lang>
print(‘Decoded: ’unstraddle(straddle(O)).join(‘’))</syntaxhighlight>


{{out}}
{{out}}
Line 50: Line 50:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<lang algol68>#!/usr/local/bin/a68g --script #
<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #


PRIO MIN=5, MAX=5;
PRIO MIN=5, MAX=5;
Line 167: Line 167:
))
))


)</lang>
)</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 176: Line 176:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>board := "
<syntaxhighlight lang="autohotkey">board := "
(
(
ET AON RIS
ET AON RIS
Line 216: Line 216:
}
}
}
}
MsgBox % dec</lang>
MsgBox % dec</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
{{libheader|GLib}}
{{libheader|GLib}}


<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 433: Line 433:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


===Shorter version===
===Shorter version===
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 523: Line 523:
decipher(enc, dec, 1); printf("decoded: %s\n", dec);
decipher(enc, dec, 1); printf("decoded: %s\n", dec);
return 0;
return 0;
}</lang>Output:<lang>message: In the winter 1965/we were hungry/just barely alive
}</syntaxhighlight>Output:<syntaxhighlight lang="text">message: In the winter 1965/we were hungry/just barely alive
encoded: 85621250626585107626916996966956265062650706225635247676226639162203702867623288640
encoded: 85621250626585107626916996966956265062650706225635247676226639162203702867623288640
decoded: IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
decoded: IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
Line 529: Line 529:
No spaces:
No spaces:
encoded: 851250658510769169969669562650650702563524767622663912037028673288640
encoded: 851250658510769169969669562650650702563524767622663912037028673288640
decoded: INTHEWINTER1965/WEWEREHUNGRY/JUSTBARELYALIVE</lang>
decoded: INTHEWINTER1965/WEWEREHUNGRY/JUSTBARELYALIVE</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Translation of [[Straddling_checkerboard#Java|Java]] via [[Straddling_checkerboard#D|D]]
Translation of [[Straddling_checkerboard#Java|Java]] via [[Straddling_checkerboard#D|D]]
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 579: Line 579:
}
}
}
}
}</lang>
}</syntaxhighlight>
<pre>139539363509369743061399059745399365901344308320791798798798367430685972839363935
<pre>139539363509369743061399059745399365901344308320791798798798367430685972839363935
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>
#include <map>
#include <map>
Line 673: Line 673:
return out;
return out;
}
}
};</lang>
};</syntaxhighlight>


Test program:
Test program:
<lang cpp>int main()
<syntaxhighlight lang="cpp">int main()
{
{
StraddlingCheckerboard sc("HOLMESRTABCDFGIJKNPQUVWXYZ./", 3, 7);
StraddlingCheckerboard sc("HOLMESRTABCDFGIJKNPQUVWXYZ./", 3, 7);
Line 689: Line 689:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


Output:
Output:
Line 700: Line 700:
=={{header|D}}==
=={{header|D}}==
Partially based on the PicoLisp version:
Partially based on the PicoLisp version:
<lang d>import std.stdio, std.algorithm, std.string, std.array;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.string, std.array;


immutable T = ["79|0|1|2|3|4|5|6|7|8|9", "|H|O|L||M|E|S||R|T",
immutable T = ["79|0|1|2|3|4|5|6|7|8|9", "|H|O|L||M|E|S||R|T",
Line 735: Line 735:
writeln("Encoded: ", O.straddle);
writeln("Encoded: ", O.straddle);
writeln("Decoded: ", O.straddle.unStraddle);
writeln("Decoded: ", O.straddle.unStraddle);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935
<pre>Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935
Line 743: Line 743:
{{trans|C++}}
{{trans|C++}}
Same output:
Same output:
<lang d>import std.stdio, std.algorithm, std.ascii;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.ascii;


struct StraddlingCheckerboard {
struct StraddlingCheckerboard {
Line 817: Line 817:
writeln("Encoded: ", en);
writeln("Encoded: ", en);
writeln("Decoded: ", sc.decode(en));
writeln("Decoded: ", sc.decode(en));
}</lang>
}</syntaxhighlight>


===Dictionary-Based Version===
===Dictionary-Based Version===
{{trans|Java}}
{{trans|Java}}
<lang d>import std.stdio, std.string, std.algorithm, std.regex, std.array, std.range, std.typecons;
<syntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.regex, std.array, std.range, std.typecons;


immutable string[const string] val2key, key2val;
immutable string[const string] val2key, key2val;
Line 848: Line 848:
s.encode.writeln;
s.encode.writeln;
s.encode.decode.writeln;
s.encode.decode.writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>139539363509369743061399059745399365901344308320791798798798367430685972839363935
<pre>139539363509369743061399059745399365901344308320791798798798367430685972839363935
Line 854: Line 854:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
(*
(*
Encode and Decode using StraddlingCheckerboard
Encode and Decode using StraddlingCheckerboard
Line 875: Line 875:
Seq.iter2 (fun ng gn->a.[(char i,char ng)]<-string gn;b.[gn]<-i+ng) z l
Seq.iter2 (fun ng gn->a.[(char i,char ng)]<-string gn;b.[gn]<-i+ng) z l
{n=char n;i=char i;g=a;e=b}
{n=char n;i=char i;g=a;e=b}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
{{out}}
{{out}}
<lang fsharp>
<syntaxhighlight lang="fsharp">
let N = G "2" "6" "ETAONRIS" "BCDFGHJKLM" "PQ/UVWXYZ." ["0";"1";"2";"3";"4";"5";"6";"7";"8";"9"]
let N = G "2" "6" "ETAONRIS" "BCDFGHJKLM" "PQ/UVWXYZ." ["0";"1";"2";"3";"4";"5";"6";"7";"8";"9"]
N.decode "450582425181653945125016505180125423293721256216286286288653970163758524"|>Seq.iter(fun n->printf "%s" n);; //ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
N.decode "450582425181653945125016505180125423293721256216286286288653970163758524"|>Seq.iter(fun n->printf "%s" n);; //ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
N.encode "IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE"|>Seq.iter(fun n->printfn "%s" n);; //8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640
N.encode "IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE"|>Seq.iter(fun n->printfn "%s" n);; //8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640
N.decode "8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640"|>Seq.iter(fun n->printf "%s" n);; //IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
N.decode "8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640"|>Seq.iter(fun n->printf "%s" n);; //IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 961: Line 961:
}
}
return
return
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 970: Line 970:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Char
<syntaxhighlight lang="haskell">import Data.Char
import Data.Map
import Data.Map


Line 1,017: Line 1,017:
in mapM_ putStrLn [ "Original: " ++ orig
in mapM_ putStrLn [ "Original: " ++ orig
, "Encoded: " ++ enc
, "Encoded: " ++ enc
, "Decoded: " ++ dec ]</lang>
, "Decoded: " ++ dec ]</syntaxhighlight>


{{out}}
{{out}}
Line 1,026: Line 1,026:


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main()
<syntaxhighlight lang="icon">procedure main()
StraddlingCheckerBoard("setup","HOLMESRTABCDFGIJKNPQUVWXYZ./", 3,7)
StraddlingCheckerBoard("setup","HOLMESRTABCDFGIJKNPQUVWXYZ./", 3,7)


Line 1,066: Line 1,066:
}
}
}
}
end</lang>
end</syntaxhighlight>


Output:<pre>text = "One night. it was on the twentieth of March, 1888. I was returning"
Output:<pre>text = "One night. it was on the twentieth of March, 1888. I was returning"
Line 1,074: Line 1,074:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j>'Esc Stop'=: '/.'
<syntaxhighlight lang="j">'Esc Stop'=: '/.'
'Nums Alpha'=: '0123456789';'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'Nums Alpha'=: '0123456789';'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Charset=: Nums,Alpha,Stop
Charset=: Nums,Alpha,Stop
Line 1,098: Line 1,098:
}.board {~ _2 (_&".)\ idx
}.board {~ _2 (_&".)\ idx
end.
end.
)</lang>
)</syntaxhighlight>
'''Example usage:'''
'''Example usage:'''
<lang j> preprocess=: (#~ Charset e.~ ])@toupper NB. verb to compress out non-alphanumerics
<syntaxhighlight lang="j"> preprocess=: (#~ Charset e.~ ])@toupper NB. verb to compress out non-alphanumerics
chkbrdRC=: chkbrd (3 7;'HOLMESRTABCDFGIJKNPQUVWXYZ./') NB. define adverb by applying Rosetta Code key to chkbrd conjunction
chkbrdRC=: chkbrd (3 7;'HOLMESRTABCDFGIJKNPQUVWXYZ./') NB. define adverb by applying Rosetta Code key to chkbrd conjunction
0 chkbrdRC preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
0 chkbrdRC preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
139539363509369743061399059745399365901344308320791798798798367430685972839363935
139539363509369743061399059745399365901344308320791798798798367430685972839363935
1 chkbrdRC 0 chkbrdRC preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
1 chkbrdRC 0 chkbrdRC preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</lang>
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</syntaxhighlight>
Or using the rules proposed by [[User:Util|Util]] on the discussion page:
Or using the rules proposed by [[User:Util|Util]] on the discussion page:
<lang j> preprocess=: Stop&([`([: I. Charset -.@e.~ ])`]} toupper) NB. replace all non-alphanumerics with Stop
<syntaxhighlight lang="j"> preprocess=: Stop&([`([: I. Charset -.@e.~ ])`]} toupper) NB. replace all non-alphanumerics with Stop
1 chkbrdRC 0 chkbrdRC preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
1 chkbrdRC 0 chkbrdRC preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
ONE.NIGHT.IT.WAS.ON.THE.TWENTIETH.OF.MARCH..1888.I.WAS.RETURNING</lang>
ONE.NIGHT.IT.WAS.ON.THE.TWENTIETH.OF.MARCH..1888.I.WAS.RETURNING</syntaxhighlight>




Line 1,115: Line 1,115:


'''Solution:'''
'''Solution:'''
<lang j>NB. stops setcode alphabet
<syntaxhighlight lang="j">NB. stops setcode alphabet
NB. creates verbs encode and decode which change between unencoded text and lists of encoded numbers
NB. creates verbs encode and decode which change between unencoded text and lists of encoded numbers
setcode=: 3 :0
setcode=: 3 :0
Line 1,131: Line 1,131:
decode=: (alphabet {~ keys i. break f.) :. encode
decode=: (alphabet {~ keys i. break f.) :. encode
i.0 0
i.0 0
)</lang>
)</syntaxhighlight>


'''Example usage:'''
'''Example usage:'''
<lang j> 3 7 setcode 'HOLMESRTABCDFGIJKNPQUVWXYZ./'
<syntaxhighlight lang="j"> 3 7 setcode 'HOLMESRTABCDFGIJKNPQUVWXYZ./'
preprocess=: (#~ alphabet e.~ ])@toupper
preprocess=: (#~ alphabet e.~ ])@toupper
,":"0 encode message=: preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
,":"0 encode message=: preprocess 'One night-it was on the twentieth of March, 1888-I was returning'
Line 1,143: Line 1,143:
OWVKRNEOAMTMXROWOHTMTMTROTQ4SEMRRLRZLVSTTLLOROMHALSFOHECMRWESWEE
OWVKRNEOAMTMXROWOHTMTMTROTQ4SEMRRLRZLVSTTLLOROMHALSFOHECMRWESWEE
((10|-) 0 4 5 2$~$)&.encode s
((10|-) 0 4 5 2$~$)&.encode s
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</lang>
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|7}}
{{works with|Java|7}}
<lang java>import java.util.HashMap;
<syntaxhighlight lang="java">import java.util.HashMap;
import java.util.Map;
import java.util.Map;
import java.util.regex.*;
import java.util.regex.*;
Line 1,194: Line 1,194:
return sb.toString();
return sb.toString();
}
}
}</lang>
}</syntaxhighlight>


<pre>139539363509369743061399059745399365901344308320791798798798367430685972839363935
<pre>139539363509369743061399059745399365901344308320791798798798367430685972839363935
Line 1,200: Line 1,200:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript><script>
<syntaxhighlight lang="javascript"><script>
var alphabet=new Array("ESTONIA R","BCDFGHJKLM","PQUVWXYZ./") // scramble alphabet as you wish
var alphabet=new Array("ESTONIA R","BCDFGHJKLM","PQUVWXYZ./") // scramble alphabet as you wish
var prefixes=new Array("",alphabet[0].indexOf(" "),alphabet[0].lastIndexOf(" "))
var prefixes=new Array("",alphabet[0].indexOf(" "),alphabet[0].lastIndexOf(" "))
Line 1,240: Line 1,240:
document.writeln(straddle(str))
document.writeln(straddle(str))
document.writeln(unstraddle(straddle(str)))
document.writeln(unstraddle(straddle(str)))
</script></lang>
</script></syntaxhighlight>


Output:<pre>One night-it was on the twentieth of March, 1888-I was returning.
Output:<pre>One night-it was on the twentieth of March, 1888-I was returning.
Line 1,249: Line 1,249:
Unlike the precomputed table versions, this version takes a 30-character string specifying the 3 rows of the checkerboard
Unlike the precomputed table 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.
as an argument, recomputing the lookup table each run, which allows easier changes of keys without modifying the code.
<lang julia>
<syntaxhighlight lang="julia">
function straddlingcheckerboard(board, msg, doencode)
function straddlingcheckerboard(board, msg, doencode)
lookup = Dict()
lookup = Dict()
Line 1,315: Line 1,315:
decoded = straddlingcheckerboard(btable, encoded, false)
decoded = straddlingcheckerboard(btable, encoded, false)
println("Original: $message\nEncoded: $encoded\nDecoded: $decoded")
println("Original: $message\nEncoded: $encoded\nDecoded: $decoded")
</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Line 1,324: Line 1,324:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.2.0
<syntaxhighlight lang="scala">// version 1.2.0


val board = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
val board = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
Line 1,404: Line 1,404:
println("Decrypted : $decrypted")
println("Decrypted : $decrypted")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,430: Line 1,430:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>
<syntaxhighlight lang="lua">
local brd = { "HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./" }
local brd = { "HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./" }
local dicE, dicD, s1, s2 = {}, {}, 0, 0
local dicE, dicD, s1, s2 = {}, {}, 0, 0
Line 1,503: Line 1,503:
-- entry point
-- entry point
enterText()
enterText()
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,524: Line 1,524:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
module Straddling_checkerboard {
module Straddling_checkerboard {
function encrypt$(message$, alphabet) {
function encrypt$(message$, alphabet) {
Line 1,648: Line 1,648:
}
}
Straddling_checkerboard
Straddling_checkerboard
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,686: Line 1,686:
=={{header|Nim}}==
=={{header|Nim}}==
I used the same example as in the Go solution (and, fortunately, got the same result).
I used the same example as in the Go solution (and, fortunately, got the same result).
<lang Nim>import strutils, tables
<syntaxhighlight lang="nim">import strutils, tables


const
const
Line 1,776: Line 1,776:
let crypted = board.encrypt(message)
let crypted = board.encrypt(message)
echo "Crypted: ", crypted
echo "Crypted: ", crypted
echo "Decrypted: ", board.decrypt(crypted)</lang>
echo "Decrypted: ", board.decrypt(crypted)</syntaxhighlight>


{{out}}
{{out}}
Line 1,785: Line 1,785:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,835: Line 1,835:
say 'Original: ', my $original = 'One night-it was on the twentieth of March, 1888-I was returning';
say 'Original: ', my $original = 'One night-it was on the twentieth of March, 1888-I was returning';
say 'Encoded: ', my $en = encode($original);
say 'Encoded: ', my $en = encode($original);
say 'Decoded: ', decode($en);</lang>
say 'Decoded: ', decode($en);</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
<pre> 0 1 2 3 4 5 6 7 8 9
Line 1,848: Line 1,848:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|C}}
{{trans|C}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">read_table</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">cb</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">read_table</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">cb</span><span style="color: #0000FF;">)</span>
Line 1,928: Line 1,928:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"encoded: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">enc</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"encoded: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">enc</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"decoded: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dec</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"decoded: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dec</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,943: Line 1,943:
{{trans|Java}}
{{trans|Java}}
{{works with|PHP 7}}
{{works with|PHP 7}}
<lang php>$key2val = ["A"=>"30", "B"=>"31", "C"=>"32", "D"=>"33", "E"=>"5", "F"=>"34", "G"=>"35",
<syntaxhighlight lang="php">$key2val = ["A"=>"30", "B"=>"31", "C"=>"32", "D"=>"33", "E"=>"5", "F"=>"34", "G"=>"35",
"H"=>"0", "I"=>"36", "J"=>"37", "K"=>"38", "L"=>"2", "M"=>"4", "."=>"78", "N"=>"39",
"H"=>"0", "I"=>"36", "J"=>"37", "K"=>"38", "L"=>"2", "M"=>"4", "."=>"78", "N"=>"39",
"/"=>"79", "O"=>"1", "0"=>"790", "P"=>"70", "1"=>"791", "Q"=>"71", "2"=>"792",
"/"=>"79", "O"=>"1", "0"=>"790", "P"=>"70", "1"=>"791", "Q"=>"71", "2"=>"792",
Line 1,977: Line 1,977:
}
}


main('One night-it was on the twentieth of March, 1888-I was returning');</lang>
main('One night-it was on the twentieth of March, 1888-I was returning');</syntaxhighlight>
<pre>
<pre>
139539363509369743061399059745399365901344308320791798798798367430685972839363935
139539363509369743061399059745399365901344308320791798798798367430685972839363935
Line 1,984: Line 1,984:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de *Straddling
<syntaxhighlight lang="picolisp">(de *Straddling
(NIL "H" "O" "L" NIL "M" "E" "S" NIL "R" "T")
(NIL "H" "O" "L" NIL "M" "E" "S" NIL "R" "T")
("3" "A" "B" "C" "D" "F" "G" "I" "J" "K" "N")
("3" "A" "B" "C" "D" "F" "G" "I" "J" "K" "N")
Line 2,011: Line 2,011:
(get (cdr @) (inc (format (pop 'L))))
(get (cdr @) (inc (format (pop 'L))))
(get (cdar *Straddling) (inc (format C))) ) )
(get (cdar *Straddling) (inc (format C))) ) )
(link (if (= "/" C) (pop 'L) C)) ) ) ) ) )</lang>
(link (if (= "/" C) (pop 'L) C)) ) ) ) ) )</syntaxhighlight>
Output:
Output:
<pre>: (straddle "One night-it was on the twentieth of March, 1888-I was returning")
<pre>: (straddle "One night-it was on the twentieth of March, 1888-I was returning")
Line 2,020: Line 2,020:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang purebasic>Procedure.s encrypt_SC(originalText.s, alphabet.s, blank_1, blank_2)
<syntaxhighlight lang="purebasic">Procedure.s encrypt_SC(originalText.s, alphabet.s, blank_1, blank_2)
Static notEscape.s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ."
Static notEscape.s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ."
Protected preDigit_1.s = Str(blank_1), preDigit_2.s = Str(blank_2)
Protected preDigit_1.s = Str(blank_1), preDigit_2.s = Str(blank_2)
Line 2,115: Line 2,115:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>
Sample output:
Sample output:
<pre>Original: One night-it was on the twentieth of March, 1888-I was returning
<pre>Original: One night-it was on the twentieth of March, 1888-I was returning
Line 2,124: Line 2,124:
=={{header|Python}}==
=={{header|Python}}==
Partially based on the PicoLisp version:
Partially based on the PicoLisp version:
<lang python>T = [["79", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
<syntaxhighlight lang="python">T = [["79", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
["", "H", "O", "L", "", "M", "E", "S", "", "R", "T"],
["", "H", "O", "L", "", "M", "E", "S", "", "R", "T"],
["3", "A", "B", "C", "D", "F", "G", "I", "J", "K", "N"],
["3", "A", "B", "C", "D", "F", "G", "I", "J", "K", "N"],
Line 2,144: Line 2,144:
O = "One night-it was on the twentieth of March, 1888-I was returning"
O = "One night-it was on the twentieth of March, 1888-I was returning"
print "Encoded:", straddle(O)
print "Encoded:", straddle(O)
print "Decoded:", "".join(unstraddle(straddle(O)))</lang>
print "Decoded:", "".join(unstraddle(straddle(O)))</syntaxhighlight>
Output:
Output:
<pre>Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935
<pre>Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935
Line 2,153: Line 2,153:


We store the straddling checkerboard in a structure, so it can be reused or changed for the different examples. The “constructor” transforms the lines of the checkboard into an internal representation.
We store the straddling checkerboard in a structure, so it can be reused or changed for the different examples. The “constructor” transforms the lines of the checkboard into an internal representation.
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(struct *straddling (header main original)
(struct *straddling (header main original)
Line 2,181: Line 2,181:
(*straddling (list->vector (list* "?" "" header-tail)) main lines))
(*straddling (list->vector (list* "?" "" header-tail)) main lines))
(define escape (straddling-encode-char #\/ temporal-board))
(define escape (straddling-encode-char #\/ temporal-board))
(*straddling (list->vector (list* escape "" header-tail)) main lines))</lang>
(*straddling (list->vector (list* escape "" header-tail)) main lines))</syntaxhighlight>
Now we define the functions to straddle and unstraddle the message.
Now we define the functions to straddle and unstraddle the message.
<lang Racket>(define (straddling-encode-char char board)
<syntaxhighlight lang="racket">(define (straddling-encode-char char board)
(or (for/or ([head (in-vector (*straddling-header board))]
(or (for/or ([head (in-vector (*straddling-header board))]
[line (in-vector (*straddling-main board))])
[line (in-vector (*straddling-main board))])
Line 2,222: Line 2,222:
(values #f (cons decoded rev-ret))))))])
(values #f (cons decoded rev-ret))))))])
(unless row ;check that last number was not missing
(unless row ;check that last number was not missing
rev-ret)))))</lang>
rev-ret)))))</syntaxhighlight>
'''Two examples:'''
'''Two examples:'''
<lang Racket>(define (test-straddling message board)
<syntaxhighlight lang="racket">(define (test-straddling message board)
(let* ([encoded (straddle message board)]
(let* ([encoded (straddle message board)]
[decoded (unstraddle encoded board)])
[decoded (unstraddle encoded board)])
Line 2,240: Line 2,240:
(straddling "ET AON RIS"
(straddling "ET AON RIS"
"BCDFGHJKLM"
"BCDFGHJKLM"
"PQ/UVWXYZ."))</lang>
"PQ/UVWXYZ."))</syntaxhighlight>
{{out}}
{{out}}
<pre>#<straddling ("HOL MES RT" "ABCDFGIJKN" "PQUVWXYZ./")>
<pre>#<straddling ("HOL MES RT" "ABCDFGIJKN" "PQUVWXYZ./")>
Line 2,258: Line 2,258:


We build the full table during .new, which simplifies .encode and .decode.
We build the full table during .new, which simplifies .encode and .decode.
<lang perl6>class Straddling_Checkerboard {
<syntaxhighlight lang="raku" line>class Straddling_Checkerboard {
has @!flat_board; # 10x3 stored as 30x1
has @!flat_board; # 10x3 stored as 30x1
has $!plain2code; # full translation table, invertable
has $!plain2code; # full translation table, invertable
Line 2,317: Line 2,317:
say "Decoded: $de";
say "Decoded: $de";
}
}
}</lang>
}</syntaxhighlight>


Output:<pre> 0 1 2 3 4 5 6 7 8 9
Output:<pre> 0 1 2 3 4 5 6 7 8 9
Line 2,339: Line 2,339:
:* &nbsp; no hard-coding of the location of the escape character,
:* &nbsp; no hard-coding of the location of the escape character,
:* &nbsp; support the usage of a blank in the 1<sup>st</sup> character (the top line of the table).
:* &nbsp; support the usage of a blank in the 1<sup>st</sup> character (the top line of the table).
<lang rexx>/*REXX program uses the straddling checkerboard cipher to encrypt/decrypt a message. */
<syntaxhighlight lang="rexx">/*REXX program uses the straddling checkerboard cipher to encrypt/decrypt a message. */
parse arg msg /*obtain optional message from the C.L.*/
parse arg msg /*obtain optional message from the C.L.*/
if msg='' then msg= 'One night-it was the twentieth of March, 1888-I was returning'
if msg='' then msg= 'One night-it was the twentieth of March, 1888-I was returning'
Line 2,376: Line 2,376:
if pos(x, @.r)\==0 then do; j= j+1; rw=x; x=substr(!, j, 1); end
if pos(x, @.r)\==0 then do; j= j+1; rw=x; x=substr(!, j, 1); end
$= $ || substr(@.rw, x+1, 1) /*add a character to decrypted message.*/
$= $ || substr(@.rw, x+1, 1) /*add a character to decrypted message.*/
end /*j*/; return $</lang>
end /*j*/; return $</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,386: Line 2,386:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{works with|Ruby|2.0}}
{{works with|Ruby|2.0}}
<lang ruby>class StraddlingCheckerboard
<syntaxhighlight lang="ruby">class StraddlingCheckerboard
EncodableChars = "A-Z0-9."
EncodableChars = "A-Z0-9."
SortedChars = " ./" + [*"A".."Z"].join
SortedChars = " ./" + [*"A".."Z"].join
Line 2,446: Line 2,446:
"#<%s board=%p, row_labels=%p, mapping=%p>" % [self.class, to_s, @row_labels, @mapping]
"#<%s board=%p, row_labels=%p, mapping=%p>" % [self.class, to_s, @row_labels, @mapping]
end
end
end</lang>
end</syntaxhighlight>


The test suite
The test suite
<lang ruby>require 'test/unit'
<syntaxhighlight lang="ruby">require 'test/unit'
class StraddlingCheckerboardTest < Test::Unit::TestCase
class StraddlingCheckerboardTest < Test::Unit::TestCase
def setup
def setup
Line 2,488: Line 2,488:
assert_raise(ArgumentError) {StraddlingCheckerboard.new "ET ON RISBCDFGHJKLMPQ/UVWXYZ.!"}
assert_raise(ArgumentError) {StraddlingCheckerboard.new "ET ON RISBCDFGHJKLMPQ/UVWXYZ.!"}
end
end
end</lang>
end</syntaxhighlight>


output
output
Line 2,507: Line 2,507:
=={{header|Scala}}==
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/xCHsvaC/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/42qQPkm6TiONpAOC1ByTCg Scastie (remote JVM)].
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/xCHsvaC/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/42qQPkm6TiONpAOC1ByTCg Scastie (remote JVM)].
<lang Scala>object StraddlingCheckerboard extends App {
<syntaxhighlight lang="scala">object StraddlingCheckerboard extends App {


private val dictonary = Map("H" -> "0", "O" -> "1",
private val dictonary = Map("H" -> "0", "O" -> "1",
Line 2,532: Line 2,532:
println(enc)
println(enc)
println(decode(enc))
println(decode(enc))
}</lang>
}</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Structured as a class, the instances of which contain encoding and decoding mappings suitable for use with Tcl's built in string remapping engine. This uses the fact that no sequence of digits in the encoded form that maps to one character is a prefix of the sequence for any other character. (Inspired by the ''description'' of the [[#Raku|Raku]] solution.)<!--not a translation though; I didn't read the Raku code…-->
Structured as a class, the instances of which contain encoding and decoding mappings suitable for use with Tcl's built in string remapping engine. This uses the fact that no sequence of digits in the encoded form that maps to one character is a prefix of the sequence for any other character. (Inspired by the ''description'' of the [[#Raku|Raku]] solution.)<!--not a translation though; I didn't read the Raku code…-->
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


oo::class create StraddlingCheckerboardCypher {
oo::class create StraddlingCheckerboardCypher {
Line 2,571: Line 2,571:
string map $decmap [regsub -all {[^0-9]} $msg ""]
string map $decmap [regsub -all {[^0-9]} $msg ""]
}
}
}</lang>
}</syntaxhighlight>
Demonstration code:
Demonstration code:
<lang tcl>StraddlingCheckerboardCypher create demo {
<syntaxhighlight lang="tcl">StraddlingCheckerboardCypher create demo {
{{} E T {} A O N {} R I S}
{{} E T {} A O N {} R I S}
{2 B C D F G H J K L M}
{2 B C D F G H J K L M}
Line 2,583: Line 2,583:
puts "Input: $input"
puts "Input: $input"
puts "Encoded: $encrypted"
puts "Encoded: $encrypted"
puts "Decoded: $output"</lang>
puts "Decoded: $output"</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 2,593: Line 2,593:
=={{header|VBScript}}==
=={{header|VBScript}}==
{{trans|PureBasic}}
{{trans|PureBasic}}
<lang vb>' Straddling checkerboard - VBScript - 19/04/2019
<syntaxhighlight lang="vb">' Straddling checkerboard - VBScript - 19/04/2019


Function encrypt(ByVal originalText, ByVal alphabet, blank1, blank2)
Function encrypt(ByVal originalText, ByVal alphabet, blank1, blank2)
Line 2,680: Line 2,680:
decoded = decrypt(encoded, cipher, 3, 7)
decoded = decrypt(encoded, cipher, 3, 7)
Buffer=Buffer & "decoded: " & decoded & vbCrlf
Buffer=Buffer & "decoded: " & decoded & vbCrlf
Wscript.Echo Buffer </lang>
Wscript.Echo Buffer </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,691: Line 2,691:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-str}}
{{libheader|Wren-str}}
<lang ecmascript>import "/str" for Str
<syntaxhighlight lang="ecmascript">import "/str" for Str


var board = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
var board = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
Line 2,772: Line 2,772:
System.print("Encrypted : %(encrypted)")
System.print("Encrypted : %(encrypted)")
System.print("Decrypted : %(decrypted)")
System.print("Decrypted : %(decrypted)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,799: Line 2,799:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|D}}
{{trans|D}}
<lang zkl>var [const]
<syntaxhighlight lang="zkl">var [const]
val2key=Dictionary(
val2key=Dictionary(
"A","30", "B","31", "C","32", "D","33", "E","5", "F","34", "G","35",
"A","30", "B","31", "C","32", "D","33", "E","5", "F","34", "G","35",
Line 2,810: Line 2,810:
fcn encode(txt){ txt.toUpper().pump(String,val2key.find.fp1("")) }
fcn encode(txt){ txt.toUpper().pump(String,val2key.find.fp1("")) }
fcn decode(str){ RegExp("79.|3.|7.|.").pump(str,String,key2val.get) }</lang>
fcn decode(str){ RegExp("79.|3.|7.|.").pump(str,String,key2val.get) }</syntaxhighlight>
<lang zkl>txt:="One night-it was on the twentieth of March, 1888-I was returning";
<syntaxhighlight lang="zkl">txt:="One night-it was on the twentieth of March, 1888-I was returning";
str:=encode(txt);
str:=encode(txt);
println("Encoded: ", str);
println("Encoded: ", str);
println("Decoded: ", decode(str));</lang>
println("Decoded: ", decode(str));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>