rosetta-stone

Code Golf: Piano

The challenge The shortest code by character count to output a part of a piano keyboard starting from input note in a given length. Input will be composed of a note ([ACDFG]#|[A-G]) to start printing the keyboard from and a positive number representing length of keys to print including the first note. The first key should be printed i...

Code Golf: Tic Tac Toe

Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player 1 (X) 2 = player 2 (O) So, given the array b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ] would re...

Code Golf: Scrabble-able

Code Golf: Scrabble-able Input: a list of words Output: a legal arrangement for the words on a scrabble board (or false if impossible). Format of output is entirely up to you, as long as it is comprehensible. Bonus points for pretty printing. Scrabble board size is unbounded, no un-listed words should be found in solution. Examples: ...

zig zag scan algorithm

I need a pseudocode or real code on how to do a zig zag scanning on a 2-d array using the following pattern: I have tried to use google to find this, but can't find any answers... This pattern is usually used for image decoding ...

Code Golf: Collatz Conjecture

Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple it and add one. Repeat this with the n...

Code Golf: Triforce

This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable. When the input is 1, print the following: *********** ********* ******* ***** *** ...

Code Golf - π day

The Challenge Guidelines for code-golf on SO The shortest code by character count to display a representation of a circle of radius R using the *character, followed by an approximation of π. Input is a single number, R. Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. This means that whe...

Rewriting UNIX cal(1)

Hello, Today I was testing out SRFI 19 and wrote a simple version of the UNIX cal(1) command. Here's a version in R6RS Scheme which runs in Ikarus, Ypsilon, and Larceny. A few example runs. Schemers: How would you write it? Use your favorite implementation. Ruby and Python: I'm guessing that y'all have elegant date and time libraries...

Rosetta Stone: Observer Pattern

How is the Observer Pattern expressed in various programming languages? Can you provide a code snippet that illustrates the major differences in each language. This post is intended to demonstrate the differences of this commonly used design pattern. I will start this off with a Java example. Remember to start your answer off with the l...

Code Golf: Connecting the dots

You may remember these drawings from when you were a child, but now it's time to let the computer draw them (in full ascii splendour). Have fun! Description: The input are multiple lines (terminated by a newline) which describe a 'field'. There are 'numbers' scattered across this field (seperated by whitespace). All lines can be consid...

Code Golf: Easter Spiral

What's more appropriate than a Spiral for Easter Code Golf sessions? Well, I guess almost anything. The Challenge The shortest code by character count to display a nice ASCII Spiral made of asterisks ('*'). Input is a single number, R, that will be the x-size of the Spiral. The other dimension (y) is always R-2. The program can assume...

Code Golf: Word Search Solver

Note: This is my first Code Golf challenge/question, so I might not be using the correct format below. I'm not really sure how to tag this particular question, and should this be community wiki? Thanks! This Code Golf challenge is about solving word searches! A word search, as defined by Wikipedia, is: A word search, word find, wor...

Code Golf: Numeric equivalent of an Excel column name

The challenge The shortest code by character count that will output the numeric equivalent of an Excel column string. For example, the A column is 1, B is 2, so on and so forth. Once you hit Z, the next column becomes AA, then AB and so on. Test cases: A: 1 B: 2 AD: 30 ABC: 731 WTF: 16074 ROFL: 326676 Code count includes ...

Code golf: the Mandelbrot set

Usual rules for the code golf. Here is an implementation in python as an example from PIL import Image im = Image.new("RGB", (300,300)) for i in xrange(300): print "i = ",i for j in xrange(300): x0 = float( 4.0*float(i-150)/300.0 -1.0) y0 = float( 4.0*float(j-150)/300.0 +0.0) x=0.0 y=0.0 ...

Function which returns itself

As a purely academic exercise (read "because I have no life"), I'm trying to write a function f which accepts another function g, executes g for its side effect, and returns itself. So I have this: let rec f g x = ignore(g x) fun y -> f g y F# complains: fun y -> f g y;; -------------^^^^^ C:\Users\Juliet\AppData\Lo...

Code-Golf: Friendly Number Abbreviator

Based on this question: Is there a way to round numbers into a friendly format? THE CHALLENGE - UPDATED! (removed hundreds abbreviation from spec) The shortest code by character count that will abbreviate an integer (no decimals). Code should include the full program. Relevant range is from 0 - 9,223,372,036,854,775,807 (the upper li...

Implement a simple class in your favorite language.

I'm doing this to learn the syntax of different programming languages. So, how would you defined the following class along with with its operations in your favorite programming language? Image generated by http://yuml.me/ And a main method or equivalent to invoke it: For instance, for Java it would be: ... public static void ma...

Code Golf: MSM Random Number Generator

The challenge: The shortest code by character count that will generate a series of (pseudo)random numbers using the Middle-Square Method. The Middle-Square Method of (pseudo)random number generation was first suggested by John Von Neumann in 1946 and is defined as follows: Rn+1 = mid((Rn)2, m) For example: 34562 = 11943936 m...

Code Golf: Ghost Leg

The challenge The shortest code by character count that will output the numeric solution, given a number and a valid string pattern, using the Ghost Leg method. Examples Input: 3, "| | | | | | | | |-| |=| | | | | |-| | |-| |=| | | |-| |-| | |-|" Output: 2 Input: 2, "| | |=| | |-| |-| | | |-| | |" Output: 1 Clarifications ...

How can this closure test be written in other languages?

I wonder how the following closure test can be written in other languages, such as C/C++ and Java. Can the same result be expected also in Perl, Python, and PHP? Ideally, we don't need to make a new local variable such as x and assign it the value of i inside the loop, but just so that i has a new copy in the new scope each time. (if p...