KATHRYN and her school friends have been using a Lorenz-type code to pass covert messages to each other. Each letter is expressed as a five-digit binary number such that A = 1 = 00001, M = 13 = 01101 and so on, but other symbols are represented by 00000 and by 11011 upwards.
A fixed letter, say M, is chosen as a “coder”, known only to the sender and receiver. To transmit a letter, say D, it is added to the coder by the “exclusive-NOR” rule 1 + 1 = 1, 1 + 0 = 0, 0 + 1 = 0, 0 + 0 = 1. So, for example, D + M = 00100 + 01101 = 10110 = V. When the sent letter V is added by the recipient to the coder M, the original letter reappears: 10110 + 01101 = 00100.
She has sent her name to her friends as seven letters. KATHRYN and its coded version together consist of 14 different letters.
What was the coded version?
Note: Adapted from Enigma Number:1764 by Adrian Sommerfield, which appered in the New Scientist on 28 August, 2013.
Some combinations of a letter and a coder produce a 5 digit binary which is not between 1 and 26.
All of the coders: A, B, I, K, O, Q, S, W, X produce 14 unique characters when combined with KATHRYN, but in each of these, at least one of the binaries is outside the range 1 to 26.
Only when the coder is 'D' as in the example, is the encoding of KATHRYN 7 other distinct letters: PZOSIBU.
Program output:
A U_JVLGP
B V\IUODS
D PZOSIBU
I ]WB^DOX
K _U@\FMZ
O [QDXBI^
Q EOZF\W@
S GMXD^UB
W CI\@ZQF
X LFSOU^I
---------------
def xnor(a,b):
""" a and b are either '0' or '1'; return exclusive NOR """
if a not in ['0','1'] or b not in ['0','1']:
print('err')
return None
if a == b:
return '1'
elif a != b:
return '0'
else:
print('how did i get here')
return None
def lorenzeCode(word, alpha_coder):
if type(alpha_coder) != str:
return
if len(alpha_coder) != 1:
return
word = word.upper()
coder = base2base( (ord(alpha_coder.upper()) - 64),10,2 ).zfill(5)
ans = ''
for ch in word:
binchar = base2base( (ord(ch) - 64),10,2 ).zfill(5)
binans = ''
for i in range(5):
binans += xnor(binchar[i], coder[i])
ans += chr((base2base(binans,2,10)) + 64)
return ans
name = 'KATHRYN'
for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
codename = lorenzeCode(name, letter)
if len(set(codename + name)) == 14:
print(letter, codename)
|
Posted by Larry
on 2024-09-06 09:51:33 |