Netlumination Portfolio
Netlumination Blog

Archive for March, 2010


A recursive PHP function to turn digits into words

Wednesday, March 31st, 2010

Warning: This is a big spoiler for Project Euler Problem 17, so if you’re still working on it, don’t keep reading!

I quickly mentioned recursion in my previous post. Turning digits into letters is clearly another example for which recursion is useful. Think about a number like 524. If you know that 4 = “four”, 5 = “five”,  and that 20 = “twenty” then all you have to do, to turn 524 into words,  is run your script on 5, add “hundred,” run your script on 20, and run your script on 4, and if you write your script well, then you can do this using recursion.

Take a look at a working example of the function I wrote to do just this with PHP.

Take a look at my solution to Project Euler Problem 17 [SPOILER ALERT]



Quick Note on Recursion

Monday, March 29th, 2010

Recursion

Example in PHP using the factorial:

  1. // $n must be a positive integer or 0 - function returns 1 for 0 and all errors
  2. function factorial($n)
  3. {
  4.   if ($n > 0 && is_int($n))
  5.   {
  6.     // factorial of positive integer
  7.     return $n * factorial($n - 1);
  8.   } else
  9.   {
  10.     // factorial of 0
  11.     return 1;
  12.   }
  13. }

There are many other blog posts that cover this topic well. Some include:

Recursion in PHP (and iteration)

Recursive patterns in Regex from the PHP manual

Here is a little snippet of a C++ function that will ask for user input and make sure it is a float. If it’s not a float, the functions recurses (is that a word?)… calls itself. The end effect is that the user is asked to enter a float until they do. You must include both iostream and limits.

  1. float collectFloat()
  2. {
  3.   float user_input(0.0);
  4.   try
  5.   {
  6.     // Could also use cin.fail() to check for success
  7.     if (cin >> user_input) cinIgnoreRest();
  8.     else
  9.       throw (string(“\nNot a float entered.\n Please enter a number.\n”));
  10.   }
  11.   catch (string error)
  12.   {
  13.     cout << error << “==> “;
  14.     cin.clear();
  15.     cinIgnoreRest();
  16.     // This will keep repeating until user enters a float
  17.     user_input = collectFloat();
  18.   }
  19.     return user_input;
  20. }


I Hope Your Inheritance Isn’t An EM

Sunday, March 28th, 2010

The web allows users to customize their experience. This can be satisfying for the user and scary for the designer. Designers often have very specific ideas about how their web pages should look. Phrases like, “pixel perfect” come to mind. Well, “pixel perfect” font size is limiting to users who want to be able to control the size of their text.

Read the rest of this entry »



Not much help, but there is truth in advertising

Sunday, March 28th, 2010

I got an email saying that lost laptops are recovered 3% of the time. The email promised to increase the chances of recovery by 80 whole percents! Well, at least there is still truth in advertising?

So that would be 0.03 + (0.03 * 0.80) = 5.4% recovery. Read the rest of this entry »



blog