Yow-Hann Lee - Software Happens

All things Computer Science, .NET & WWW

  Home  |   Contact  |   Syndication    |   Login
  132 Posts | 7 Stories | 38 Comments | 50 Trackbacks

News


Article Categories

Archives

Post Categories

About

So by now, everyone is tired of reading about FizzBuzz. It's even been on a podcast episode. So without further ado, here is a question to replace the now infamous technical filter.

QUESTION:

Write a function that takes in a base 15 value as a string and returns its integer value. An example would be "1000" = 3375.

posted on Wednesday, March 21, 2007 11:24 PM

Feedback

# re: Quick BrainSharpener (#9) - Retiring FizzBuzz with a Replacement 3/22/2007 8:31 AM thorkia
First off, "1000" would not be 3375. It would be 4096.

Second, here is avery basic converter. It took me 2 minutes to write:

static public int toInt(string hexvalue)
{
int returnvalue = 0;

for (int charcount = hexvalue.Length; charcount > 0; charcount--)
{
returnvalue += getCharValue(hexvalue[charcount - 1]) * (int)Math.Pow((double)16, (double)(hexvalue.Length - charcount));
}

return returnvalue;
}

static private int getCharValue(char chr)
{
int retval;
if (int.TryParse(chr.ToString(), out retval))
return retval;

switch (chr.ToString().ToUpper())
{
case "A":
return 10;
break;
case "B":
return 11;
break;
case "C":
return 12;
break;
case "D":
return 13;
break;
case "E":
return 14;
break;
case "F":
return 15;
break;
}
return 0;
}

It doesn't check if there are invalid characters or not. That could be added



# Base15 converter 3/23/2007 10:58 AM Lorin Thwaits
Good effort, Thorkia, but 15^3 = 3375. What you've built is a hex to decimal converter!

Here's a base 15 to 10 option written in C#, optimized a bit for performance:

string base15="1000";
int num = 0;
for (int i = 0; i < base15.Length; ++i)
{
num *= 15;
ch = base15[i];
if ((ch & 192) == 64) // Is it a letter?
num += (ch & 15) + 9; // Letters A-E
else
num += ch & 15; // Numbers 0-9
}

It will handle A through E as either upper or lower case, but if there are other illegal characters then it will give a goofy (and incorrect) result. Remember, the focus here was 100% for speed! On my measely 1.8 GHz laptop and running the .NET Framework 2.0 it does 100 million conversions in 5 seconds.

BTW, what in the world is FizzBuzz?

-Lorin


# re: Quick BrainSharpener (#9) - Retiring FizzBuzz with a Replacement 3/25/2007 12:24 AM Yow-Hann
The link to FizzBuzz is above in the blog entry.

Lorin pointed out the flaw in Thorkia's solution. Ironically, this was actually something that is a common mistake. Once I post the post-mortem, I will link it here.



# re: Quick BrainSharpener (#9) - Retiring FizzBuzz with a Replacement 3/25/2007 12:38 AM Yow-Hann
Thanks for participating in the little experiment.
The post-mortem entry can be found at:
http://geekswithblogs.net/yowhann/archive/2007/03/25/109746.aspx

# re: Quick BrainSharpener (#9) - Retiring FizzBuzz with a Replacement 3/30/2007 8:09 PM Keen
Interesting experiment.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 5 and 2 and type the answer here: