rosetta-stone

Code Golf - Generate nearby page numbers based on the current page

The challenge is to create an algorithm for generating a specifically-sized subset of numbers in a sequence based on the current position in that sequence. While navigating through the many pages of content on a busy site like Stack Overflow or Digg it is often desirable to give the user a way to quickly jump to the first page, the last...

Code Golf: Who has the best poker hand?

I love challenges like this, I'll hopefully submit my answer soon. Which player has the best 7 card hand? Given an unordered list of 9 cards (separated by a space), work out which player has the best poker hand. Here is a list of poker hand rankings. Example input: 2C 5H AS KS 2D 4D QD KH 3S (ie: [[2C 5H] [AS KS] [2D 4D QD KH 3S]]) ...

Code Golf: Build Me an Arc

Challenge The shortest program by character count that accepts standard input of the form X-Y R, with the following guarantees: R is a non-negative decimal number less than or equal to 8 X and Y are non-negative angles given in decimal as multiples of 45 (0, 45, 90, 135, etc.) X is less than Y Y is not 360 if X is 0 And produces on ...

Understanding the motion of a disk using two static switches

This is a major re-write of the original question, I tried to clarify those points that were evidently confusing for some in my first version of the question. Thanks for the input in helping formulate the problem better! CONTEXT The challenge comes from a real-life coding problem. The first thing that responders should be aware of, is ...

Code-golf: Output multiplication table to the Console

Hi, I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each. This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to...

Code Golf: Frobenius Number

Write the shortest program that calculates the Frobenius number for a given set of positive numbers. The Frobenius number is the largest number that cannot be written as a sum of positive multiples of the numbers in the set. Example: For the set of the Chicken McNuggetTM sizes [6,9,20] the Frobenius number is 43, as there is no solution...

Checking whether a number is mathematically a perfect number

The algorithm should check a given number and return 'true' if it is a perfect number or 'false' if it is not. The wikipedia definition of a perfect number: In mathematics, a perfect number is a positive integer that is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number it...

Code Golf: Conway's Game of Life

The Challenge: Write the shortest program that implements John H. Conway's Game of Life cellular automaton. [link] EDIT: After about a week of competition, I have selected a victor: pdehaan, for managing to beat the Matlab solution by one character with perl. For those who haven't heard of Game of Life, you take a grid (ideally infinit...

Code Golf: Regex parser

The goal Today's Code Golf challenge is to create a regex parser in as few characters as possible. The syntax No, I'm not asking you to match Perl-style regular expressions. There's already a very reliable interpreter for those, after all! :-) Here's all you need to know about regex syntax for this challenge: A term is defined as a...

Are there mechanisms similar to Go's defer in other languages?

I am not familiar with mechanisms similar to Go's "defer" in other languages. Are there similar mechanisms in other languages? ...

File I/O in Every Programming Language

This has to be a common question that all programmers have from time to time. How do I read a line from a text file? Then the next question is always how do i write it back. Of course most of you use a high level framework in day to day programming (which are fine to use in answers) but sometimes it's nice to know how to do it at a low...

Code Golf: Happy Primes!

It's Sunday, time for a round of code golf! Challenge Write the shortest source code by character count to determine if an input number is a "happy prime", "sad prime", "happy non-prime", or "sad non-prime." Input The input should be a integer that comes from a command line argument or stdin. Don't worry about handling big numbers, ...

Any examples of very shocking undefined behavior?

People often comment that a program can legitimately respond to undefined behavior by deciding to reformat your hard drive. However, in practice undefined behaviors in most languages do something rather unsurprising, for example: Fail (e.g. crash, throw an exception, or otherwise interrupt execution). Refuse to compile. Yeah, that ...

Code Golf: Calculate Orthodox Easter date

The Challenge Calculate the Date of the Greek Orthodox Easter (http://www.timeanddate.com/holidays/us/orthodox-easter-day) Sunday in a given Year (1900-2100) using the least amount of characters. Input is just a year in the form '2010'. It's not relevant where you get it (Input, CommandLineArgs etc.) but it must be dynamic! Output sho...

Code Golf: JSON Binary Encoding/Decoding

The Challenge Encode ANY series of integers, floats, strings, nulls, booleans and arrays/objects of them, into a binary format and decode it again. The term object refers to a HashMap/Dictionary At the top level there's always a surrounding array or object, depending on the input The Test Input [-1, 0.1, 0, 'Foo', {'b': 8388608.25...

How to factor a number functionally

For example, if the input is 825 the output expected is (0 1 2 0 1). What this means is: 0 two's, 1 three's, 2 five's, 0 seven's and 1 eleven. Doing this imperatively was quite easy for me. Functional, not so much. Could you please guide me how to go about solving the above problem in a functional way? Note: Fold/reduce ways will be pr...

Emulating lisp cons cells in Tcl

A list in lisp is a series of cons cells, but in Tcl, a list is a string with whitespace separating the elements. For translating code from lisp to tcl, one might simply take lisp lists and translate them to Tcl lists. However, this runs into trouble with side effecting cons cells not coming across to the Tcl code. For example, consid...

Emulating lisp cons cells in Python

A list in lisp is a series of cons cells, but in Python, a native list is a different kind of object. For translating code from lisp to Python, one might simply take lisp lists and translate them to Python native lists. However, this runs into trouble with side effecting cons cells not coming across to the Python code. For example, co...

Code Golf: draw ascii art stars

Since this week noone has posted a code-golf challenge, I'll give it a try. I do it so you can do something other than playing with swords during those long compile cycles. The challenge: Draw ASCII art stars, given three numbers on standard input (number of spikes, type of star (star is drawn by joining vertices that are n vertices ap...

alternative solutions to this programming problem? all languages welcome

Create a method that: Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very beginning of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror...