Jump to content
Science Forums

Programming Challenges


Recommended Posts

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 ;)

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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<0

This 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 A

As 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 example

example: 1->B, 26->AA, 1000->ALL

If $$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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ? :oopsie:

Link to comment
Share on other sites

  • 6 months later...
  • 4 weeks later...

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

Link to comment
Share on other sites

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

right, that's what my code does. 26 = AA, 27 = AB, 28 = AC etc.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...