letter combination sequence numbers

37 views
Skip to first unread message

Larry

unread,
Dec 25, 2012, 10:15:37 AM12/25/12
to GoogleSemWare TSE, tse...@freelists.org
/*** A2nN2a.s

(Definitions:

The 'alphabetic sequence' addressed by this macro refers to the
sequential letter combinations from 'a' to 'fxshrxw'.

(The 'sequence number' used by this macro refers to the position of any
letter combination in the alphabetic sequence supported.

(For example, the letter combination 'a' occupies sequence number '1'.
The letter 'z' is at '26'. The letters 'aa' are at '27'. The letters
'aaa' are at '703'. And so on up to the letters 'fxshrxw' at
'2147483647'.)

This macro will return the sequence number for letter combinations of 7
or fewer letters from 'a' up to 'fxshrxw'. The limit here is the TSE
integer limit of 2147483648.

Likewise, this macro can also convert any number from 1 to 2147483647
into its representative letter combination in the alphabetic sequence from
'a' to 'fxshrxw'.

Some relationship examples (letter combination = sequence number):
===================================================================
a = 1
aa = 27
aaa = 703
aab = 704
sammy = 8709271
knud = 203350
carlos = 36425993
and so on to infinity.

Some Interesting Sequence Number to Letter Combinations:
==========================================================

20010911 = aqtmxk (9/11)
20121221 = aqzucc (Apollo 11)
19690704 = aqbhft (EOW)
734862 = aouax (Days Since Day 1 Year 1)

This macro was written as a reference for three other macro, t2nN2t
(text to numbers..numbers to text on whole files), and vwcm (the Virtual
Wheel Cipher Machine based on Thomas Jefferson's Wheel Cipher machine).

This macro (having no practical value other than that above) joins the
growing World Famous MUSALMs Collection. :)

Enjoy.

(NOTE: Although I have confidence in the macro's letter combination
sequence number calculations, I cannot guarantee them...just don't have
the patience to wade through the 2 billion letter conbinations this
macro can address. But, the saving semi-grace is that the sequence
numbers produced will always match the same letter combination.)

***/

// a2nn2a.s - convert letter sequence2 to number and vice versa
string rev[50]="A2nn2aRev1025"

<ctrlshift j><a> execmacro("A2nn2a")

/***
{This Revision:}
1025- 20121225- safe save
1024- 20121225- safe save
1023- 20121225- fixed caryover rerun for b
1022- 20121224- safe save
1021- 20121224- adding clipboard
1020- 20121223- safe save - everything looks good...flw
1019- 20121223- going to full 7 character limits...
- adding prompt helps
1018- 20121119- added alim and nlim variables
1017- 20121119- safe save
1016- 20121119- modification done, works...recombining a2n + n2a
1015- 20121118- divided into two macros, Jiga2nRev1001 and jign2a
- worked out support for full integer limit sequences
- will combine back into this macro
1014- 20121116- back to normal
1013- 20121116- adding loop to return to menu after results
- first try at adding 7th letter...failed
1012- 20121115- safe save -- finally! turns out that the macro was okay, but
my number was wrong...
1011- 20121115- the whole thing is wrong...
1010- 20121115- working on the length for case 2
- it aiiin't workin' :(
1009- 20121114- adding comments
- take another look at the calculations...may not be doing
them right
- work up a range table for each set of letters
1008- 20121113- safe save
1007- 20121113- finishing up...
1006- 20121113- adding menu and prompts
1005- 20121113- testing...
1004- 20121113- "seems" to be working...
1003- 20121113- troubles...
1002- 20121113- just like that, number to letter done
1001- 20121113- exactly!!
1001- new macro
-------------------------------------------------------
***/

// local globals
string clip[5]=""

// help
helpdef a2nn2ahelp
title="A2nn2a Overview Help"
x=4
y=4

"Macro Name: A2nn2a"
"Macro Version: A2nn2aRev1025"
"Assigned Key: CtrlShift j a"
""
"If you commonly use an outline, then you know that part of the level"
"structure involves lettering. Typically, you have I., A., 1., a., and"
"son on. If you have more than 26 alphabetically numbered levels, the"
"lettering jumps to AA., AB., etc."
""
"This is the letter sequenc that this macro works with. The letter"
"seqence range that this macro supports is from 'a' to 'fxshrxw' and this"
"corresponds to the sequence numbering from 1 to 246409243."
""
"The purpose of this macro is to be able to convert back and forth"
"between letters and the number of those letters in the sequence. As a"
"practical example, say you have an outline with an alphabetic level"
"whose lettering runs from 'a' to 'abc', and you want to know just how"
"many levels there are. Fire up this macro, choose the Letters To Numbers"
"option at the menu, enter 'abc', and, voila, there are 731 levels."
""
"In the same manner, say you want to know where in the letter sequence a"
"certain combination is, choose the Letters To Numbers, enter something"
"like 'fractal', and know that it is 2067882686 in the sequence."
""
"Going the other way, choose the Numbers To Letters option at the menu"
"and enter your favorite number to see which letter combination occupies"
"that sequence. You can even compose poetry this way, like, converting 'I"
"Love You' into sequence number results in '9 221629 17311' :)"
""
"Once the macro generates an answer, it opens an info box to show it and"
"also allows you to copy the answer to the current cursor line. It puts"
"the // TSE comment bars at the front of the answer."
""
"After a successful conversion operation, the answer is added to the"
"macro's clipboard file, a2nn2a.clp. In this version of the macro, this"
"is a normal file but can be hidden in the Main() procedure. This file"
"can be opened using the Go To Clipboard selection on the main menu. The"
"macro does not save this file. If you want to keep it, you must save it"
"manually."
""

