Zsbán, I learned a lot along the way. First  "Not is Nice":

   D=:'1234:ABC'
   f=: 13 :'y-.'':'''
   f D
1234ABC
   f
':' -.~ ]
   
Second "Easy alphanumeric characters"

   ]L=:(2|'/9@Z`z'I.a.)#a.
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

However, all the side trips along the way were fun.

Linda   


On 3/8/13, Linda Alvord <[email protected]> wrote:
>  ]A=:1 2 3 { 8 32 $ a.      NB. Example from definition of  a.
>    ]AL=:_5}."1 1}."1 A
>    ]L=:15}.,AL
>    f=: 13 :'((i.#y)~:y i. '':'')#y' NB. Remove the  :  from list
>    f
> ] #~ ([: i. #) ~: ':' i.~ ]
>    f L
> 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Why don't you use this instead?

   f=: -.&':'

In any case, this is a nice method, and with some liberal changes, can be
shortened to this.

   42}.,4{._32(26&{.)\a.-.':'

(Incidentally, I'm collecting these expressions on
"http://www.jsoftware.com/jwiki/BJonas";.


> Here's where I got lost in your solution:
>
>    a.#~2|a.i.&0@/:@,"{'/9@Z`z'
>
> I can't seem to unlock what happens at the heart of it.
> What does this do?
>
>  ([: i.&0 /:)

I admit that solution is a bit overcomplicated.  It's not what I'd write in
a real program.

Let me explain how it works.  This post will be a bit long, sorry, but this
is the easiest way I can explain.  I'll show you a much easier solution at
the end though.


I'll show on a shorter range of characters for readability.  Suppose we
start from this range of characters.

   ]alp =: (26{.65}.a.)
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Take this key of six characters which we'll use as endpoints of three
intervals to cut the above.

   ]key=: 'DHMQTV'
DHMQTV

Suppose we concatenate each letter with the key and sort the resulting seven
character long string.

   alp /:~@,"0 1 key
ADHMQTV
BDHMQTV
CDHMQTV
DDHMQTV
DEHMQTV
DFHMQTV
DGHMQTV
DHHMQTV
DHIMQTV
DHJMQTV
DHKMQTV
DHLMQTV
DHMMQTV
DHMNQTV
DHMOQTV
DHMPQTV
DHMQQTV
DHMQRTV
DHMQSTV
DHMQTTV
DHMQTUV
DHMQTVV
DHMQTVW
DHMQTVX
DHMQTVY
DHMQTVZ

As you can see, the single letter we concatenate goes to different positions
in the seven letter string, and jumps to a new position whenever we reach a
letter in the key.  For example, ABCD are in the first position in the first
four lines, then EFGH are in the second position, then IJKLM in the third.
The boundaries are exactly DHM because those are the first three letters of
the key.

Now instead of sorting the seven letter strings, we grade them to find out
where the letter we insert goes.

   alp /:@,"0 1 key
0 1 2 3 4 5 6
0 1 2 3 4 5 6
0 1 2 3 4 5 6
0 1 2 3 4 5 6
1 0 2 3 4 5 6
1 0 2 3 4 5 6
1 0 2 3 4 5 6
1 0 2 3 4 5 6
1 2 0 3 4 5 6
1 2 0 3 4 5 6
1 2 0 3 4 5 6
1 2 0 3 4 5 6
1 2 0 3 4 5 6
1 2 3 0 4 5 6
1 2 3 0 4 5 6
1 2 3 0 4 5 6
1 2 3 0 4 5 6
1 2 3 4 0 5 6
1 2 3 4 0 5 6
1 2 3 4 0 5 6
1 2 3 4 5 0 6
1 2 3 4 5 0 6
1 2 3 4 5 6 0
1 2 3 4 5 6 0
1 2 3 4 5 6 0
1 2 3 4 5 6 0

As you can see, the position of the zero in this string shows where the
extra letter got inserted.  So let's find the position of the zero, putting
the corresponding inserted letter above for clarity.

   |:alp;"0 i.&0"1 alp /:@,"0 1 key
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|0|0|0|0|1|1|1|1|2|2|2|2|2|3|3|3|3|4|4|4|5|5|6|6|6|6|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

So now the characters up to D get converted to a zero, the characters up to
H  get converted to a one, etc.  The key string controls where exactly the
numeric result changes.  So if we take the letters that get converted to an
odd number, we get exactly the letters corresponding to the three intervals
determined by the key string:
EFGH for the first interval (between D and H), then NOP for the second
interval (from M to P), etc.

   alp#~ 2| i.&0"1 alp /:@,"0 1 key
EFGHNOPQUV

In the actual solution to get the alphanumeric characters, I use '/9@Z`z' as
the key because '/9' gives the interval for numeric characters, '@Z' the one
for uppercase letters, etc; and I use a. as the starting list of letters.


Now it turns out the whole sort thing is a bit superfluous, because the
search primitive gives a much simpler and shorter solution.

   alp#~ 2| key I. alp
EFGHNOPQUV
   a.#~2|'/9@Z`z'I.a.
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

I wonder why I didn't find that earlier.


--
Ambrus
----------------------------------------------------------------------
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