Coworking

Paul Brown @ 2006-04-03T04:59:31Z

Via Scott McMullan:

What’s coworking? I think of it as your favorite wifi coffeehouse, but with paid membership, more explicit community, and focus on project work and collaboration.

After a quick look at the coworking wiki, I'd probably give it a shot if I were doing independent consulting. (Nonetheless, it doesn't look like there's anyone doing something similar in Seattle, at least by that name.) A cafe where I had a guaranteed table, a properly trained barrista on-call, bottomless americanos, some white noise, ample power, wifi, and a couple of cellular phone booths would make an attractive work environment even without the possibility of collaboration and conversation.

(comment bubbles) 0 comments

Comprehending the Problem

Paul Brown @ 2006-04-02T18:48:00Z

List comprehensions are a programming language convenience but also an instance of a good way to think about solving some kinds of programming problems. For example, consider the problem:

Given a set, define a function that returns its powerset, i.e., the set of all subsets.

For my purposes herein, the datatype for a set has a notion of membership, size, and enumeration.

Et tu, Bruté?

The brute force solution using recursion is roughly:

Set powerset(Set s) {
  if s == EmptySet {
    return {EmptySet}
  } else {
    x = first element of s
    y = powerset (s \ x)
    return y union (apply (__ union {x}) to y) 
}

And this is far from efficient, since the powerset of a set of n elements has 2n elements which have a total of n2n-1 elements between them:

# elements in union over powerset =
                       = sum (j*(n choose j),j=0 to n)
                       = sum(j * n!/((n-j!)*j!),j=1 to n)
                       = sum(n!/((n-(k+1))!*k!,k=0 to n-1)
                       = n sum( (n-1)!/((n-1)-k)!*k!),k=0 to n-1)
                       = n (1+1)n-1 = n 2n-1

Thinking a Little

So how could thinking about comprehensions help things? The idea is to use the definition (“set of all subsets”) to influence the implementation. For example, to implement the size method on the powerset:

Powerset implements Set {

  Set _s

  Powerset(Set s) {
    _s = immutable copy of s
  }

  int size() {
    return 2^_s.size
  }
}

The same goes for membership:

boolean contains(e) {
  return (e is a Set && e is a subset of _s)
}

The only item left would be generating an iterator that visits all elements of the powerset, but thinking in terms of comprehensions again suggests a good approach. A subset of the source set is defined by a function that returns true if an element is in the set and false if not. With the source set indexed by 0,..,n-1, this suggests an approach where each subset corresponds to a number between 0 and 2n in binary notation — 1 in the kth bit if the kth element is in the set.

The bit vector encoding approach can be used to get a generator-style, recursion-free solution without necessarily encapsulating the powerset, but the encapsulation is equally important because of the frame of mind that it creates. Thinking about what "is a subset" means is what actually leads to the encoding concept.

(comment bubbles) 0 comments

Java Brainteaser on Integral Types

Paul Brown @ 2006-03-24T05:35:17Z

The following code produces a compiler error (“Type mismatch: cannot convert from int to byte”):

byte a = 1;
byte b = 2;
byte c = a + b;

What motivated the designers of the Java language to make this an error?

(comment bubbles) 0 comments

NetNewsWire + NewsGator = Good

Paul Brown @ 2006-03-23T18:07:44Z

I tried the Bloglines thing with NetNewsWire for a while, but then I went to dump my blogroll as an OPML (which gets processed by an XSLT stylesheet and posted in the right column on my blog), but all of the xmlUrl attributes were Bloglines-specific URLs. That is, my blogroll was locked into Bloglines. That annoyed me, especially since I wasn't informed of the lock-in in advance, so I reconstructed my subscriptions and dumped Bloglines. (That was almost as bad as when a misbehaved hotel gateway b0rked my subscriptions by 301'ing the feeds to their signup page when I connected to the hotel network.)

With the new version of NetNewsWire, NewsGator sync is now an option, and it appears to work correctly. The web interface is automatically updated, as are remote aggregators when they poll the feeds, and the OMPL export is pristine. Much better.

(comment bubbles) 0 comments

Say "Bit Shift" Ten Times Quickly

Paul Brown @ 2006-03-20T23:06:00Z

This week's cute interview problem came from Hanson Char:

What is the best (time, space) way to convert a Java int to a four-byte array where the zeroth entry of the array contains the LSB?