end

// letters prompt help
helpdef lphelp
title="Letters Prompt Help"
x=4
y=4

""
"This macro can handle all letter combinations ranging from 'a' to"
"'fxshrxw'. The 7 character limitation to the 'fxshrxw' sequence is"
"purely a matter of integer calculations."
""
"At this prompt, enter any letter combination in that range and the macro"
"will return the sequence number of that letter combination."
""
"the sequence number is a letter group's location in the sequence from"
"'a' to 'fxshrxw' such that letter 'a' is '1', 'b' is '2', 'z' is 26,"
"'aa' is 27, and so on."

""

end

keydef lpHelpkey
<F1> QuickHelp(lpHelp)
end
proc lpstartup()
Enable(lpHelpKey)
UnHook(lpstartup)
end

// mubers prompt help
helpdef nphelp
title="Numbers Prompt Help"
x=4
y=4

""
"This prompt asks for the number you wish to convert to letter"
"combination equivalent in the sequence of letters from 'a' to 'fxshrxw'."
""
"The range of numbers you can enter go from 1 to 2147483647, which is one"
"shy of TSE's positive integer limit. Any number in that range will"
"produce the corresponding letter combination for that number in the"
"sequence."
""
"For instance, if you enter the number '26', the macro will return the"
"letter combination 'z'. If you enter the number 246409243, the macro"
"will return 'tsepro'. :)"
""
"Some interesting lettering can be produced by entering other numbers."
"Enter '20010911' and get 'aqtmxk', or '20121221' and get 'aqzucc'. Try"
"your family's birthdates."
""

end

keydef npHelpkey
<F1> QuickHelp(npHelp)
end
proc npstartup()
Enable(npHelpKey)
UnHook(npstartup)
end


// a2nn2a menu
menu a2nn2amenu()
title="Letters to Numbers, Numbers to Letters Menu"
history

"&Letters To Numbers"
"&Numbers To Letters"
"" , , divide
"&Go To The A2nN2a Clipboard"[clip : 5]
"&Help" , quickhelp(a2nn2ahelp) , dontclose
"" , , divide
"(ESC Quits The Macro)" , , _mf_skip_

end

proc main()

string clipfn[255]=loaddir()+"a2nn2a.clp"
integer clip_id=0

string a[7]="unique" // letters to number
string b[7]="" // accumulator for number
string n[10]="256076787" // number to letters
string cr[1]=chr(13) // carriage return
string q[1]=chr(34) // quote mark
string alim[7]="fxshrxw"//"zzzzzz"

integer x=0 // number accumulator
integer z=0 // report number
integer aa=97 // lower case a
integer zz=122 // lower case z
integer i=0 // loop
integer l=0 // length of letters
integer v=0 // value to calculate
integer ch=0 // special cse for z affecting next character
integer bad=0 // repeat bad user input flag
integer nlim=2147483647 //321272406

// load the clipboard
pushposition()
editthisfile(clipfn)
clip_id=getbufferid()
//buffertype(_hidden_)
clip="Empty"
if lfind("[A-Za-z0-9]","igx")
clip="Clips"
endif
popposition()

// start the menu loop
loop

// open the menu
case a2nn2amenu()

when 1 // convert letters to numbers
// prompt for letters
repeat
bad=0
Hook(_PROMPT_STARTUP_,lpstartup)
if ask("Enter 7 or fewer letters (range:
a-"+alim+"):,F1=Help,ESC=Quit",a) and length(a)
a=lower(a)

// see if letters are in range
if length(a)==length(alim) and a>alim
sound(30,90)
message("Letter combination is out of range. Try again.")
bad=1
endif
// check for non-alpha characters
for i=1 to length(a)
if not (a[i:1] in "a".."z")
sound(30,30)
message("Incorrect Characters Found. Try Again.")
bad=1
endif
endfor
else
sound(30,30)
warn("Cancelled.")
goto ending
endif
until bad==0

