Posts RSS Comments RSS 132 Posts and 25 Comments till now

Archive for March, 2008

I love code snippet challenges

I stumbled upon a blog with a random code snippet challenge. Here was the problem at hand:

Given a String (x) containing only characters a-z, write a function (f) that returns a base 10 integer, which converts the String as if it were a base 26 numeral. Function f is bijective.

I wanted to give it a stab in javascript not because it sounded particularly difficult but to play with different javascript code styles.  Here’s my 4 versions:

  1. function f(i) {
  2.  
  3.     if (!i) { return 0; }
  4.  
  5.     var parts, n, pow, val;
  6.     parts = i.split();
  7.     parts.reverse();
  8.  
  9.     val = 0;
  10.     pow = 1;
  11.     for (n = 0; n < parts.length; ++n) {
  12.         val += pow * (parts[n].charCodeAt(0) - 96);
  13.         pow = pow * 26;
  14.     }
  15.  
  16.     return val;
  17. }

The first one turns the string into an array and reverses it before doing the math loop. My next try eliminates the array and iterates backwards over the string.

  1. function f(i) {
  2.  
  3.     if (!i) { return 0; }
  4.  
  5.     var n, mult = 1, val = 0, offset = 96, base = 26;
  6.  
  7.     for (n = i.length - 1; n > -1; n–) {
  8.         val += mult * (i.charCodeAt(n) - offset);
  9.         mult = mult * base;
  10.     }
  11.  
  12.     return val;
  13.  
  14. }

Here I’m playing with chaining array functions. While I don’t create an array, this is very similar to the first try.

  1. function f(i) {
  2.  
  3.     if (!i) { return 0; }
  4.  
  5.     var mult = 1, val = 0, offset = 96, base = 26;
  6.  
  7.     i.split().reverse().forEach(function(el) {
  8.         val += mult * (el.charCodeAt(0) - offset);
  9.         mult = mult * base;
  10.     });  
  11.  
  12.     return val;
  13.  
  14. }

Finally, recursion instead of an iterative loop.

  1. function f(i, mult) {
  2.  
  3.     if (!i) { return 0; }
  4.     if (!mult) { mult = 1; }
  5.  
  6.     var offset = 96, base = 26;
  7.     return mult * (i.charCodeAt(i.length - 1) - offset)
  8.              + f(i.substr(0, i.length - 1), mult * base);
  9.  
  10. }

Checkout the test output of my functions. Works in Firefox 1.5+ or other browsers that implement more modern javascript functions. Shouldn’t work in IE.

Quick www. survey

I’ve been trying to pay attention to sites that either add a www or remove www from their domain. For example http://www.reddit.com/ turns into http://reddit.com/ or http://cnn.com/ turns into http://www.cnn.com/.

I’ve been defaulting any new domains I setup to remove the www.  Remember,  www. is deprecated.

Here’s a quick survey of some popular sites:

adobe.com adds www
amazon.com adds www
bbc.co.uk adds www
bloglines.com adds www
blogspot.com adds www
cnn.com adds www
engadget.com adds www
facebook.com adds www
forbes.com adds www
google.com adds www
last.fm adds www
microsoft.com adds www
mozilla.com adds www
msnbc.msn.com adds www
myspace.com adds www
politico.com adds www
salon.com adds www
techcrunch.com adds www
w3.org adds www
washingtonpost.com adds www
whitehouse.gov adds www
wired.com adds www
   
creativecommons.org removes www
digg.com removes www
gizmodo.com removes www
news.yahoo.com removes www
reddit.com removes www
slashdot.org removes www
tinyurl.com removes www
twitter.com removes www
wordpress.org removes www
   
blogger.com no change
boingboing.net no change
boston.com no change
delicious.com no change
flickr.com no change
geocities.com no change
gmail.com no change
imdb.com no change
nytimes.com no change
prweb.com no change
technorati.com no change
youtube.com no change

Belchin’ Waffles is back!

It’s been a year or so since the domains dropped off the face of the Internet (I moved and was too lazy to put them up).  They’re back!

The Original Belchin’ Waffles Zine: http://belchinwaffles.com/

Friend message board (the original twitter, lol): http://friends.belchinwaffles.com/

Douchey party pictures, etc: http://friends.belchinwaffles.com/gallery/

IE 8 Beta

IE 8 Features

IE 8 beta is out.  Looks like they’re making great strides to improve their support for standards.  I love that you can install the beta with out the need to uninstall it when it breaks sites.  I’m hoping they keep the Emulate IE7 button for the final release.  Or somehow tuck it away in a menu so the option is still there.  An amazing feature for web developers would be an additional Emulate IE6 feature.  Testing sites in Internet Explorer would be a breeze if that was the case.  I haven’t played with the developer tools much, but I like how much easier they’re making JS debugging for IE.  Here’s a post from the IE Blog about the new developer tools

In and out all day

Like any mature individual my age, I find ‘that’s what she said’ references to be the funniest joke possible.

I recently helped my friend Devlin setup a new blog.  I’m trying to get all my friends into blogging or having their own site.  I feel it’s good to get the conversion out of the closed world of facebook/myspace.

She picked a rad domain name for her blog:  http://waityouremrodonnellsdaughter.com/.  Devlin told me a story a few weeks ago involving someone defacing creating a work of art on her whiteboard.  ‘Not amused’ is what she said in response.  Devlin just wrote a followup after she caught the culprit in another act of defacement.