alexander Posted January 28, 2010 Report Posted January 28, 2010 Basically here i want to throw problems that shouldn't take too much of your time, but they are fun to solve programatically. The code can be in whatever language you prefer, or detailed pseudo code, and i think that some of you might enjoy this. Also seeing code of others is great and all, but try to do your own thing :phones: As such first problem: You have an array of arbitrary length, that you are trying to shove into a datastructure (excel work sheet for example) that addresses as A - XFD for columns and 1 - [any number] rows. Write a function to convert a numeric value to the column address format. example: 1->B, 26->AA, 1000->ALM enjoy ;) Quote
alexander Posted January 28, 2010 Author Report Posted January 28, 2010 really, still no takers? Ok, so i guess i'll post my friend's solution to this, note the "my friend's part", maybe this will inspire someone to make a better version... I'll hold off on my personal solution until all hope of anyone interested in this is lost... function dec2excel($dec) { while($dec > 25) { $rem=$dec%26; $dec=floor($dec/26-1); $value=chr(65 + $rem).$value; } $value=chr(65+$dec).$value; return $value; } Quote
CraigD Posted January 29, 2010 Report Posted January 29, 2010 This is your friend's algorithm as MUMPS xecute code that expects and returns the symbol A:n B s B=A,A="" f s A=$c(B#26+65)_A,B=B26-1 q:B<0This is it as a MUMPS extrinsic function, more similar to most programming languages functions:D2EXCEL(;) n A s A="" f q:B<0 s A=$c(B#26+65)_A,B=B26-1 Q AAs with most MUMPS, it’s a bit terser than most other languages, but for this program, looks about the same as in most languages. There’s something suspicious about this exampleexample: 1->B, 26->AA, 1000->ALLIf $$D2EXCEL(1)="B", then $$D2EXCEL(1000)="ALM"If the in-the-loop code is changed slightly, to this:f q:B<1 s B=B-1,A=$c(B#26+65)_A,B=B26$$D2EXCEL(1)="A" and $$D2EXCEL(1000)="ALL" I can’t, after a bit of thinking, think of a much better way to code this conversion than this, and am curious to see your “personal solution”, Alexander. Quote
alexander Posted January 29, 2010 Author Report Posted January 29, 2010 I ended up implementing a recursive function (i loves proper recursion) function numToCol($num) { if(($num)>25) $col=numToCol(($num/26==1) ? 0 : ((int)($num/26)-1)); return $col.chr(65+$num%26); } if 0 is A then 1000 is ALM, sorry i was running 1000 iterations from 0 to 999, and forgot that i had a < not <=, which makes the last one 999... i'll change that so people dont get confused this thing is so ridiculously fast that 1,000,001 iterations of it (from 0-1000000) takes under 6s to run (at 2.4 GHz (using one core)), and 1000000 => BDWGO, which took 4 recursions to complete Quote
alexander Posted January 29, 2010 Author Report Posted January 29, 2010 side note, so far my friends code actually runs faster, i think php, or probably interpreted languages, dont deal well with recursion... bestrangeful.... Quote
CraigD Posted January 30, 2010 Report Posted January 30, 2010 My non-recursive MUMPS example (MUMPS being notoriously slow for anything other than persistent data accessing and updating, and not required by language standard to support more than a dozen or so stack levels, so a perilously place of recursion) took about 9 s for 1000000 calls on a 8-CPU powerpc with about 100 competing users. On a single 3.4 Ghz commodity PC with no competition, it took less than 3. Why you'd much care about this code's speed (or fail to quail at the mere thought of a million-column spreadsheet) I can't imagine - but speed's everybody's friend :shrug: So where’s everybody’s inverse of the conversion, that will translate BDWGO back into 1000000 ? Quote
phillip1882 Posted August 22, 2010 Report Posted August 22, 2010 here's my python version that does both. x = input("input a word or number. ") if x[0] >= "0" and x[0] <= "9": x = int(x) y = "" while x > 0: letter = x%26 x = int(x/26) y = chr(letter+65) +y print(y) else: y = 0 start = 0 while len(x) > 0: y += ord(x[0])-65 if len(x) >1: y *= 26 x = x[1:] print(y) Quote
alexander Posted September 14, 2010 Author Report Posted September 14, 2010 Philip you misread the original code and what it does, namely it doesn't convert ascii value to to a character and vice-versa, it takes a number and converts it to a corresponding column lettering, such as that found in a spreadsheet, columns 0-25 being A-Z, 26 being AA, 1000 being ALM and column 1000000 being BDWGO Quote
phillip1882 Posted September 16, 2010 Report Posted September 16, 2010 Philip you misread the original code and what it does, namely it doesn't convert ascii value to to a character and vice-versa, it takes a number and converts it to a corresponding column lettering, such as that found in a spreadsheet, columns 0-25 being A-Z, 26 being AA, 1000 being ALM and column 1000000 being BDWGOright, that's what my code does. 26 = AA, 27 = AB, 28 = AC etc. Quote
Qfwfq Posted September 16, 2010 Report Posted September 16, 2010 Looks like Alex confused 26 with 170. Quote
alexander Posted September 16, 2010 Author Report Posted September 16, 2010 My bad, i quickly glimpsed over the code last time, i did look back at it and, well, it doesn't work, both syntactically and your math is off, both things that you should check before posting, or if it really isn't finished code, title it as pseudo-code. I know it sounds like i'm being a grump, but if you post code that doesn't work saying hey here's my code, you waste the time of others who want to may want to use it making it work, least you can do it make sure it runs, it doesn't have to run correctly (unless you are posting it as a solution). Actually working and a bit more efficient version of your code: x = raw_input("input a word or number. ") if x[0].isdigit(): x=int(x) y = "" while x >= 0: y = chr(x%26+65)+y x = int(x/26)-1 print(y) else: y = 0 while len(x) > 0: y = y*26+(ord(x[0])-64) x = x[1:] print(y-1) Quote
phillip1882 Posted September 21, 2010 Report Posted September 21, 2010 you're right about the math, as to it running, i was using python 3.1, not 2.7. i do wish to apologize about the math, not sure why i didnt test more thoughly. Quote
alexander Posted September 21, 2010 Author Report Posted September 21, 2010 Gotcha with the python version, yeah i should have tried running it in 3... i guess since 2 is still very main stream, it didnt occur to run the code through python 3 interpreter, gah, why did they have to break python again like that... Also no problemos about the math hickup, lol it gets weird sometimes... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.