StringBuilder builder = new StringBuilder();
string newline = "";
foreach (Map.Entry<MyClass.Key,String> entry : data.entrySet())
{
builder.append(newline)
.append(entry.key())
.append(": ")
.append(entry.value());
newline = "\n";
}Today's DailyWTF presented a programming challenge: write a function to multiply two numbers "Russian Peasant"-style. Reading that article, I immediately recognized two things. The first is that the algorithm essentially used a stack to hold completed portions of the calculation and then sums up that stack at the end. The 2nd is that stacks are fundamentally related to recursion. If I could write a stack-based solution I could also write a recursive one as well, and the code for the recursive solution would likely be fairly simple.
Of course, by the time I finished reading the article there were also already 22 comments, most of them solutions, one of them in VB.Net (not very good, imo) and one recursive one in Java, but I want to make it clear that I did not read any of those until after I completed my own implementation. It took less than 5 minutes to write and test the stack-based solution. In the end I discarded the stack as unnecessary and just kept a running total instead. It then required less then 5 additional minutes to adapt this to use recursion. I now had a working function for a grand total of 4 lines of code, and I felt it safe to take a peek at what others had done.
The only thing I added to my code based on the comments was to use bit shifts to multiply and divide by two. In one sense you could say it's cheating to write a multiplication function that uses the multiplication operator in any way, even if you're just multiplying or dividing by two. In this case I'd argue that the actual "Russian Peasents" suffered from the same chicken and egg problem, and so you've still met the spirit of the problem. But clearly the bit shifting is "better" from a standpoint of keeping your function "pure", and so I incorporated that into my code, which you can now see here:
Function multiply(ByVal x As Integer, ByVal y As Integer) As Integer
multiply = IIf(x Mod 2 <> 0, y, 0)
If x > 1 Then multiply += multiply(x >> 1, y << 1)
End Function
This space has really stagnated this year. And the thing is, it's not the only one. I have so many friends who have abandoned their blogs in the last 6 to 8 months. In fact, I'm going on record right now and calling 2009 the "year the personal blog died."
It's been a while since I've posted here, so to get caught up here a few of my own posts from StackOverflow that I'm most proud of:
It's been a few days now, so I've had time to think about what this will mean. My interest in Sun lies primarily with MySQL, Java, VirtualBox, and Solaris. As the title indicates, I'm cautiously optimistic.
First I want to consider MySQL. On the one hand, Oracle certainly knows databases. If they choose to they can really take MySQL to the next level. On the other hand, this wouldn't be the first time Oracle has purchased a better and cheaper competitor just to kill it off **cough**Peoplesoft**cough**.
The reason I'm optimistic that they'll do the former here is that MySQL will be a lot harder to just kill off than Peoplesoft. There's a recent open source version available, and they that back. If they try, they'll find that they don't have as much power in that area as they'd hoped. They best they can do is take it closed source and then release a few compelling updates, such that much of the community follows those updates and the open source clone's can't keep up. Only then can they kill it, and even at this point I'll be surprised if there's not still a viable open source-forked clone available.
Now on to Java. Under Sun, Java was starting to fall behind. Oracle relies on Java as the natural language choice for it's database, so it has every motivation to make sure the language stays relevant. That said, it will likely continue to be the same people driving development. I am a little concerned that Oracle will decide it needs to make more money from Java, but not too concerned. After all, there are a number of good open source Java tools available now, so just like MySQL the only way Oracle can really hurt Java is if they innovate at a faster pace than the open source community can emmulate. And that's not necessarily a bad thing.
I'm a little concerned that VirtualBox will cease to be free, and become more like VMWare. There's enough room to move here that Oracle can do what it wants. It's also very possible that Oracle will have no interest in continuing the product and will just turn it over to the community or sell it to someone else.
Finally, Solaris. Solaris is pretty much irrelevant. For Oracle, it gives them a native platform for their database without dependance on anyone else... if they want to go that route. In practice, Solaris is nothing if doesn't take advantage of open source libraries and rely on open source applications. I won't be surprised if Oracle kills Solaris, but I don't think it will matter much if they do. Those who used to use it can easily migrate to linux.
Follow up to this post.
public static string JoinWith(this IEnumerable<string> strings, string separator)
{
return String.Join(separator, strings.ToArray());
}Most .Net programmers who need to use JSON can either pull in a third-party library or are using .Net 3.5 and therefore can rely on the built-in JavascriptSerializer . However, I recently found myself needing to turn a .Net DataTable into JavaScript Object Notation without either of those options available.
My first thought was that something must already be out there that I could use. Indeed, there are some really great libraries for this. Unfortunately, my situation is such that I can't pull in a whole library. I needed some purpose-specific code. I did find some specific implementations as well, but I found them all some how lacking and ended up rolling my own. The code itself would take up about 2 printed pages, so I'll content myself with posting the link for now.
Today's question: How do I append a newline character for all lines except the last one?
boolean first = true;
StringBuilder builder = new StringBuilder();
for (Map.Entry<MyClass.Key,String> entry : data.entrySet()) {
if (first) {
first = false;
} else {
builder.append("\n"); // Or whatever break you want
}
builder.append(entry.key())
.append(": ")
.append(entry.value());
}This is something I stumbled on a while ago and known how to do in a basic sense. But now thanks to this StackOverflow question I also know what it's called. This will make a huge impact on my ability to use this in the future, as before I couldn't really search google or msdn to get help with it.
Today I came across a question about virtualizing web servers and databases. It caught my eye because I'd asked a similar question back in November.
This blog has been feeling a bit neglected recently. So in an effort to get it going again I'm re-purposing it. I'll still post original content from time to time, but effective immediately this place is now primarily dedicated to highlighting interesting questions I come across at StackOverflow.com. Essentially, I'm leveraging the community there to help me create content here. The important point is that I won't normally just repost a question. If I write about it here it's because I have something to add that may not be as appropriate for the other site.