({.={:) works the best: not only does it give the desired functionality, but
it inserts a fill at the end for odd numbers of elements.

   preprocess=: (#!.'x'~ 1 j. i.@# = (2* 1 i.~ _2 ({.={:)\ ])) ::]^:_

   preprocess 'allisdiscoveredflee'
allisdiscoveredfleex
   preprocess 'xallisdiscoveredflee'
xalxlisdiscoveredfleex
   preprocess 'xallisdiscoveredxflee'
xalxlisdiscoveredxflexex

Even running
preprocess 'x'
will go forever with this. The Playfair cipher has no mechanism for dealing
with double x's or ending x's. Best is just to not put any x's in your
initial string.

Also, the preprocessor should replace all j's with i's:

   replace_j_i=: (=&'j')`(,:&'i')}

   (,: replace_j_i) (97+i.26){a.
abcdefghijklmnopqrstuvwxyz
abcdefghiiklmnopqrstuvwxyz

Marshall

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Henry Rich
Sent: Sunday, February 20, 2011 9:18 AM
To: Programming forum
Subject: Re: [Jprogramming] Playfair cipher

(=/`0:@.(2|#))

could be   =/@:(2&{.)

but if it doesn't matter what the result is when there is one character 
(as it appears to my eye), ({.=}.) would also work.  Best might be to 
use fill: =/"1@:(_2&(]\))

Insert-at-index is a pain, and I don't know of any better way than using #.

I spent some time thinking of a way to do this without ^:_ with no luck. 
  I'd like to see one.

I'm afraid to run preprocess on an input of 'xxx': it looks like a slow 
way to out-of-memory.

Henry Rich

On 2/19/2011 9:16 PM, Marshall Lochbaum wrote:
>     dblprocess=: (#!.'x'~ 1 j. i.@# = (2* 1 i.~ (_2 (=/`0:@.(2|#))\ ])))
> ::]^:_
>     preprocess=: [: ,&'x'^:(1=2|#) dblprocess
>
> Provides a moderately blunt preprocessor. Things I'd like fixed:
> (_2 (=/`0:@.(2|#))\ ])) should just be (_2 =/\ ]) , but on an odd length
> string the last part, say (=/\ 'e') , gives a single character and not a
> Boolean, resulting in a domain error.
> Does anyone know a better way to do insertion with an index than using # ?
>
>     preprocess 'allisdiscoveredflee'
> allisdiscoveredfleex
>     preprocess 'xallisdiscoveredflee'
> xalxlisdiscoveredfleex
>     preprocess 'xallisdiscoveredxflee'
> xalxlisdiscoveredxflexex
>
> Marshall
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Henry Rich
> Sent: Saturday, February 19, 2011 1:40 PM
> To: Programming forum
> Subject: Re: [Jprogramming] Playfair cipher
>
> Oh yeah,
>
>      pf =. 'paswo','rdbce','fghik','lmnqt',:'uvxyz'
>
> (thanks Ric)
>
> Henry Rich
>
> On 2/19/2011 11:39 AM, Henry Rich wrote:
>> I worked on the easy part:
>>
>>       pfr =. ((] (<"1@[ { ])~ ((0 1&(|."_1)&.|:@[)`(-"1 |.)@.(+./@])
>> =/) @ ($@] #: (i.~ ,))) ];.0)~
>>
>>       ; _2 pf&pfr\ 'allisdiscoveredfleex'
>> pmqfabhwewzddrrgtrbz
>>
>>       ; _2 (];.0 pf)&pfr\ 'pmqfabhwewzddrrgtrbz'
>> allisdiscoveredfleex
>>
>> I can now add monad u;.0 to my Life List.
>>
>> (you would also need a preconditioner to handle double letters and
>> odd-length input).
>>
>> I was hoping the same program would work for the hard part, but I
>> wasn't sure what to do when the two letters match in more than one axis.
>>
>> Henry Rich
>>
>>
>> On 2/19/2011 7:40 AM, Kip Murray wrote:
>>> The Playfair cipher http://en.wikipedia.org/wiki/Playfair_cipher is
>>> based on a 5 by 5 character array that treats i and j as equivalent:
>>>
>>> paswo
>>> rdbce
>>> fghik
>>> lmnqt
>>> uvxyz
>>>
>>> To encode "All is discovered flee" you replace pairs of letters al li
>>> sd is co ve re df le ex with pairs diagonally opposite, one to the
>>> right, or one below (wrapping) from the table, getting
>>>
>>> pm qf ab hw ew zd dr rg tr bz
>>>
>>> Your mission, should you decide to accept it, is to write j code for
>>> enciphering and deciphering, given the five by five table.  That's
>>> the easy part.  The hard part is to design and implement an analogous
>>> cipher that uses the 3 by 3 by 3 character array below to replace
>>> triples of letters alx _is _di sco ver ed_ fle ex_
>>>
>>> pas
>>> wor
>>> dbc
>>>
>>> efg
>>> hij
>>> klm
>>>
>>> nqt
>>> uvx
>>> yz_
>>>
>>> ---------------------------------------------------------------------
>>> - For information about J forums see
>>> http://www.jsoftware.com/forums.htm
>>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to