Reduced to 17 by using echo -n to avoid a chop call.
Reduced to 15 by using say instead of print.
Reduced to 11 by using -p instead of say.
Explanation:
A is evaluated in string context and A..$_ builds a list starting at "A" and string-incrementing up to the input string. Perl interprets the ++ operator (and thus ..) on strings in an alphabetic context, so for example $_="AZ";$_++;print outputs BA.
=()= (aka "goatse" operator) forces an expression to be evaluated in list context, and returns the number of elements returned by that expression i.e., $scalar = () = <expr> corresponds to @list = <expr>; $scalar = @list.
public class A{public static void main(String[] z){int o=0,c=0;for(int i=z[0].length()-1;i>=0;i--,c++)o+=(z[0].charAt(i)-64)*Math.pow(26,c);System.out.println(o);}}
Java, 177 characters
public class A
{
public static void main(String[] z)
{
int m,o=0,c=0;
for(int i=z[0].length()-1;i>=0;i--,c++)
{
m=(int)Math.pow(26,c);
o+=(z[0].charAt(i)-64)*m;
}
System.out.println(o);
}
}
Assumes an uppercase input (via command line argument). The obvious approach with no tricks.
You can also replace raw_input() with input() to reduce the character count by 4, but that then requires the input to contain quotation marks around it.
And here's a subroutine that clocks in at 47 characters:
FYI. The base 26 arithmetic is called hexavigesimal and Excel's maximum column is XFD which converts to 16383 (using 0 as the first cell) which is coincidentally exactly 2^14 cells.
using System;class P{static void Main(string[]a){var r=0d;int j=0,i=a[0].
Length;while(i-->0)r+=(a[0][i]-64)*Math.Pow(26,j++);Console.WriteLine(r);}}
Ungolfed:
using System;
class P
{
static void Main(string[] a)
{
var r = 0d;
int j = 0, i = a[0].Length;
while (i-- > 0)
r += (a[0][i] - 64) * Math.Pow(26, j++);
Console.WriteLine(r);
}
}
,[ // get character input into p[0], enter loop if it isn't null (0)
>>>[->>+++++[-<+++++>]<+<] // take what's in p[3] and multiply by 26, storing it in p[4]
>[-<+>] // copy p[4] back to p[3]
<<++++++++[<++++++++>-]< // store 64 in p[1]
[<->-]< // subtract p[1], which is 64, from the input char to get it's alphabetical index
[>>>+<<<-] // add p[0] to p[3]
,] // get another character and repeat
>>> // move to p[3], where our final result is stored
So you'll notice I didn't actually convert the numerical value to an ascii string for printing. That would likely ruin the fun. But I did the favor of moving the pointer to the cell with the result, so at least it's useful to the machine.
The only reason J beat me is because of the parentheses. I'm thinking there should be some way to rearrange it to avoid the need for them, but it's been a long day. Ideas?
(Heh, you perl programmers with your 30+ character solutions are so cute!)
My Javascript solution is just 82 characters long and uses Integer.parseInt with Radix 36. It'd be fine if somebody could appen this to the Javascript section of this thread! :-)
Applescript: 188
Here's the requisite applescript in 188 characters, which is a very difficult language to make non-verbose. It also happens to be the longest answer of any language so far. If anyone knows how to shorten it, do share.
on run s
set {o, c} to {0, 0}
repeat with i in reverse of (s's item 1)'s characters
set m to 26 ^ c as integer
set c to c + 1
set o to o + ((ASCII number of i) - 64) * m
end repeat
end run
Private Sub CB1_Click()
Dim C, S
Range("A1").Select
Do
S = Len(ActiveCell)
x = 0
C = 0
Do
C = (Asc(Mid(ActiveCell, (S - x), 1)) - 64) * (26 ^ x) + C
x = x + 1
Loop Until x = S
ActiveCell.Offset(0, 1) = C
ActiveCell.Offset(1, 0).Activate
Loop Until ActiveCell = ""
End Sub
Uses Column A for input, outputs to Column B, runs off a VB command button click. =D
char[] a=args[0];t=0;for(i in a)t=26*t+i-64;print t
Invoke as
groovy *scriptname* ROFL
or
groovy -e "char[] a=args[0];t=0;for(i in a)t=26*t+i-64;print t" ROFL
This essentially the same as Java. I imagine some possibilities with using ranges and closures, but nothing came to mind for this example. Anyone else see a way to shorten this?
A more groovy-looking version with a closure is a bit longer, unfortunately.
t=0;args[0].toCharArray().each{t=t*26+it-64};print t
OOBasic: 178 characters, not counting indentational whitespace
revised
This version passes all the test cases. I suspect that it would be more successfully golf if it didn't "take advantage" of the fact that there's a spreadsheet using this numbering system. See the notes on the original version below for info on why that's not particularly useful. I didn't try very hard to cut down the score.
Also note that this will only work when run as a macro from an OO calc spreadsheet, for obvious reasons.
Function C(st as String) as Long
C = 0
while len(st)
C = C*26 + ThisComponent.Sheets(0).getCellRangeByName(left(st,1) &"1").CellAddress.Column+1
st = mid(st,2)
wend
End Function
original
OOBasic (OpenOffice Basic), too many characters (124):
Function C(co As String) As Long
C = ThisComponent.Sheets(0).getCellRangeByName(co &"1").CellAddress.Column+1
End Function
Limitations:
maximum value of co is AMJ (1024 columns). Anything larger results in an error with a completely uninformative error message.
This limitation is also present for the COLUMN() cell function. Presumably this is the maximum number of columns in an OOCalc spreadsheet; I didn't bother scrolling over that far or googling to find out.
Notes:
strangely it's not possible to give the variable 'co' a 1-letter name. Not sure what the logic is behind this, but after having spent enough time using OOBasic you stop looking for logic and begin to blindly accept the way things are (perhaps from gazing too long at the Sun).
Anyway entering =C("A"), =C("ABC"), etc. in a cell works for the first four test cases; the last two give errors.
As mentioned above K evaluates from right to left, so the _ic function takes whatever is to its right and converts it to an integer value, this includes both single characters and character vectors
-64 is added to each item in the integer vector that to get a set of base values
_sv takes two arguments: the one on its left is the numeric base, 26, and the one on its right is the integer vector of offset values