code-golf

Munging non-printable characters to dots using string.translate()

So I've done this before and it's a surprising ugly bit of code for such a seemingly simple task. The goal is to translate any non-printable character into a . (dot). For my purposes "printable" does exclude the last few characters from string.printable (new-lines, tabs, and so on). This is for printing things like the old MS-DOS debu...

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...

Can this Roman Number to Integer converter code be shorter?

95 bytes currently in python I,V,X,L,C,D,M,R,r=1,5,10,50,100,500,1000,vars(),lambda x:reduce(lambda T,x:T+R[x]-T%R[x]*2,x,0) Here is the few test results, it should work for 1 to 3999 (assume input is valid char only) >>> r("I") 1 >>> r("MCXI") 1111 >>> r("MMCCXXII") 2222 >>> r("MMMCCCXXXIII") 3333 >>> r("MMMDCCCLXXXVIII") 3888 >>> r...

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...

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...

Anti-golf on "Hello World"?

Contrary to the popular code-golf challenges which demonstrate the genius of many regulars here, I'd like to see that genius illustrated in an antithetical fashion. The challenge is to successfully perform "Hello World" with special focus on over-complicating matters. Not verbosity, not obscurity, just pure sloppiness/over-complication....

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...

Codility-like sites for code golfs

Hi, I've run into codility.com new cool service after listening to one of the recent stackoverflow.com podcasts. In short, it presents the user with a programming riddle to solve, within a given time frame. The user writes code in an online editor, and has the ability to run the program and view the standard output. After final submis...

Is the RLE algorithm flawed?

I was looking at a recent Code Golf on the removal of duplicate characters in a string. i mulled it over and thought that the RLE algorithm would solve it, in fact, I did believe that would resolve removing duplicates, I wrote an implementation here in C, to see how far I could go with it char *rle(const char *src){ char *p=(char *...

What is the shortest way to calculate the nth prime?

What is the shortest C code to "Calculate the n-th prime"? Shortest in terms of significant characters, i.e. the number of semicolons, non-whitespace characters, keywords and commas. Input: Integers n from the standard input, separated by new lines. The input will be terminated by EOF. Output: Right after the input n, print the n-th...

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: ...

Shortest code in C

Problem Statement : Sum the positive values in a series of integers. Input : t – number of test cases [t < 1000]. On each of next t lines given a integer N [-1000 <= N <= 1000] Output : One integer equals to sum of all positive integers. This is actually a SPOJ problem.I had solved it using python & ruby already but now I am interes...

Code golf: c++ deque->vector

I'm coding in C++ What is the shortest function of: template<T> std::vector<T> make_copy(const deque<T>& d) { // your code here } ? ...

Are there any "short tricks" in JavaScript 1.8 that I can use to reduce my golf score?

I have recently picked up golfing as a pastime. I've been trying to golf with JavaScript, and the amount of time I have spent with JavaScript 1.8 is about zero. Are there any nifty shortcuts I might be missing out on in JavaScript between 1.5 and 1.8 that I can rely on to reduce my golf score? ...

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...

Writing shorter code/algorithms, is more efficient (performance)?

After coming across the code golf trivia around the site it is obvious people try to find ways to write code and algorithms as short as the possibly can in terms of characters, lines and total size, even if that means writing something like: //Code by: job //Topic: Code Golf - Collatz Conjecture n=input() while n>1:n=(n...