rosetta-stone

What is your solution to the FizzBuzz problem?

See here Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Disclaimer: I do realize this is easy, and I understand the content of the Coding Horror post ...

Factorial Algorithms in different languages

I want to see all the different ways you can come up with, for a factorial subroutine, or program. The hope is that anyone can come here and see if they might want to learn a new language. Ideas: Procedural Functional Object Oriented One liners Obfuscated Oddball Bad Code Polyglot Basically I want to see an example, of different way...

How would you implement a hashtable in language x?

The point of this question is to collect a list of examples of hashtable implementations using arrays in different languages. It would also be nice if someone could throw in a pretty detailed overview of how they work, and what is happening with each example. Edit: Why not just use the built in hash functions in your specific languag...

Rosetta Stone - Sorting Arrays

I thought I would pose a question I'm describing as a "Rosetta Stone". That is to say, I am posing a question, and would like to see answers given for a number of different languages so that someone moving from one language to another can see how some typical operation is done. I have always found these types of comparisons to be very ...

Stack overflow code golf

To commemorate the public launch of Stack Overflow, what's the shortest code to cause a stack overflow? Any language welcome. ETA: Just to be clear on this question, seeing as I'm an occasional Scheme user: tail-call "recursion" is really iteration, and any solution which can be converted to an iterative solution relatively trivially by...

Nested for loops in different languages

Here is a fairly common problem. We have an array of arrays. We'd like to call some function for every combination of elements from the different arrays. Conceptually we'd like to do something like this: for my $x (1, 2) { for my $y ('a', 'b') { for my $z ('A', 'B') { print "$x $y $z\n"; } } } except that we don't...

Language showdown: Lazy (aka short-circuit) evaluation for And and Or applied to a list.

If I have a boolean function f(x) = {print(x); return is_even(x)} that does some time-consuming (in this example, for illustrative purposes, side-effect-producing) stuff and a list a={2,3,4} I can check if f() is true for every element of a with apply(And, map(f, a)) but that wastefully applies f to every element of a instead of retur...

Simplest, efficient ways to read binary and ascii files to string or similar in various languages.

Personally, I always forget this stuff. So I figured this is a useful thing to have as a reference. Read an ascii file into a string Write an ascii file from a string Read a binary file into something appropriate. If possible, store in a string, too. Write a binary file from something appropriate. If possible, write from a string, t...

Calculate the digital root of a number

A digital root, according to Wikipedia, is "the number obtained by adding all the digits, then adding the digits of that number, and then continuing until a single-digit number is reached." For instance, the digital root of 99 is 9, because 9 + 9 = 18 and 1 + 8 = 9. My Haskell solution -- and I'm no expert -- is as follows. digitalRoo...

Palindrome Golf

The goal: Any language. The smallest function which will return whether a string is a palindrome. Here is mine in Python: R=lambda s:all(a==b for a,b in zip(s,reversed(s))) 50 characters. The accepted answer will be the current smallest one - this will change as smaller ones are found. Please specify the language your code is in. ...

Fibonacci Code Golf

Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f, which prints the Fibonacci numbers. Starting point: 25 characters in Haskell: f=0:1:zipWith(+)f(tail f) ...

Capturing system command output as a string

Perl and PHP do this with backticks. For example: $output = `ls`; This code returns a directory listing into the variable $output. A similar function, system("ls"), returns the operating system return code for the given command. I'm talking about a variant that returns whatever the command prints to stdout. (There are better ways ...

Code Golf: Factorials

Since the palindrome code golf was a big hit, here is one that doesn't rely on built in functions. What is the shortest (in characters) way to write a factorial function? ...

Code Golf: Number to Words

The code golf series seem to be fairly popular. I ran across some code that converts a number to its word representation. Some examples would be (powers of 2 for programming fun): 2 -> Two 1024 -> One Thousand Twenty Four 1048576 -> One Million Forty Eight Thousand Five Hundred Seventy Six The algorithm my co-worker came up was alm...

Rosetta Stone: reservoir random sampling algorithm

I've been killing some time with a little puzzle project -- writing the algorithm described in Knuth for random sampling of a population without knowing the size of the population in advance. So far I've written it in JavaScript Rhino, Clojure, Groovy, Python, and Haskell. The basic parameters are: the function takes two arguments, a s...

Create, sort, and print a list of 100 random ints in the fewest chars of code

What is the least amount of code you can write to create, sort (ascending), and print a list of 100 random positive integers? By least amount of code I mean characters contained in the entire source file, so get to minifying. I'm interested in seeing the answers using any and all programming languages. Let's try to keep one answer per ...

Code Golf: How do I write the shortest character mapping program?

I learned a lot about various languages last time I did one of these, so I figured I'd submit another. Community Wiki of course... All programming languages are acceptable. The goal is to create the program in a language with the least amount of characters. New lines and indenting should be left in for clarity, but not counted. Try ...

Which libraries for handling very large integers in languages without native support?

Various questions (like this, and this) ask about how to handle integer values exceeding the native types of a particular language. Please reply here, with one response per language, recommendations for libraries designed for handling very large numbers in various languages. Where languages have built-in support for large numbers that'...

Algorithm: iterate over 2 variables in order of descending product

Note: This is NOT homework. Hi, It's easy to iterate over two variables and print their product like so: for a in range(1,100): for b in range(1,100): # or range(a,100) to prevent duplicates print( "%s = %s * %s" % (a*b,a,b) ) However, is it possible to come up with a looping structure that will iterate over a and b in desc...

Code Golf: Print the entire "12 Days of Christmas" song in the fewest lines of code.

Print all 12 verses of the popular holiday song. By 12 verses I mean the repetition of each line as is sung in the song, ie Verse One: On the first day of Christmas my true love gave to me a partridge in a pear tree. Verse Two On the second day of Christmas my true love gave to me two turtle doves and a partridge in a pear tree. ... ...