Recursion

I made some comment to Kris recently about how I wish I had more formal computer science training. I took all of two programming classes in college, and learned the rest on my own. I believe I do okay, since programming is essentially solving one big logic problem, but every so often I solve a problem with brute force instead of creating an elegant solution.

Kris offered to help me out with the formal computer science education, and suggested we go through his Algorithms book. I agreed, figuring it would happen not before next year, given all of my copious free time.

When I was at Hackday yesterday, Kris pinged me and asked if PHP could handle recursion, which cracked me up (the answer being, uh, yes, of course). When I said yes, he asked me if I could write a recursive function that printed out a series of numbers, from one to the single function argument N. The two keys to this problem were recursion and single parameter.

Counting down from N to 1 was easy. I figure both are easy, but counting down is clearly the easier:

function kris($n = 1) {
  print "$n ";
  if ($n > 1) {
    kris($n - 1);
  }
}

Oddly enough, counting up was harder for me. I told Kris he couldn't offer any hints, and eventually I figured out a convoluted solution using a static variable. My solution clearly showed me my lack of CS education. Kris' solution was much simpler:

function kitt($n=1) { 
  if ($n>1) { 
    kitt($n-1);
  }
  print "$n ";
}

As Kris commented, people forget you can do the recursion first, and the action second, when writing recursive functions.

A candidate at Kris' work was unable to solve the problem, and he wanted to see if I could, especially since I was commenting on the CS education I didn't have. I did solve it, but not correctly.

Next time, I'll have the trick. This time? I felt like an idiot.

 How to keep an SSH tunnel open in MacOS X

Kitt asked me to post this, so here goes: ssh_command_no_exit.sh as follows:
ssh -N -l remoteprocess -L13307:127.0.0.1:3306 -R13307:127.0.0.1:3306 -o ServerAliveInterval=240 re.mo.te.ip
/Library/LaunchDaemons/ssh_tunneler.plist as follows:




   Label
   ssh_tunneler
   ProgramArguments
   
      /path/to/ssh_command_no_exit.sh
   
   OnDemand
   
   Disabled
   


This basically sets up the equivalent of an inittab:respawn on Linux. OnDemand=false indicates that the server should be continuously running. Type in
launchctl load /Library/LaunchDaemons/ssh_tunneler.plist
Finally, launchd kills something that fails within 60 seconds 10 times in a row. If our DSL/cable line goes out, this could take longer. So, add the following line to crontab:
#Re-list the ssh_tunneler
10      0       *       *       0       launchd load /Library/LaunchDaemons/ssh_tunnelr.plist >> /dev/null &2>1

 Using Bharat's Time Turner

Much to my pleasure, I have finally found a project that interests me enough to begin using Bharat's Time Turner with it. I've installed a development environment on my laptop that means every time I have two minutes to work on it, zing! out comes the laptop, and I start working.

Because everyone knows I don't go anywhere without my laptop.

Gah! Two minutes wasted typing this up!

 Note to self re: barcode scanners

I'm sure to forget this, so I'll write it down here, for when I implement the roster barcode scanner part of the rostering system for college. Via Joel on Software's article on How to Ship Anything:

"I didn't really know which barcode scanner to buy, and no matter how hard I tried, I couldn't find any online reviews of barcode scanners. I did find out that the kind of scanner I wanted was a USB keyboard wedge scanner. That means that it has a USB port, which makes connection simple, and it behaves exactly like a keyboard as far as your computer is concerned. I also found out that I wanted a laser scanner, not a CCD scanner, because it works at a distance. I ended up buying a Wasp WSL-9000 along with the stand which you can see in the picture. So far it's been great.

Setting up the barcode reader was the easiest thing in the world. Plug it in to a USB port, and you're done. It comes with a whole book of barcodes that you can scan to configure it to behave in many interesting ways, which you can throw away. Without any setup, when you scan a barcode, your scanner will type the letters that barcode represents on the keyboard and press Enter."

According to Google, though, that's the only place on the web that refers to that scanner as "Wasp WSL-9000", so search for Wasp WSL 9000" (no dash).

And get the one with the USB cable.

 Recovering from SHDH4

This morning started with a really loud, jarring phone ring, from not my phone, followed by a muffled conversation, followed by a wild haired, sleepy eyed, not-quite-sober looking attempt at standing up by my fellow all-nighter at SHDH (Mom: it was an overnight programming event where 60+ developers and designers descended on a house in the evening and programmed, designed, presented work, shared ideas, and socialized until the early hours).

My conversation with said guy:

me:  Are you sober?
him:  A good question, Kitt.
me:  And?
him:  The short answer is, "No." But I think I'm sober enough to drive.
me:  Doesn't the first answer negate the second one?
him:  blank stare

Um...

Yeah.

11 of us crashed at the SHDH, waking up the this morning to a smaller morning geek fest, with 8 of us huddled around the tables on laptops, sharing even more geek moments.

Eventually, calls for food overrode all entertainment, and the remaining 10 of us (Jeff, David, Lloyd, Messina, Tantek, Dani, Eris, Andy, Adam and me) drove to Buck's (south! closer to home for me!) for breakfast/lunch. Had an entertaining conversation about procreation with David, allowing my completely non-traditional views on children to filter out. David tried to explain (at my request) his the desire to have children: wanting to raise a mini-me, influencing positively another life, validating oneself through gene propogation. I think fundamentally if that desire is missing, all the reasons in the world aren't going to make it appear. David did point out, in a gentle non-judgemental way, however, how shallow my reasons for not wanting children are.

I took no pictures this weekend, which is a little odd.

Low point of the night: realizing Andy had just rebooted my laptop, and I hadn't saved its session state in any meaningful, recoverable way.

High point of the night: wrastling said laptop from the evil clutches of Andy and Cal as they attempted to both root it, and install the hello program on it that I really, really didn't want.

General impressions of the evening: had a great time, though the saying "You get out of life what you put into it," comes to mind. I had wanted to program in a code-jam scenario, but I spent more time socializing and less time actually programming than I wanted. Which is unfortunate given the level of expertise in that house last night. I think the large number of people made it more social, less tech, and had the numbers been around 20-30, more coding would have been done (by me, unsure in general).

Enjoyed meeting and talking to Elliot and Jesse and Adam and Bryan and Cal (though I'd actually met Cal before at the Carson Workshop in June, as he's the one that presented - Mike pointed out to me that I totally blown off Messina and Ryan King at lunch at that workshop to work on the pagination module during lunch - Andy pointed out he showed up for the bar meeting afterward). I especially liked Alex Russell's Dojo presentation. I'm terribly inspired to add it to the UPA's online rostering system.

Right after I finish up a couple of Messina's demos.

Syndicate content