Bacon cipher: Difference between revisions

added MiniScript example
(added MiniScript example)
Line 1,301:
FALL OVER DEAD NOT REALLY BUT GETS SYPATHY
</pre>
 
=={{header|MiniScript}}==
<lang MiniScript>c = {}
c["a"] = "AAAAA"; c["b"] = "AAAAB"; c["c"] = "AAABA"; c["d"] = "AAABB"; c["e"] = "AABAA"; c["f"] = "AABAB";
c["g"] = "AABBA"; c["h"] = "AABBB"; c["i"] = "ABAAA"; c["j"] = "ABAAB"; c["k"] = "ABABA"; c["l"] = "ABABB";
c["m"] = "ABBAA"; c["n"] = "ABBAB"; c["o"] = "ABBBA"; c["p"] = "ABBBB"; c["q"] = "BAAAA"; c["r"] = "BAAAB";
c["s"] = "BAABA"; c["t"] = "BAABB"; c["u"] = "BABAA"; c["v"] = "BABAB"; c["w"] = "BABBA"; c["x"] = "BABBB";
c["y"] = "BBAAA"; c["z"] = "BBAAB"; c[" "] = "BBBAA";
codeMap = c // (used "c" above just to make the lines shorter)
decodeMap = {}
for kv in codeMap
decodeMap[kv.value] = kv.key
end for
 
message = "Computers are everywhere. There are the obvious ones, like your smart phone or game system. "
message = message + "There are less obvious ones, like in your microwave or your bedside clock. And then "
message = message + "there are the really invisible ones, like the 50 or so small computers in your car."
message = message + "All these computers work in pretty much the same way."
message = message.values // (convert to list)
 
encodeChar = function(ch)
out = []
for bit in codeMap[ch]
while message[0].upper == message[0].lower
out.push message.pull // (not a letter we can use; pass through as-is)
end while
if bit == "A" then out.push message.pull.upper else out.push message.pull.lower
end for
return out
end function
encode = function(s)
out = []
for ch in s
out = out + encodeChar(ch)
end for
return out.join("")
end function
 
decodeChar = function(encodedChar)
code = ""
for ch in encodedChar
if ch == ch.upper then code = code + "A" else code = code + "B"
end for
if decodeMap.hasIndex(code) then return decodeMap[code]
return "?"
end function
 
decode = function(s)
// strip out all non-letters
schars = s.values
for i in range(schars.len-1, 0)
if schars[i].lower == schars[i].upper then schars.remove i
end for
s = schars.join("")
// now, decode using groups of 5 characters
out = []
for i in range(0, s.len-1, 5)
out.push decodeChar(s[i:i+5])
end for
return out.join("")
end function
 
codedMsg = encode("the quick brown fox jumps over the lazy dog")
print codedMsg
print decode(codedMsg)</lang>
 
{{out}}
<pre>cOMpuTErs aRE eVErywHErE. THErE aRE ThE OBVIOuS OnEs, Like YOUR SMarT PHoNe or GaMe sYSteM. therE ARE lEsS obvIoUs ones, lIKE iN YouR mICRowAVE or youR BeDsidE CLock. AnD tHeN ThERe ARE the rEAlLY inVIsibLE oNEs, liKE ThE 50 or SO SMAll COmpuTERs in YOUR Car.All tHESe cO
the quick brown fox jumps over the lazy dog</pre>
 
 
=={{header|Perl}}==
222

edits