rosetta-stone

Rosetta Stone: Lambda expressions

How are anonymous functions/lambda expressions expressed in various programming languages? Are the syntax and semantics especially useful or not useful in that language? Are there any programming languages for which true anonymous functions aren't possible? Like other Rosetta Stone questions, responses should start with the name of th...

Code Golf Christmas Edition: How to print out a Christmas tree of height N

Given a number N, how can I print out a Christmas tree of height N using the least number of code characters? N is assumed constrained to a min val of 3, and a max val of 30 (bounds and error checking are not necessary). N is given as the one and only command line argument to your program or script. All languages appreciated, if you s...

Code Golf New Year Edition - Integer to Roman Numeral

Write a program that take a single command line argument N and prints out the corresponding Roman Numeral. Eg N = 2009 should print MMIX. Let's say this should work for 0 < N < 3000. (Had fun playing my first ever round of code golf with the Christmas edition, and thought this could fit for New Year. Googled to see if this has come up...

Code Golf: Leibniz formula for Pi

I recently posted one of my favourite interview whiteboard coding questions in "What's your more controversial programming opinion", which is to write a function that computes Pi using the Leibniz formula. It can be approached in a number of different ways, and the exit condition takes a bit of thought, so I thought it might make an in...

How to get all subclasses?

Hello, this is a common problem that I always have when I use different languages, for example in Smalltalk you can do something like: aClass allSubclasses What about other languages? Like Java? PHP? Python? Please post snippets! ...

Finding closest match in collection of numbers

So I got asked today what was the best way to find the closes match within a collection. For example, you've got an array like this: 1, 3, 8, 10, 13, ... What number is closest to 4? Collection is numerical, unordered and can be anything. Same with the number to match. Lets see what we can come up with, from the various languages o...

Compare and Contrast Java Module/Plugin mechanisms

There are several mechanisms for module/plugin mechanisms: JSR-277 (now defunct), Project Jigsaw OSGi Eclipse plugins (currently built on top of OSGi) Netbeans plugins Android apps Glassfish plugins (also the lesser used HK2 system) java.util.ServiceLoader ... your favourite ... Inspired by this dZone article, I had hoped that we co...

Rosetta Stone: Great example projects

In your opinion, what is a great example application which demonstrates the best techniques for its language and problem domain, and could be used as a reference for other programmers? Please provide answers where the source is readily available for viewing (i.e. open-source projects), and provide a link. The first line of each answe...

RESTful webservice to sum a list of numbers.

PHP makes this sort of thing dirt simple but I'd like to know how other languages do it. To standardize on a simple example, how would you implement the following webservice to sum a list of numbers: http://server.com/sum?summands=LIST where LIST is a list of space-separated real numbers. For example, http://server.com/sum?summands...

Code Golf: Encode / Decode ascii binary

Decode the following string and encode it back. Any programming language is welcome, the point is to create a solution with the minimum amount of characters. Formatting and new lines does not count toward the char count. Here's the message: 1010100 1101000 1100101 1110010 1100101 1100001 1110010 1100101 110001 110000 1110100...

How do you create a function that returns a function in your language of choice?

Recently I've been learning Lua and I love how easy it is to write a function that returns a function. I know it's fairly easy in Perl as well, but I don't think I can do it in C without some heartache. How do you write a function generator in your favorite language? So that it's easier to compare one language to another, please wri...

Matrix Munging (with import and export to CSV file)

Here's a (simplification of a) task I do a lot: 1) Import a CSV file like the following: 1, cat, 10, junk 2, dog, 20, junk 3, mouse, 30, junk 2) Remove the fourth column and replace it with twice the third column. 3) Insert a new column that is half the third column. 4) Export to to CSV, ie: 1, cat, 5, 10, 20 ...

What's your take on this little input processing task?

I came across a little problem that looks very easy but requires a bit more thought than I first assumed. It can also be solved in many different ways and, I think, would be a perfect interview question. So what solution would you write down? Your input is a stream of pairs (x, y), with each x and y on a separate line. A short example, ...

How do you read a file line by line in your language of choice?

I got inspired to try out Haskell again based on a recent answer. My big block is that reading a file line by line (a task made simple in languages such as Perl) seems complicated in a functional language. How do you read a file line by line in your favorite language? So that we are comparing apples to other types of apples, please ...

How do you implement a dispatch table in your language of choice?

A dispatch table is a data structure that associates an index value to an action. It's a rather elegant replacement for a switch-type statement. Most languages have support for dispatch tables, but the support ranges from do-it-yourself to built-in and hidden under a layer of syntax. How does your favorite language implement dispatch ...

Objective-C arrays, style and spirit

Newbie question. Looking at arrays (ie: dynamically sized) this works: NSArray *array; array = [NSArray arrayWithObjects: @"one", @"two", nil]; This does not: array = [NSArray arrayWithObjects: 1, 2, nil]; Ok, I get it. This works: array = [NSArray arrayWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWith...

Email to IM gateway, and other ways to send Instant Messages programmatically.

It's very easy to send an email programmatically. For example, in Perl you can do this: open(MAIL, "|/usr/sbin/sendmail -oi -t") or die; print MAIL "From: ...\n"; print MAIL "To: ...\n"; print MAIL "Subject: ...\n"; print MAIL "\n"; print Mail "... body ..."; close(MAIL); I'd like to collect simple, self-contained snippets of code fo...

Code Golf: Automata

I made the ultimate laugh generator using these rules. Can you implement it in your favorite language in a clever manner? Rules: On every iteration, the following transformations occur. H -> AH A -> HA AA -> HA HH -> AH AAH -> HA HAA -> AH n = 0 | H n = 1 | AH n = 2 | HAAH n = 3 | AHAH n = 4 | HAAHHAAH n = 5 | AHAHHA n...

Your Language Regex match full name "last, first middle1 middle2 suffix"

Spent me 3 hours to get a good regex finished for parsing these 3 main possible situations. Redden, Taylor Captain Hasting Jr. Redden, Taylor Hasting Jr. Redden, Taylor Hasting full, l, f, m1, m2, s = /your_regex_here/.match("Redden, Taylor Captain Hasting Jr.").to_a I want to see what other possible answers there are beside min...

Code Golf: Quickly Build List of Keywords from Text, Including # of Instances

I've already worked out this solution for myself with PHP, but I'm curious how it could be done differently - better even. The two languages I'm primarily interested in are PHP and Javascript, but I'd be interested in seeing how quickly this could be done in any other major language today as well (mostly C#, Java, etc). Return only wor...