Saturday, October 25, 2008

Golden function

The golden function is the upper branch of the hyperbola.


ASP

  1. function gold(x)
  2.     gold = (x + sqr(x^2 + 4)) / 2
  3. end function

PHP

  1. function gold($x)
  2. {
  3.     return ($x + sqrt($x^2 + 4)) / 2;
  4. }

Sorry if this is not exciting stuff. I've got some better stuff coming, but I want to clear out some older stuff that has been waiting a while.


Saturday, October 18, 2008

Levenshtein distance

The Levenshtein distance between two strings is a measurement of similarity. The smaller the distance, the more similar two strings are. Our PHP programmer friends have a function to calculate this distance; we deserve one too.


ASP

  1. function levenshtein(byVal first, byVal second)
  2.     dim distance
  3.     dim truncateLength
  4.     if first = second then
  5.         ' The distance is zero if the strings are identical.
  6.         distance = 0

  7.     else
  8.         ' The distance is at least the difference of the lengths of the two strings.
  9.         distance = abs(len(first) - len(second))
  10.         ' Force the strings to be the same length to prevent overflows.
  11.         truncateLength = ((len(first) + len(second)) - distance) / 2
  12.         first = Left(first, truncateLength)
  13.         second = Left(second, truncateLength)
  14.         ' Compare the corresponding characters in each string.
  15.         for i = 1 to truncateLength
  16.             if Mid(first, i, 1) <> Mid(second, i, 1) then
  17.                 distance = distance + 1
  18.             end if
  19.         next
  20.     end if
  21.     levenshtein = distance
  22. end function

View ASP implementation on Snipplr

Saturday, October 11, 2008

WordCount

PHP has a function called str_word_count() which allows you to count the number of words in a string, and an array or associative array of those words. Unfortunately for us, ASP doesn't support optional parameters, so we can't duplicate all of this functionality in a single function, so we'll just concentrate on the main feature of counting the number of words.


ASP

  1. function WordCount(byVal someString)
  2.     dim position
  3.     dim spaces
  4.     spaces = 1
  5.     someString = trim(someString)
  6.     for position = 1 to len(someString)
  7.         if mid(someString, position, 1) = " " and not mid(someString, position - 1, 1) = " " then
  8.             spaces = spaces + 1
  9.         end if
  10.     next
  11.     WordCount = spaces
  12. end function

View ASP implementation on Snipplr

Saturday, October 4, 2008

Nth Root

Both ASP and PHP have a function that allows you to calculate the square root of a number. What if we wanted the cubic root of a number, or some deeper root? Calculating the root of a number is the same as raising that number to a fractional exponent.


ASP

  1. function root(x, y)
  2.     root = x ^ (1 / y)
  3. end function

PHP

  1. function root($x, $y)
  2. {
  3.     return pow($x, 1/$y);
  4. }

x
the number you want the root of
y
the depth you want to go (2 = square, 3 = cubic, etc.)

Saturday, September 27, 2008

Format a number in scientific notation

Surprisingly, neither ASP nor PHP seem to have a built-in way of formatting a number in scientific notation. Today we're going to change that.


ASP

  1. function formatScientific(someFloat)
  2.     dim power
  3.     power = (someFloat mod 10) - 1
  4.     formatScientific = cStr(cDbl(someFloat) / 10^power) & "e" & cStr(power)
  5. end function

PHP

  1. function formatScientific($someFloat)
  2. {
  3.     $power = ($someFloat % 10) - 1;
  4.     return ($someFloat / pow(10, $power)) . "e" . $power;
  5. }

Sunday, September 21, 2008

Logarithms

ASP's log() function returns the natural logarithm of a number. But what if we want a different base? The same problem exists with handheld calculators, and the same trick we use to get around it there can be used here too.


ASP

  1. function logx(number, base)
  2.     logx = log(number) / log(base)
  3. end function

View ASP implementation on Snipplr

Saturday, September 13, 2008

str_repeat

This week we're duplicating another string function from PHP: str_repeat().


ASP

  1. function str_repeat(input, multiplier)
  2.     dim output
  3.     output = ""
  4.     for i = 1 to multiplier
  5.         output = output & input
  6.     next
  7.     str_repeat = output
  8. end function

View ASP implementation on Snipplr