This is a fairly neat anagram checker:

   anag =: -:&(/:~@:(0 27-.~27<.32|64>.123<.a.&i.))

   'Dr. K. E. Iverson' anag 'Knives Or Red'

1


   'city sojourners up wry keg' anag 'New York City J Users Group'

1


It checks the sorts of the indices of the arguments' elements
in the atomic vector, a. , without needing to put them back
into character form.  It needs a little care to get rid of
these characters, '<=>?@[]^_`' ,  which is why the 27<. is needed.
123<. ensures '{|}...' are ignored, once 0 27 -.~ is performed, so
that:

#(0 27 -.~(27<.32|64>.123<.a.&i.))'<=>?@[]^_`',123 124 125{a.

0


So also:
'<=>~@Dr. K. E. Iverson' anag 'Knives Or Red[\]{|}'

1


In the old days,  one couldn't take the location of alphabetical
characters for granted,  but the atomic vector has been stable
for many years,  so this should be fairly safe for the next few
releases of J!


Any use?


Mike


On 08/06/2015 14:46, Devon McCormick wrote

Using stdlib noun "Alpha_j_" and verb "tolower":
   str=. 'Dr. K. E. Iverson'
   Alpha_j_
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
   str-.alpha_j_
'Dr. K. E. Iverson' -. alpha_j_
   str-.Alpha_j_
. . .
   str-.str-.Alpha_j_
DrKEIverson

   tolower str-.str-.Alpha_j_
drkeiverson

So,
      standardizeStr=: Alpha_j_&([: tolower ] -. -.~)
and
   isAnagramOf=: ([: /:~ [: standardizeStr ]) -: [: /:~ [: standardizeStr [
   'Dr. K. E. Iverson' isAnagramOf 'Knives Or Red'
1
   'city sojourners up wry keg' isAnagramOf 'New York City J Users Group'
1


On Mon, Jun 8, 2015 at 9:33 AM, Roger Hui <[email protected]> wrote:

Strings x and y are anagrams if x -:&(/:~)&(-.&'. ')&tolower y .  That is,
compare them after reducing them to a standard form, by (converting to
lower case, then deleting spaces and dots (and whatever), then sorting
them).






On Mon, Jun 8, 2015 at 6:27 AM, Gian Medri <[email protected]> wrote:

> Is there a shorter or better method of finding
> if 2 strings are an anagram in one line code?
>
> Write an idiom that takes two character vectors as its left and right
> arguments and returns 1 if they are anagrams of each other. An anagram
> of a string uses all of the letters
>
> of the string ignoring word spacing, capitalisation, and dot.
>
>
>
>  anagram=: 13 : '(/:~(B,a.) {~ (A,a.) i.~~  (-.&'' .'')y)-:
> /:~(B=.((97+i.26){a.),a.) {~ (A=.((65+i.26){a.),a.) i.~~  (-.&''
> .'')x'
>
>
>
>
>
>    'Dr. K. E. Iverson' anagram 'Knives Or Red'
>
> 1
>
>
> Gian Medri
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm




--
Devon McCormick, CFA
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

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

Reply via email to