I made a constructive method that generates the first ~26^n labels in the
sequence:

   cp=. ,/@:(,"1/)&.>
   step=. ({...@[ ,"0 1&.> <@;@]) , cp&{:
   digits=.' '; ,.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
   a2=.digits&step^:2 digits
   ((] , (24+]) , (700+])) i.4) { }.;a2
  A
  B
  C
  D
  Y
  Z
 AA
 AB
 ZY
 ZZ
AAA
AAB

Each result of (digits&step^:n digits) contains (numbers with
spaces);(numbers without spaces) . There are 26^n elements in the second
box, and some complicated but smaller (for reasonably small n) number in the
first.

}.; digits&step^:n digits

gives you the first some number of labels.

n=4 gives 12356631 labels in about a third of a second on my computer. After
that memory becomes scarce.

Marshall

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Tracy Harms
Sent: Thursday, December 16, 2010 3:31 PM
To: Programming forum
Subject: [Jprogramming] increasing-width base conversion (e.g. Excel column
labels)

I encountered the question of how to produce the alphabetic label for a
given column in Excel. I enjoyed analyzing the problem, and I enjoyed coding
a solution in J.

I notice that I have difficulty "ironing out" the program I've written.
There is a need to use several calculated results more than once and I'm not
particularly happy with how these are structured in the program I've
written, especially the last half.

What I'd particularly like is to have the "central" calculation shine
through more visibly. I'm thinking about
  (howmany # Base) #: Value

Would anybody like to restructure this, or provide alternative solutions to
this problem?

--
Tracy



convert=: 3 : 0
  Base  =. # Digits=. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  offset=. [:+/ [^ [:>: i.@:]
  om    =. ( * 0<]) @ <. @ ^. NB. order of magnitude, in chosen base (x)
  OM    =. Base om y
  Value =. ( -  Base offset"0 OM"_ ) y
 ((OM + _1<Value) # &.> Base ) (Digits{~ #: )&> Value
)

NB. 'example execution
   convert (] , (24+]) , (700+])) i.4
A
B
C
D
Y
Z
AA
AB
ZY
ZZ
AAA
AAB
----------------------------------------------------------------------
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