rosetta-stone

Code Golf: Hourglass

The challenge The shortest code by character count to output an hourglass according to user input. Input is composed of two numbers: First number is a greater than 1 integer that represents the height of the bulbs, second number is a percentage (0 - 100) of the hourglass' capacity. The hourglass' height is made by adding more lines to...

Insertion Sort Code Challenge

My task for you this time is to implement an insertion sort algorithm. Sure, everyone and his dog can do it; but how easy is it to do in the fewest characters? One must take an array (or suitable alternative in your language) of 32-bit integers and sort them by value via the insertion sort method, then return the sorted array as a copy ...

Code Golf: Sierpinski's Triangle

The challenge The shortest code, by character count to output an ASCII representation of Sierpinski's Triangle of N iterations made from the following ASCII triangle: /\ /__\ Input is a single positive number. Test cases Input: 2 Output: /\ /__\ /\ /\ /__\/__\ Input: 3 Output: /\ ...

Remove Duplicates From File (vs COBOL)

This Cobol question really piqued my interest because of how much effort seemed to be involved in what seems like it would be a simple task. Some sloppy Python to remove file duplicates could be simply: print set(open('testfile.txt').read().split('\n')) How does removing duplicates in the same file structure as above in your langua...

Code Golf: Running Water

The challenge The shortest code by character count to identify and mark water depressions in the ASCII representation of a land from input. Input will be an ASCII representation of a landscape, having hills, valleys and flat lands. The program should simulate what the landscape would look like if if was flooded - filling all valleys wi...

A puzzle - a program printing its own source

Blast from the past. This is one of the puzzles from my early days: Can you write a method (a function) which when called outputs its own source - literally including all the quotes, indentations, etc? No cheating - no reading from a file ...

Turing Machine Code Golf

Ok guys, today's goal is to build a Turing machine simulator. For those that don't know what it is, see the Wikipedia article. The state table we are using today is found at the end of the Formal Definition that's part of that page. The code will take a sequence of "0" and "1" string characters, an integer representing the character tha...

Code Golf: Ulam Spiral

The Challenge The shortest code by character count to output Ulam's spiral with a spiral size given by user input. Ulam's spiral is one method to map prime numbers. The spiral starts from the number 1 being in the center (1 is not a prime) and generating a spiral around it, marking all prime numbers as the character '*'. A non prime wi...

Code Golf - Tetris Fitting

Time for NP-Complete code golf! Given a rectangular board, and a set of tetrominoes, can the tetrominoes be fit into the board such that all pieces are used, no space is left over, and the pieces don't overlap? See Wikipedia if you've been living under a rock and don't know what Tetrominoes are - they're just the pieces from Tetris. Inp...

Code Golf: Diamond Blackjack

The challenge The shortest code by character count to output a best-case blackjack hand from the list of number-cards given. Input is a list of numbers from 1 to 10 (inclusive) separated by space. Output will be the best blackjack hand formed from that list of cards - the closest available combo to reach 21 by the sum of all card valu...

Code Golf: Does a binary number lie within the Cantor set?

Definition The Cantor set T, sometimes called the Cantor comb or no middle third set (Cullen 1968, pp. 78-81), is given by taking the interval [0, 1] (set T0), removing the open middle third (T1), removing the middle third of each of the two remaining pieces (T2), and continuing this procedure ad infinitum. The challenge Given a s...

Code Golf: New Year's Fireworks

The year 2009 is coming to an end, and with the economy and all, we'll save our money and instead of buying expensive fireworks, we'll celebrate in ASCII art this year. The challenge Given a set of fireworks and a time, take a picture of the firework at that very time and draw it to the console. The best solution entered before midnig...

Given list, subset of list: return bitmask (bit vector same length as list) corresponding to the subset.

For example, bitmask({1, 2, 3, 4}, {2, 4}) returns {False, True, False, True} The naive way to do this is to map a membership function over the list: map(lambda(x, member(x, subset)), list) but that's O(n*m) and it can be done in O(n+m). What's the best way to implement bitmask() in your favorite language? PS: It doesn't actua...

Code Golf: Lights out!

The challenge The shortest code by character count to solve the input lights out board. The lights out board is a 2d square grid of varying size composed of two characters - . for a light that is off and * for a light that is on. To solve the board, all "lights" have to be turned off. Turning off a light is made 5 lights at a time - t...

How to generate a random number from scratch

How can a random number be generated from scratch, i.e without using any built-in features of the language / outside API that will help with the randomization process? For example php has a rand() function and a seed function, and I'm sure java also has some similar stuff. You can use built-in functions to get the current time etc, but...

Rosetta Stone: Y-combinator

The Y-combinator is defined as: Y = λf. (λx. f (x x)) (λx. f (x x)) Using this combinator, you can write recursive lambda functions or intercept recursive methods with custom code. How is the Y-combinator written in various languages? I'd be interested in seeing the Y-combinator defined and used to implement a recursive factorial fu...

Is there any WCF equivalent in any platform other than .NET?

In other platforms can I develop a service without worrying about addresses and bindings? What is the name of equivalent technologies for Java, Python and other environments? Edit: For those who are unfamiliar with WCF, this is a part of the .NET 3.x framework whose main feature is to enable developing services that can answer multiple ...

Rosetta Stone: Building a string consisting of a character repeated n times

This question has been asked so many times for so many different languages, with some fascinating answers. I propose compiling the answers here so we can compare them side by side. Also, how is it done in the languages not yet "covered" by a question on SO? One programming language or idiom per answer, please. ...

Selecting by observation in R table

I was working through the Rosetta Code example of the knapsack problem in R and I came up with four solutions. What is the best way to output only one of the solutions based on the observation number give in the table? > values[values$value==max(values$value),] I II III value weight volume 2067 9 0 11 54500 25 25 211...

Find SmallestInt : Smallest integer greater than or equal to n that contains exactly k distinct digits(given values of n and k)

Hi guys, I found this on the internet. It looks good an interview question. I guess I got the working correct but programatically I haven't tried it. SmallestInt You are given an integer n. Return the smallest integer greater than or equal to n that contains exactly k distinct digits in decimal notation. Definition ...