The best solution that I'm aware of is to use shift right (>>>) and a cast to grab the bytes:

public byte[] toBytes(int i) {
  return new byte[] {
    (byte)i,
    (byte)(i>>>8),
    (byte)(i>>>16),
    (byte)(i>>>24)};
}

My initial solution included some &255 that a look at the Java Language Specification convinced me were superfluous. (Narrowing conversions just truncate the leading bits in twos complement notation.)

Checking the bytecode with either javap -c or the very convenient ASM plugin for Eclipse reminds me that shift-right is a single opcode, IUSHR, parameterized by the number of bits to shift.

One alternative suggestion was to use java.nio.ByteBuffer. That solution yields comparably compact code (putInt(i)) but on inspection, the implementation uses the package-private class java.nio.Bits which just incorporates the same shifting technique as above, and the result is not surprisingly significantly slower that just using the shifting technique directly.

(comment bubbles) 0 comments

Swearing Off the iTunes Music Store

Paul Brown @ 2006-03-14T08:12:00Z

I have a few hundred DRM-hobbled MP3s from the iTunes Music Store, but I've decided that it no longer deserves my business. Most of the music that I like is available DRM-free and in higher-bitrate encoding from Bleep, and when what I'm looking for is not available someplace like eMusic, paying a little more for a CD will get me both a version that I can play on a Squeezebox and the good feeling that I'm not supporting a world view that includes DRM.

I'm not opposed to rights, per se, but I like to be able to exercise them. The iTunes model seemed reasonable enough, but the Squeezebox changed my mind. I can't listen to iTunes-encrypted music on the Squeezebox because Apple hasn't licensed the necessary software to SlimDevices. Why should I have to pay whatever incremental cost the licensing would add to the cost of the Squeezebox in order to exercise the rights that I purchased through iTunes? (Yes, I realize that I can strip the DRM, use icecast to redirect audio streams, etc. — all of which is more trouble than paying the incremental cost of a CD.)

It wouldn't take much for someone to win my business — $1.50/song, $11/album, no DRM, high bitrate, critical mass of artists/labels, and something reasonable for community (“also bought”, etc.) or similarity (e.g., taxonomy). Any takers for iTunes “2.0”? (No, the subscription models from Real and Yahoo! are just 1.1 efforts in my view.)

Update: It looks like the French have similar sensibilities:

[...] “It will force some proprietary systems to be opened up ... You have to be able to download content and play it on any device,” Vanneste told Reuters in a telephone interview on Monday [...]

Good wine, good food, device independence... Vive la France!

(comment bubbles) 0 comments

Cute Interview Problem

Paul Brown @ 2006-03-05T09:25:00Z

I like to answer the basic engineering skills questions given to candidates that interview for a position in my org, just to make sure that I'm not getting too rusty. This problem was particularly cute and only takes about 5 minutes if you just plow through it; on the other hand, it can be done in thirty minutes or so even if a progression of hints is needed.

Here's the statement of the problem:

Given a batch of n jobs with positive durations {t0,...,tn} and positive difficulties {c0,...,cn}, what is the optimal order of execution for the batch under the assumption that the cost of performing a job is the product of the difficulty and the start time for that job?

One way to approach this kind of problem is to assume that we know the optimal order for n jobs, and the we just need to find a best location for the a new job with duration d and cost k in the sequence. WLOG, we can assume that the optimal order for the n jobs happens to be zero through n. If the original cost is OC, the total cost of scheduling the job at slot j is:

TC(j) = c1t0+...+k(tj-1+...+t0)+cj(d+tj-1+...+t0)+...
TC(j) = OC+k(tj-1+...+t0)+d(cj+...+cn)

Scheduling at j is better than scheduling at j+1 if TC(j+1)>TC(j):

0 < TC(j+1)-TC(j)
0 < (OC+k(tj+...+t0)+d(cj+1+...+cn)) - (OC+k(tj-1+...+t0)+d(cj+...+cn))
0 < ktj - dcj

And that's equivalent to k/d > cj/tj.

Thus, a lowest cost solution is to perform the jobs in descending order of difficulty per unit time. This problem also has the nice property that a greedy algorithm (“Do the hardest job per unit time first; repeat.”) produces an optimal solution.

(comment bubbles) 1 comment

All Posts contains 399 items in 57 pages of 7 items each:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57