3 o'clock javascript

I was writing some code to react at a textarea.onKeyUp. I take the size of the current textarea.val().length, update an element and do some other stuff. yawn The first version of the code looked like: 1 2 3 4 $('#message').keyup(function(e){ $('#chars_num').html( new_len ); $('#sms_num').html( Math.floor($('#message').val().length / 161) +1) ); }); Working good, but was clearly slow: every keystroke was “giving back the cursor” too slowly for a fast typer like me. I went to take a look at twitter, and their text box was WAY FASTER. ...

March 28, 2010 · 1 min · 201 words

Pascal's Triangle generator

What’s Pascal’s Triangle? That’s what it is (Wikipedia has all the theory, if you need). Pascal’s Triangle first 6 rows The thing I wrote here is a generator of the n-th row of the triangle, that doesn’t use more then the memory needed to store the solution. Instead of allocating a Triangular Matrix, and building every row based on the one above, solution is built in place. How does it work The result is generated “filling the row from right to left”. I start initiating the element on the right hand side to ‘1’. Then, I run something like: ...

January 11, 2010 · 3 min · 480 words

Fibonacci's numbers calculator

Another simple-but-yet-interesting problem that I found challenging solving is the to Write a Fibonacci’s numbers calculator. It’s a REALLY SIMPLE problem, but still can demonstrate how superficial thinking in programming can lead to dramatically bad solutions. What’s a Fibonacci’s number A Fibonacci’s number is an integer number generated using the following function: Assumed that: f(0) = 0 f(1) = 1 for a generic “n” Integer: f(n) = f(n-1) + f(n-2) For example, the first 20 Fibonacci’s number are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 ...

January 5, 2010 · 11 min · 2288 words

Find the non repeating char in O(n) time and O(1) space - v2

My colleague and friend Luca (@lucabox) described a better solution to the problem of "Finding the first non repearing char in a string in O(n) time and O(1) space". It uses smartly the space, making the solution nicer and slicker. Or we are just 2 geeks that need to give more attention to their girlfriends :P Luca’s solution description The logic of this solution is based on the usage of an array of unsigned chars. Every char (assumed to be lowecase) has an associated small byte (1 char = 8 bits), where the bits 0x1 and 0x2 (the 2 least significant) represents, respectively, “present once in the input string” and “present multiple times in the input string”. After the input is “scanned” once, and every letter is marked with the correspondent “presence bit” (once, multiple or none), it get’s scanned a second time to find the first char of the input which has the bit “present once” set to “1”. ...

December 18, 2009 · 3 min · 503 words

Browser Adaptive CSS with AppEngine

As I said, I’m doing some stuff with Google AppEngine. And, of course, I’m facing the usual problem of Browser Incompatibility: Browser Incompatibility ;-) =Browser Incompatibilities: the Most Common Problem= The most common problem for Web Site developers is the fact that every browser treats HTML Tags, CSS and Javascript in it’s own way. This Recipe tries to address one of the problem I faced the most: having a slightly different CSS for every Browser. =The Usual Solution= The usual solution is to load every time a different CSS depending on the Browser. But this solution has some side effects: ...

September 21, 2008 · 1 min · 190 words

Debian on my NSLU2: The Revenge of the Swirl

After some playing with Unslung on my Linksys NSLU2, I realize it was a “very limited solution” for our needs. We need to share 4 (sometimes 5) NTFS (or others) volumes, where everyone of them is 500GB: this is too much even for the modified firmware of Unslung, unable to read the full directory trees (and the contained files) of my massive movie’s collection. [![](http://lh6.ggpht.com/detronizator/SC9F6fE2BaE/AAAAAAAAA8I/oz-Ujfnmj9g/s160-c/TheDebianNSLU2SReign.jpg)](http://picasaweb.google.com/detronizator/TheDebianNSLU2SReign)[The Debian/NSLU2's Reign](http://picasaweb.google.com/detronizator/TheDebianNSLU2SReign) So, I came back to the Debian/NSLU2 solution. This time, with all the intention to make it work. It’s quite pointless to report here all the things I did to make it work in the way I want/need. I’ll just write down the most important bits: ...

May 17, 2008 · 2 min · 296 words