My Octopress Blog

A blogging framework for hackers.

Heapsort

I was reading about Algorithm Geeks, a Google Group dedicated to algorithm-related questions apparently. I looked at one of the topics, Unique Elements in an Array, wondering what the chatter looked like.

To remove duplicate elements of an array, the best way I know is to sort your array, and then do a linear traversal, comparing each to its successor, and when they match, deleting the one of them. Quicksort is generally the algorithm-of-choice in large part because of its simplicity, but someone on this thread suggested heapsort - an algorithm of which I had never heard. I looked it over, and was drawn to the article on heaps. On that page, I was looking at an analysis of the time complexity for building the heap, and I got curious about the summation they present (h/2^h). So, I took some time and proved its convergence:

Convergence of Infinite Sum

Bad-Code-Free Zone

A little over a month ago, I walked into to my brand new job - an internship at ReadyTalk. Down a corridor, and there’s a wall covered with dry-erase boards, with the phrase “Bad-Code-Free Zone.” At first, I thought this was meant to be a sort of inspirational semi-strict policy / challenge to / for developers until about a week later, a jokester erased to the “Bad,” leaving us all to imagine a “Code-Free Zone.”

A few days later, a “C” disappeared, leaving us without odes of any kind - “ode-Free Zone.”

Days later, “Diode-Free Zone,” with the accompanying symbol for a diode with a large “NO!” written next to it. Now, as I get up to go to the bathroom, I’m left thinking about a “Lymph Node-Free Zone.”

You know, I never much cared for lymph.

Doctor Who

I just caught up on the Doctor Who I had missed in the last couple of weeks, and I am just continually impressed at how wrapped up in it I can get. Seriously, it’s so incredibly fantastic.

I realize that watching it makes me kind of a nerd (not that I needed any help in that department), but it just gets me so involved each episode. Twists, turns, adventure and cleverness, I can’t get over how much I enjoy it.

Bus Crazy

“You must be sophomores or juniors,” began a man sporting a cowboy hat and giant backpack. A sign strapped to his pack read, “Montana Old Vet.” He was speaking to a couple of girls who were sitting behind me on the bus.

“Or at least approaching the junior… approach,” he said with surprising deliberateness. One was a junior in college and the other was a recent graduate. “Congratulations.”

“The thing about life… There’s one thing you have to understand about life, and that’s the difference between the subjective and the objective.” At this point, I was expecting some mild old-man crazy, but I always get hopeful that an elder will have something real and useful to tell me. “If you understand that, you’re set.”

The girls, you could tell, were going to politely engage the man without trying to encourage him. They spoke a little while longer, and he wished them luck in their future endeavors. “The things about life,” he began again. Over the course of the conversation lecture, he would tell this “thing about life” several times. “You have to understand the difference between the subjective and the objective.”

He mentioned that he had been through the ropes “several times” over the course of his 59 years. He fought in the war, and made it clear that his three primary pursuits were fishing, beer and women. “You two are the most beautiful women I’ve ever talked to.”

“That’s very flattering.” “I don’t mean to flatter anyone.” “Do you know the difference between subjective and objective?” “Yes, I think so.” “Well?” “The objective is, like, truth, and the subjective is like, feeling.” “That’s why a professor will tell you to shut your fucking mouth. I don’t mean to insult you.” “Why don’t you tell us so we don’t get it wrong.” “God damn! Do I have to be the professor?”

He held out his pinky and began to explain that the objective was what you saw, and the subjective was what you felt, tasted, heard and smelled. “The thing about life is, it’s not a penis or a vagina, it’s you have to know the difference between the subjective and the objective.”

He told the girls about how he’d given away all of his money, and that it was important to understand the function of money and economics. When one of the girls mentioned she was going to go into law, he seemed to have a lot to say on the subject.

“You’re going to have defendants, and you gotta give the other guy hell. Are you smart enough to be a lawyer? Do you think you’re smart enough? You got to treat the judge with respect. Do you outsmart the guys?”

He talked about how he was going up to Montana to see his grandkids, and hunt and fish and tell stories. One of the girls asked that he eat some elk for her as she hadn’t had a chance in a while. “Do you hunt?”

“No, but my uncle does.” “Well, do you love him? You gonna marry him? You got to learn to hunt for yourself!”

He mentioned the war again, “I can tell you about any weapon… at the time. I once shot something from 288 yards. You know how I know? I walked there after. It took me two and a half hours.”

“What did you shoot? A deer? An elk?”

After a pause of several minutes he told her it was a Vietcong Lieutenant Colonel. “I sometimes think about this finger, about chopping it off because of it. I got to live with it. Those were my orders, and you gotta follow ‘em. I’m not saying it’s an excuse. The thing about life is…”

As far as crazy people go, pretty mild, but you never find crazy like bus-crazy.

Top Reasons It’s Awesome to Be in Engineering

A friend of mine (Andrew Ferguson) posted some reasons he thought it sucked to be an engineering student.

Yes, sometimes it’s difficult, but I think we often have a head start on our counterparts in a lot of ways by engineering.

Some of my favorite professors have been from computer science. Granted, that is what I study, but even from high school, many of my favorites were in math / science. My high school calculus teacher motivated me very much, and would spend hours with me after school talking about various proof-projects we had going on (yes, I’m apparently that kid).

Textbooks can be rough, I’ll give him that, but I think that’s in large part symptomatic of the information engineering textbooks must contain. They have to catalog a lot of things, and for my money, I think they generally do a pretty reasonable job.

While other disciplines may have inflated grades, I haven’t been denied opportunities because of my grades. I don’t have quite the GPA I’d like, but people know how difficult sciences are and appreciate it when considering you for positions.

Sometime the beatings the assignments put us through do tend to blend together; I don’t know about Andrew’s coursework, but this semester I’ve had some of the most exhilarating assignments I’ve ever had. We wrote our own shell, we downloaded and parsed RSS feeds using only shell (unfortunately not our own). For Scientific Computing, I admit there was a lot of work, but they were assignments that kept me up at night, out of pure curiosity.

This doesn’t even include the job market situation. Other engineering disciplines don’t feature the growth that computer science has seen in recent years, I admit, but Mines certainly has fantastic job placement. Of my closest high school friends, I have two in international relations, three in linguistics, one in liberal arts, and one in chemical engineering. The engineers enjoy the jealousy of the others with respect to the prospect of work.

All things considered, I’ve really my collegiate career up to this point, and I imagine I will continue to.

Scientific Computing

Two days ago I finished my final for Scientific Computing (it was a take-home final), and was somewhat sad to see the end of it. Sure, it was a lot of work (too much, according to Matt Matteson) - I spent literally upwards of a hundred hours this semester working on the homework and exams. I recently tallied up the number of pages I submitted, and it was upwards of a 135 8.5 x 11 pages, most of it marked up in LaTeX. (Because of this class my LaTeX-fu has grown significantly stronger.)

I look forward to any more classes I have with this prof.

Done-ish

For various reasons, two weeks ago, I feel a week behind in schoolwork. I have worked harder than I’ve ever worked for school, caught up, and I’m now almost finished with finals - just one more to go. A couple days after that, I’ll be starting my internship at ReadyTalk, and so the summer is starting to kick off more or less.

The 15th sees my friends and me moving into a place for a summer that promises to be filled with hanging out, organically grown food (Alan works on an organic farm), and wine.

First-in First-out, Least-Recently-Used

In my OS class, we’ve started talking about some page-replacement algorithms, including FIFO, LRU, and clock. These are going to be on an upcoming exam, and so Mike (our prof) suggested that we make up our own page access sequences, and then run the algorithm. That’s all well and good, but without knowing whether or not you got it right, problems might arise.

Enter Ruby - make a quick little script that will run the algorithm for you, so as to check your results.

fifo.rb lru.rb

Usage: fifo.rb "123412512345" 3 # Run the sequence provided with a queue size of 3 lru.rb "293847101928" 5 # Run the provided sequence with a stack size of 5

All the page numbers do have to be single characters, but letters and symbols are also accepted.

Sample output: Dan-Lecocqs-Mac:Desktop dlecocq$ ./lru.rb "123412512345" 4 1 * 1 2 * 2 1 3 * 3 2 1 4 * 4 3 2 1 1 1 4 3 2 2 2 1 4 3 5 * 5 2 1 4 1 1 5 2 4 2 2 1 5 4 3 * 3 2 1 5 4 * 4 3 2 1 5 * 5 4 3 2 Page faults: 8 / 12

Please note that you’ll have to change your shebang line to point to the path of your Ruby interpreter. I found Ruby in lots of places on my system, but check in your path. (Note: I’m using Ruby 1.8.4, but I don’t think I’m using a lot, or any, recent-version specific functionality)

If you’re not running it with “ruby fifo.rb …”, make sure you’ve chmod’d it to be executable.

Password Security

One of my favorite things is when websites show me this when I register:

I hate security

It’s almost as if they’re saying - “we hate strong passwords.” And it makes me trust even more that they’re actually hashing it in their database.

Growlnotify

If you’re a Growl user, you probably appreciate (or are annoyed by) being able to get updates from various applications in a relatively out-of-the-way fashion.

There is a command line utility included with Growl called growlnotify. It allows you to send up your own alerts from your own code (say using exec(3) or in your shell scripts), but it’s nice to use for other things, too. If I have a command that’s conceivably going to take a while, I tack on a && and growlnotify. Just like some people use

make && ./myapp

when they run their code to ensure it’s running the latest saved version, it’s a nice way to get a heads-up instead of waiting a few minutes for that god-awful command to run. For example, recently I was using curl(1) to get a bunch of files off of a server (when I fall behind on a webcomic, it’s just easier to download a bunch of them and browse them quickly in Preview instead of clicking through each), and it ended up taking a good 10 minutes:

curl http://boasas.com/boasas/[1-940].gif -o "#1.gif" && growlnotify --message "I'm done downloading" --title "curl"

I get back to reading my dailies, and by the time I’ve forgotten that I set it going, up pops a nice little notification:

Growlnotify

I will note that it can be a little finnicky. It generally has better success when no title is supplied, and if you’re going to be running it with &&, I might suggest making it a

mycommand && echo $? && growlnotify --message "my message" --title "mycommand"

Since it gets its message argument from standard in, you can also pipe to it if your command returns a value of which you’d like to be notified:

make ; if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi | growlnotify --title "make"

Growlnotify Make