rosetta stone - Code Golf: Leibniz formula for Pi

I recently posted one of my favourite interview whiteboard coding questions in "What's your more controversial programming opinion", which is to write a function that computes Pi using the Leibniz formula. It can be approached in a number of different ways, and the exit condition takes a bit of thought, so I thought it might make an interesting code golf question. Shortest code wins! Given that Pi can be estimated using the function 4 * (1 - 1/3 + 1/5 - 1/7 + ...) with more terms giving greater accuracy, write a function that calculates Pi to ...Read more

rosetta stone - 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 set1 = player 1 (X)2 = player 2 (O)So, given the array b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ] would represent the boardX|O|X-+-+- |X|O-+-+-X| |OFor that situation, your code should output 1 to indicate player 1 has won. If no-one has won you can output 0 or false.My own (Ruby) solution will be up soon.Edit: Sorry, forgot ...Read more

Code golf: find all anagrams

A word is an anagram if the letters in that word can be re-arranged to form a different word.Task:The shortest source code by character count to find all sets of anagrams given a word list.Spaces and new lines should be counted as charactersUse the code ruler ---------10--------20--------30--------40--------50--------60--------70--------80--------90--------100-------110-------120Input:a list of words from stdin with each word separated by a new line.e.g.AA'sAOLAOL'sAachenAachen'sAaliyahAaliyah'sAaronAaron'sAbbasAbbasidAbbasid'sOutput:All sets o...Read more

Code Golf: Reversi

OK, here's a rather elaborate code golf challenge: Implement a game of Reversi (Othello).The game should display the current state of the game-board and allow players at a single computer to alternately input moves.Incorrect input and disallowed moves must be caught, but can be ignored silently.The game must end when no more moves can be made (either because the board is full or because no move would flip any pieces).The game must then announce who won, or if it was a draw.Do this in as few characters as possible.A session should look something...Read more

Code Golf: Towers of Hanoi

RulesThe Towers of Hanoi is a puzzle, and if you are not very familiar with it, here is how it works:The play field consists of 3 rods, and x number of disks, each next one bigger than the previous one. The disks can be put on the rod, with these RULES:only one disk can be moved at once, and it must be moved on the top of another rodthe disk must be taken from the top of a roda disk can be moved somewhere, ONLY if the top-most disk at the target rod is bigger than the one to be movedAnd finally - the play field STARTS like this:a rod, with x di...Read more

Household mail merge (code golf)

I wrote some mail merge code the other day and although it works I'm a turned off by the code. I'd like to see what it would look like in other languages.So for the input the routine takes a list of contactsJim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004Erica,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004Marge,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7Te...Read more

rosetta stone - Code golf: the Mandelbrot set

Usual rules for the code golf. Here is an implementation in python as an examplefrom PIL import Imageim = 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 iteration = 0 max_iteration = 1000 while (x*x + y*y <= 4.0 and iteration < max_iteration): xtemp = x*x - y*y + x0 y = 2.0*x*y+y0 x = xtemp iteration += 1 ...Read more

Code Golf: Quickly Build List of Keywords from Text, Including # of Instances

I've already worked out this solution for myself with PHP, but I'm curious how it could be done differently - better even. The two languages I'm primarily interested in are PHP and Javascript, but I'd be interested in seeing how quickly this could be done in any other major language today as well (mostly C#, Java, etc).Return only words with an occurrence greater than XReturn only words with a length greater than YIgnore common terms like "and, is, the, etc"Feel free to strip punctuation prior to processing (ie. "John's" becomes "John")Return r...Read more

code golf - The Skyline Problem‍​​

I just came across this little problem on UVA's Online Judge and thought, that it may be a good candidate for a little code-golf.The problem:You are to design a program to assist an architect in drawing the skyline of a city given the locations of the buildings in the city. To make the problem tractable, all buildings are rectangular in shape and they share a common bottom (the city they are built in is very flat). The city is also viewed as two-dimensional. A building is specified by an ordered triple (Li, Hi, Ri) where Li and Ri are left and ...Read more

rosetta stone - 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 parsing library) of course. It's not so much about the email addresses but how short you can write the different string operations and constraints given below.The rules are the following (yes, I know, this is not RFC comp...Read more

rosetta stone - Palindrome Golf

The goal: Any language. The smallest function which will return whether a string is a palindrome. Here is mine in Python:R=lambda s:all(a==b for a,b in zip(s,reversed(s)))50 characters.The accepted answer will be the current smallest one - this will change as smaller ones are found. Please specify the language your code is in....Read more

esoteric languages - Code Golf: Fractran

The ChallengeWrite a program that acts as a Fractran interpreter. The shortest interpreter by character count, in any language, is the winner. Your program must take two inputs: The fractran program to be executed, and the input integer n. The program may be in any form that is convenient for your program - for example, a list of 2-tuples, or a flat list. The output must be a single integer, being the value of the register at the end of execution.FractranFractran is a trivial esoteric language invented by John Conway. A fractran program consist...Read more

Code golf: Reverse quine

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

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: RrX Example Input 2: nbHHkRbvnrXbvkn Example Output 2: RrX(the second example removes letters that occur three times; some solutions have failed to account for this)(This is based on my other question where ...Read more