Skip to main content
deleted 50 characters in body
Source Link

Python 3, 709 705 685 664 662 615615 569

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import I,re,string
n=rangea=string.ascii_uppercase
k=inputd=lambda x:I.product()
p=inputrange(5),repeat=x)
t=lambda x:x(input()+x).upper().replace('J','I')
s=[]s=''
for _ in t(k+aa):
 if _ not in s and _ in a:s.append(_)s+=_
m=[s[i:i+5]for i in nrange(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=22)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(n(5),repeat=22)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=44)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p'')if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY

Python 3, 709 705 685 664 662 615

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x:x.upper().replace('J','I')
s=[]
for _ in t(k+a):
 if _ not in s and _ in a:s.append(_)
m=[s[i:i+5]for i in n(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=2)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(n(5),repeat=2)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=4)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p)if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY

Python 3, 709 705 685 664 662 615 569

With @ceilingcat's improvements. Accepts input from stdin.

import itertools as I,re,string
a=string.ascii_uppercase
d=lambda x:I.product(range(5),repeat=x)
t=lambda x:(input()+x).upper().replace('J','I')
s=''
for _ in t(a):
 if _ not in s and _ in a:s+=_
m=[s[i:i+5]for i in range(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(2)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(2)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(4)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t('')if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY
deleted 1 character in body
Source Link

Python 3, 709 705 685 664 662 616615

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x:x.upper().replace('J','I')
s=[]
for _ in t(k+a):
 if _ not in s and _ in a:s.append(_)
 m=[s[i:i+5]for i in n(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=2)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(n(5),repeat=2)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=4)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p)if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY

Python 3, 709 705 685 664 662 616

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x:x.upper().replace('J','I')
s=[]
for _ in t(k+a):
 if _ not in s and _ in a:s.append(_)
 m=[s[i:i+5]for i in n(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=2)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(n(5),repeat=2)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=4)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p)if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY

Python 3, 709 705 685 664 662 615

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x:x.upper().replace('J','I')
s=[]
for _ in t(k+a):
 if _ not in s and _ in a:s.append(_)
m=[s[i:i+5]for i in n(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=2)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(n(5),repeat=2)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=4)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p)if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY
added 9 characters in body
Source Link

Python 3, 709 705 685 664 662662 616

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x: x.upper().replace('J','I')
s=[]
for _ in t(k+a):
 if _ not in s and _ in a: s.append(_)
 m=[s[i:i+5] fori+5]for i in n(0,len(s),5)]
e={r[i]+r[j]:r[(i+1)%5]+r[(j+1)%5] for-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=2) if i!=j-j}
e.update({c[i]+c[j]:c[(i+1)%5]+c[(j+1)%5] for-~i%5]+c[-~j%5]for c in zip(*m) for i,j in d(n(5),repeat=2) if i!=j-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r] form[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=4) if (q!=u and -u)*(r!=v-v)})
l=reprint(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p) if _ in a]))
print(''.join(e[a+(b if b else 'X')] for a,b in l))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY

Python 3, 709 705 685 664 662

Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x: x.upper().replace('J','I')
s=[]
for _ in t(k+a):
if _ not in s and _ in a: s.append(_)
m=[s[i:i+5] for i in n(0,len(s),5)]
e={r[i]+r[j]:r[(i+1)%5]+r[(j+1)%5] for r in m for i,j in d(n(5),repeat=2) if i!=j}
e.update({c[i]+c[j]:c[(i+1)%5]+c[(j+1)%5] for c in zip(*m) for i,j in d(n(5),repeat=2) if i!=j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r] for q,r,u,v in d(n(5),repeat=4) if q!=u and r!=v})
l=re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p) if _ in a]))
print(''.join(e[a+(b if b else 'X')] for a,b in l))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY

Python 3, 709 705 685 664 662 616

With @ceilingcat's improvements. Accepts input from stdin.

from string import ascii_uppercase as a
from itertools import product as d
import re
n=range
k=input()
p=input()
t=lambda x:x.upper().replace('J','I')
s=[]
for _ in t(k+a):
 if _ not in s and _ in a:s.append(_)
 m=[s[i:i+5]for i in n(0,len(s),5)]
e={r[i]+r[j]:r[-~i%5]+r[-~j%5]for r in m for i,j in d(n(5),repeat=2)if i-j}
e.update({c[i]+c[j]:c[-~i%5]+c[-~j%5]for c in zip(*m)for i,j in d(n(5),repeat=2)if i-j})
e.update({m[q][r]+m[u][v]:m[q][v]+m[u][r]for q,r,u,v in d(n(5),repeat=4)if(q-u)*(r-v)})
print(''.join(e[a+(b if b else'X')]for a,b in re.findall(r'(.)(?:(?!\1)(.))?',''.join([_ for _ in t(p)if _ in a]))))

Example:

mfukar@oxygen[/tmp]<>$ python playfair.py
Stack Overflow
The cat crept into the crypt, crapped, and crept out again.
SIRACARDFMVUICVSMORDZNAKECMZMFBCYNRDFMSVTVKBTMMY
deleted 11 characters in body
Source Link
Loading
Shorter function names.
Source Link
Loading
Source Link
Loading