Essays/Anagram

From J Wiki
Jump to navigation Jump to search

Dyadic A. is Anagram. From Dictionary:

The expression k A. b permutes items of b by the permutation of order #b whose anagram index is k.

divmod=: <.@% , |~

anag=: (4 : 0)^:(1: < #@])
  'q n'=. x. divmod !<:#y.
  (q{y.) , n anag (q{.y.) , (>:q)}.y.
)

Examples

   4 anag '123'
312
   999999 anag '0123456789'
2783915460
   999999 anag i.10
2 7 8 3 9 1 5 4 6 0
   4 anag i.3 2
4 5
0 1
2 3

Some tacit variations

if2=: ^:(1: < #@])

an1=: ((divmod !@<:@#) (({.@[ { ]) , {:@[ $: ({.@[ {. ]) , >:@{.@[ }. ]) ]) if2

an2=: ((divmod !@<:@#) (({~ {.)~ , {:@[ $: ({.~ {.)~ , (}.~ >:@{.)~) ]) if2

an3=: ((divmod !@<:@#) (({~ {.)~ , {:@[ $: ({~ <@<@<@{.)~) ]) if2

an4=: ((divmod !@<:@#) (({~ {.)~ , {:@[ $: ({~ <^:3:@{.)~) ]) if2


   2 -:/\ 999999 (A. ; anag ; an1 ; an2 ; an3 ; an4) i.10
1 1 1 1 1

See Also


Contributed by Oleg Kobchenko