// convert up to 6 letter combinations into a number
a=format(a:7:" ")
l=length(a)
x=0

for i=7 downto 1
if a[i:1]<>" "
case i

when 1 // position 1 (.??????)
x=x+26*26*26*26*26*26*(asc(a[l-6:1])-aa+1)
when 2 // position 2 (?.?????)
x=x+26*26*26*26*26*(asc(a[l-5:1])-aa+1)
when 3 // position 3 (??.????)
x=x+26*26*26*26*(asc(a[l-4:1])-aa+1)
when 4 // position 4 (???.???)
x=x+26*26*26*(asc(a[l-3:1])-aa+1)
when 5 // position 5 (????.??)
x=x+26*26*(asc(a[l-2:1])-aa+1)
when 6 // position 6 (?????.?)
x=x+26*(asc(a[l-1:1])-aa+1)
when 7 // position 7 (??????.)
x=asc(a[l:1])-aa+1

endcase

endif
endfor

if x in 1..2147483647
sound(9000,90)
if msgboxex(rev+" Letters To Number Result",
" From The Letters: "+q+trim(a)+q+cr+
"Produce the Number: "+q+str(x)+q+cr+cr+
"(Answer is saved to the A2nN2a Clipboard.)"+cr+
"Copy this to current text?","[&No];[&Yes]")==2
addline("// Sequence Number for Letters '"+trim(a)+"' is
'"+str(x)+"'")
endif
clip="Clips"
addline("// Sequence Number for Letters '"+trim(a)+"' is
'"+str(x)+"'",clip_id)
else
sound(30,30)
warn("Letters Are Out Of Range (a...fxshrxw)")
goto ending
endif

when 2 // convert number to letters

bad=0
b=""
repeat
Hook(_PROMPT_STARTUP_,npstartup)
if asknumeric("Enter a number (range:
1-"+str(nlim)+"):,F1=Help,ESC=Quit",n) and length(n)
n=strreplace(",",n,"","")
x=val(n)
if not x in 1..nlim //2147483647//321272406
sound(30,30)
message("Number ouf of range. Try again.")
bad=1
endif
else
sound(30,30)
warn("Cancelled.")
goto ending
endif
until bad==0

// get the length of the resulting letters
if x in 1..26
z=7
elseif x in 27..702
z=6
elseif x in 703..18278
z=5
elseif x in 18279..475254
z=4
elseif x in 475255..12356630
z=3
elseif x in 12356631..321272406
z=2
elseif x in 321272407..2147483647 // 2147483647 gives fxshrxw
z=1
else
sound(30,90)
warn(str(z)+" "+str(x))
warn("Number is out of range. Nothing done.")
goto next
endif

// convert the number into letters
for i=7 downto z

case i

when 7 // position 7 (??????.)
v=(x mod 26)+aa-1
when 6 // position 6 (?????.?)
v=x/26 mod 26+aa-1-ch
when 5 // position 5 (????.??)
v=x/26/26 mod 26+aa-1-ch
when 4 // position 4 (???.???)
v=x/26/26/26 mod 26+aa-1-ch
when 3 // position 3 (??.????)
v=x/26/26/26/26 mod 26+aa-1-ch
when 2 // position 2 (?.?????)
v=x/26/26/26/26/26 mod 26+aa-1-ch
when 1 // position 1 (.??????)
v=x/26/26/26/26/26/26 mod 26+aa-1-ch

otherwise
sound(30,30)
warn("Out Of Range.")
goto ending

endcase

// special case for z
ch=0
if v==aa-1
v=zz
ch=1
endif

// build the letter string
if v in aa..zz
b=chr(v)+b
endif

endfor
sound(9000,90)
if msgboxex(rev+" Number To Letter Result",
" From The Number: "+q+n+q+cr+
"Produce the Letters: "+q+b+q+cr+cr+
"(Answer is saved to the A2nN2a Clipboard.)"+cr+
"Copy this to current text?","[&No];[&yes]")==2
addline("// '"+n+"' Is The Sequence Number For The Letters
'"+trim(b)+"'")
endif
clip="Clips"
addline("// '"+n+"' Is The Sequence Number For The Letters
'"+trim(b)+"'",clip_id)
z=0
v=0

when 4 // view clipboard
placemark('n')
gotobufferid(clip_id)
clip="Empty"
if lfind("[A-Za-z0-9]","igx")
clip="Clips"
sound(7000,10)
message("To return to your file, use BookMark 'n'")
goto ending
else
sound(30,90)
updatedisplay()
warn("Nothing in Clipboard. Press OK to return.")
gotomark('n')
updatedisplay()
endif

otherwise
sound(30,30)
message("Cancelled.")
goto ending

endcase
next:


endloop

sound(6000,60)
message(rev+" Finished.")

goto ending
ending:

end
A2nn2a.s
Reply all
Reply to author
Forward
0 new messages