« Archives in June, 2007

Programming Pearls: A solution to Exercise 1-6

“What would you recommend to the programmer if … each integer could appear at most ten times? How would your solution change as a function of the available storage?”

I ignored the last part of this question because it’s a lot like the previous.

My solution to the first part basically involves maintaining a count for each integer instead of a single bit. You can count up to 10 in a nibble — up to 15, actually — so you can get away with using 4 bits for every int.

My solution, which I called NibbleCounter for lack of a better name, can be found here:

NibbleCounter: NibbleCounter.h, NibbleCounter.cc
With syntax highlighting: NibbleCounter.h, NibbleCounter.cc

Programming Pearls: A solution to Exercise 1-5

“Solve Problem 1-3 using strictly no more than 1 megabyte of space.”

I didn’t actually write code for this one since it’s not much different from 1-3. We’re only allowed 1 megabyte, or 8 388 608 bits. So we can only maintain flags for up to roughly 8 million ints at a time, but we’ve got 10 million to deal with.

My idea was to do two passes of the list. First time around you accept only integers < 5 000 000, and slot them in the bit vector as usual. You then emit all the integers that have bits set in the vector.

Clear the vector, and now in the second time around you accept only integers 5 000 000 to 10 000 000. You’ll have to subtract 5 000 000 each time from the number you’re handling in the unsorted list to actually find the index in the bit vector, and then add it again when you spew the numbers from the last half of the list on the screen.

Turns out that’s the solution that Bentley came up with too. Woo. I can’t really think of any other way to do it…

Blackened Brussels Sprouts recipe

Tonight I tricked myself into thinking that I had this recipe archived here already before I started cooking, and had the oil smoking in the pan before I realized that I never got around to writing it down. Thankfully I managed to figure it out again. I don’t want to make that mistake again, so here goes…

What you need:
Brussels sprouts (however many you feel like making)
Smallish cooking onion
x cloves of garlic, where the magnitude of x is a function of how much you like garlic
Soy sauce
Balsamic vinegar
Walnuts, sunflower seeds, peanuts, sliced almonds, or some other seed or nut that suits your fancy

What do to:

1. Slice the sprouts thinly. I usually cut the asses off (well, what else would you call them?) and then slice them lengthwise into thirds. Keep all the little leaves that fall off as well.

2. Mince or crush the garlic, and thinkly slice or mince the onion.

3. Coat the bottom of a skillet or other thick pan with oil and heat it at medium high until the oil is almost smoking.

4. Toss the sprouts in, making sure that there’s enough room so that they all have their surfaces touching the bottom of the pan. I usually have to do a couple of batches to cook all the sprouts that I bought.

5. Every couple of minutes or so, stir them around so that they blacken sort of evenly on both sides.

6. When they start to soften a bit, toss in the onions and the garlic, and cook until those are getting soft or browned.

7. At the last moment, splash roughly equal amounts of soy sauce and balsamic vinegar into the pan and stir a bit so that it reduces. Throw in the seeds/nuts after the liquids thicken, stir it around one last time, and you’re good to go.

I’ve probably stolen this recipe off of the net from somewhere, but it’s been so long that I can’t remember where from. It’s most likely the first or second hit in the Google search results for ‘blackened brussels sprouts’, but I’m too lazy to check… credit goes to the folks who made this one up, it certainly wasn’t me.

Some thoughts and some books about doing a good job, or “what has happened to me since 2006″

This post is a hybrid of a couple of book reviews and an update of how my outlook on life has changed over the past year or so.

When I returned from California in September of last year, I was mildly burnt out. I had spent eight months or so trying to cram as much professional experience as I could into this crankcase of mine while I had the opportunity. I worked a lot during the week, and on the weekends that I wasn’t working I would be reading about something. I had hobbies and such, but hardly any social life at all, and I felt like I had spent most of my life learning and hardly any of it doing anything tangible. I resolved to change things when I got back to Vancouver.

»Read More

Programming Pearls: A solution to Exercise 1-3

“Compare the runtime of the system sort and bitmap sort.”

The program I used to do the comparison is here, in hybrid C/C++: sort_compare.cc, with syntax highlighting if you prefer.

Tests we run on my MacBook. I used the program listed in the previous post (the solution to exercise 1-4) to generate the test data. I omitted the time required to print the sorted list to the terminal, but included the time required to read the numbers from disk. Numbers were restricted to the range [0, 10 000 000]. Times are stated as user time in seconds, as measure by the “time” command.

k = 1 000 000k = 5 000 000
System sort1.57.7
Bit vector sort1.35.7

The bit vector sort is faster and the difference becomes more noticeable as the file size grows. It could potentially be made faster still by not copying the results back to the input vector and by inlining the Set() and Get() member functions, but the first is sort of an artificial use case and the second is just an unholy pain. You have to love compilers that are smart enough to rearrange your code in arcane ways but that won’t permit you to inline member functions unless you ram the whole implementation into the header. Blargh.