wu :: forums
« wu :: forums - Excel Simulator »

Welcome, Guest. Please Login or Register.
May 7th, 2024, 12:20pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Eigenray, SMQ, ThudnBlunder, william wu, Grimbal, towr, Icarus)
   Excel Simulator
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Excel Simulator  (Read 2019 times)
Misha Kruk
Guest

Email

Excel Simulator  
« on: Oct 17th, 2002, 1:05pm »
Quote Quote Modify Modify Remove Remove

This one is easy, but a little bit tricky (in the "I guarantee that your first solution will fail" way):
 
write a function which converts a number into Excel spreadsheet column name.  
0 = A
...
25 = Z
26 = AA
...
701 = ZZ
702 = AAA
etc
 
P.S. Yes, I know that Excel has a limit of 256 columns; they probably couldn't solve this one Wink
IP Logged
towr
Guest

Email

Re: Excel Simulator  
« Reply #1 on: Oct 18th, 2002, 3:14am »
Quote Quote Modify Modify Remove Remove

string const & f(int i)
{
  i = i+1;
  std::string s;
 
  while (i)
  {
     s = string(chr(64+i%26)).append(s);
     i /=26;
  }
  return s;
}
 
IP Logged
towr
Guest

Email

Re: Excel Simulator  
« Reply #2 on: Oct 18th, 2002, 3:25am »
Quote Quote Modify Modify Remove Remove

ok.. so now you made good on your guarantee.. it fails Wink
now I must think more..
IP Logged
towr
Guest

Email

Re: Excel Simulator  
« Reply #3 on: Oct 18th, 2002, 3:55am »
Quote Quote Modify Modify Remove Remove

std::string f(int i)  
{  
  std::string s;  
 
  i++;  
  while (i--)  
  {  
     s = std::string(1,static_cast<char>(65+i%26)).append(s);  
     i /=26;  
  }  
  return s;  
}  
 
seems to work.. lacks in elegance..  
I seem to have forgotten most c++  I learned last year..
IP Logged
S. Owen
Full Member
***





   


Gender: male
Posts: 221
Re: Excel Simulator  
« Reply #4 on: Oct 18th, 2002, 8:18am »
Quote Quote Modify Modify

Here's my Java solution:
 
public String toExcelCol(int i) {
  StringBuffer result = new StringBuffer();
  while(i >= 0) {
    result.append((char)('A'+(i%26)));
    i = i/26 - 1;
  }
  return result.reverse().toString();
}
 
I also missed a couple times before getting it... I thought it would be like writing the number in base 26, and it sort of is, but not quite. Interesting.
 
Edited to add: hmm... this is exactly what towr came up with, isn't it, just looks a little different. I didn't realize it.
« Last Edit: Oct 18th, 2002, 8:20am by S. Owen » IP Logged
Garzahd
Junior Member
**





    mlahut


Gender: male
Posts: 130
Re: Excel Simulator  
« Reply #5 on: Oct 30th, 2002, 4:03pm »
Quote Quote Modify Modify

Recursion is the first thing that comes to my mind. (using c++ strings)
 
string foo (int x) {
  return (x<0) ? "" : foo((x/26)-1)+(char)('A'+(x%26));
}
 
And yes, of course it took me more than one try.
IP Logged
FluffysWhole
Guest

Email

Re: Excel Simulator  
« Reply #6 on: Feb 25th, 2003, 6:16am »
Quote Quote Modify Modify Remove Remove

string label;
bool is_neg=( i<0 );
if( is_neg ) i=-i;
do{
 s.push_front( 65+(i%26) );
 i/=26;
}while( i>26 );
if( i ) s.push_front( 65+((i-1)%26) );
if( is_neg ) s.push_front( '-' );
return label;
 
The reason why this fools people is that the resulting label is not a standard base 26 value.
The leftmost column of the resulting label value, assuming more then one column, is actually base 27. A=1, Z=26, just that the zero state is never displayed.
If only one column is required then the single column is base 26.
 
<attached email address is rubbish to avoid spam>
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board