code-golf

Language w/ the smallest interpreter written in itself

What is the smallest (code-golf) interpreter that is written in the language it interprets. I think overall this would be a good indicator as to the balance of how robust a language is. Extremely simple (turing tarpits) languages are difficult to write large programs like interpreters in. More 'sugary' languages are simpler to write i...

C Golf: # spaces in string

I'm trying to come up with with shortest C function (including spaces, any macros that are substituted into it) possible that is passed a NULL-terminated string and returns the number of spaces in that string. This is what I've got so far: int n(char*l){return*l?!(*l-32)+n(++l):0;} Which is 42 characters long. Any improvements? ...

Code Golf: Prime Factors of a Number

What is the shortest way, by character count, to find prime factors in any number? Example Input: 1806046 Example Output: 2x11x11x17x439 Example Calculator ...

Code Golf: Solve a Maze

Here's an interesting problem to solve in minimal amounts of code. I expect the recursive solutions will be most popular. We have a maze that's defined as a map of characters, where '=' is a wall, a space is a path, '+' is your starting point, and '#' is your ending point. An incredibly simple example is like so: ==== + = = == = # =...

Code Golf: Duplicate Character Removal in String

The challenge: The shortest code, by character count, that detects and removes duplicate characters in a String. Removal includes ALL instances of the duplicated character (so if you find 3 n's, all three have to go), and original character order needs to be preserved. Example Input 1: nbHHkRvrXbvkn Example Output 1: R...

Code Golf: Morse code

The challenge The shortest code by character count, that will input a string using only alphabetical characters (upper and lower case), numbers, commas, periods and question mark, and returns a representation of the string in Morse code. The Morse code output should consist of a dash (-, ASCII 0x2D) for a long beep (AKA 'dah') and a dot...

Code golf: Reverse quine

Write a program that outputs the reverse of its source code as a string. If the source is abcd efg (i.e., the C string "abcd\nefg") Then the output should be gfe dcba (i.e., the C string "gfe\ndcba") Bonus points for using esoteric languages such as brainf*ck. *EDIT:** Removed the unnecessary \0 characters.+ ...

Code Golf: The wave

The challenge The shortest code by character count to generate a wave from the input string. A wave is generated by elevating (line-1) a higher character, and degrading (line+1) a lower character. Equal characters are kept on the same line (no elevating or degrading done). Input is made of lower case characters and numbers only, lett...

Coding brain teaser to update an array (language agnostic)

All, I need a clever way to implement this algorithm (for work) as quickly and cleanly as possible: I think I've removed all the language specific issues and boiled it down to this: I have two arrays: A and B. A has a list of names in it {Apple, Apple, Banana, Banana, Banana, Carrot, ...} each i-th value has no upper limit on the num...

Code Golf: Mathematical expression evaluator (full PEMDAS)

I challenge you to write a mathematical expression evaluator that respects PEMDAS (order of operations: parentheses, exponentiation, multiplication, division, addition, subtraction) without using regular expressions, a pre-existing "Eval()"-like function, a parsing library, etc. I saw one pre-existing evaluator challenge on SO (here), b...

Code Golf: Email Address Validation without Regular Expressions

(Edit: What is Code Golf: Code Golf are challenges to solve a specific problem with the shortest amount of code by character count in whichever language you prefer. More info here on Meta StackOverflow. ) Code Golfers, here's a challenge on string operations. Email Address Validation, but without regular expressions (or similar parsin...

Code Golf: Seven Segments

The challenge The shortest code by character count to generate seven segment display representation of a given hex number. Input Input is made out of digits [0-9] and hex characters in both lower and upper case [a-fA-F] only. There is no need to handle special cases. Output Output will be the seven segment representation of the inpu...

Code Golf - Word Scrambler

Please answer with the shortest possible source code for a program that converts an arbitrary plaintext to its corresponding ciphertext, following the sample input and output I have given below. Bonus points* for the least CPU time or the least amount of memory used. Example 1: Plaintext: The quick brown fox jumps over the lazy dog. ...

Code Golf: Decision Tree

In Google Code Jam 2009, Round 1B, there is a problem called Decision Tree that lent itself to rather creative solutions. Post your shortest solution; I'll update the Accepted Answer to the current shortest entry on a semi-frequent basis, assuming you didn't just create a new language just to solve this problem. :-P Current rankings: ...

Simple fun C++ Asterisk Hill

Assume n is odd and is taken as user input. Can you write C++ code that takes the number n (being odd) and creates a star hill. For instance, assume n is 5 the output would be: * *** ***** *** * Could this entire hill be generated using 2 for loops? I havent done C++ in over 12 years and was playing with it... I was able to do: ...

Code Golf: Calculating the intersection between Box and Line

The challenge Given you have the coordinates of a rectangle and a line in 2D space, and they may or may not intersect, write a function that calculates the intersection. Here is an example of an intersection in glorious ASCII art: +-------------+ | | | | ----*------- | | | |...

Code Golf: Lasers

The challenge The shortest code by character count to input a 2D representation of a board, and output 'true' or 'false' according to the input. The board is made out of 4 types of tiles: # - A solid wall x - The target the laser has to hit / or \ - Mirrors pointing to a direction (depends on laser direction) v, ^, > or < - The la...

Code Golf: Banknote calculator

This question was posted by a C beginner and it was an exercise to calculate, given a dollar value input by the user, the minimum number of bills (or banknotes, depending on your locale) needed to reach that dollar value. So, if the user entered 93, the output would be: $20 bills = 4 $10 bills = 1 $5 bills = 0 $1 bills = 3 Finally su...

Code golf: Reverse Polish notation (postfix) evaluator

After having had code-golf contests on ordinary mathematical expressions, I just thought it would also be quite interesting how short an evaluation function for postfix notation (RPN) can be. Examples for RPN: 1 2 + == 3 1 2 + 3 * == 9 0 1 2 3 + + - 2 6 * + 3 / 1 - == 1 3 2 / 2.0 + == 3.5 To make things shorter, only +, -, * and /, a...

Code Golf: Beehive

The challenge The shortest code by character count that will generate a beehive from user input. A beehive is defined a a grid of hexagons in a size inputted by the user as two positive numbers greater than zero (no need to validate input). The first number (W) represents the width of the beehive - or - how many hexagons are on each ro...