See here
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
Disclaimer: I do realize this is easy, and I understand the content of the Coding Horror post ...
I run across this constantly, render some HTML if the browser is IE and some other if FFX (or mac, opera, etc).
This is what I use (pardon the missing underscores)
if(ereg("msie", strtolower($SERVER['HTTPUSERAGENT']))) { }
Anyone know a shorter way? (I use PHP but, just for the heck of it, i'd love to see non-PHP code as well who kno...
To commemorate the public launch of Stack Overflow, what's the shortest code to cause a stack overflow? Any language welcome.
ETA: Just to be clear on this question, seeing as I'm an occasional Scheme user: tail-call "recursion" is really iteration, and any solution which can be converted to an iterative solution relatively trivially by...
Perl golf is a programmers' game which involves solving a problem with the shortest perl program possible. (ie, the winner is the person who completes the round in the lowest number of (key)strokes).
...
What are your preferred size-coding (competitions for the smallest program that does something, or for the smallest source code) oriented sites?
There was a really nice python site, I can't remember where it is.
Hugi size-coding compo comes to my mind and it's also nice.
...
What's the shortest Perl one-liner that print out the first 9 powers of a hard-coded 2 digit decimal (say, for example, .37), each on its own line?
The output would look something like:
1
0.37
0.1369
[etc.]
Official Perl golf rules:
Smallest number of (key)strokes wins
Your stroke count includes the command line
...
Hello all,
The point of this question is to create the shortest not abusively slow Sudoku solver. This is defined as: don't recurse when there are spots on the board which can only possibly be one digit.
Here is the shortest I have so far in python:
r=range(81)
s=range(1,10)
def R(A):
bzt={}
for i in r:
if A[i]!=0: con...
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.
...
Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f, which prints the Fibonacci numbers.
Starting point: 25 characters in Haskell:
f=0:1:zipWith(+)f(tail f)
...
Since the palindrome code golf was a big hit, here is one that doesn't rely on built in functions.
What is the shortest (in characters) way to write a factorial function?
...
We have had an interesting competition once, where everyone would write their implementation of hello world program. One requirement was that is should be less than 20 bytes in compiled form. the winner would be the one whose version is the smallest...
What would be your solution? :)
Platform: 32bit, x86
OS: DOS, Win, GNU/Linux, *BSD
...
The code golf series seem to be fairly popular. I ran across some code that converts a number to its word representation. Some examples would be (powers of 2 for programming fun):
2 -> Two
1024 -> One Thousand Twenty Four
1048576 -> One Million Forty Eight Thousand Five Hundred Seventy Six
The algorithm my co-worker came up was alm...
Learning a little about T-SQL, and thought an interesting exercise would be to generate a Mandelbrot set with it.
Turns out someone already has (and recently, it appears). I'll let someone else post it as an answer, but I'm curious what optimizations can be made.
Alternately, what would you do to make the code more readable?
I'll sel...
What is the least amount of code you can write to create, sort (ascending), and print a list of 100 random positive integers? By least amount of code I mean characters contained in the entire source file, so get to minifying.
I'm interested in seeing the answers using any and all programming languages. Let's try to keep one answer per ...
I learned a lot about various languages last time I did one of these, so I figured I'd submit another. Community Wiki of course...
All programming languages are acceptable. The goal is to create the program in a language with the least amount of characters. New lines and indenting should be left in for clarity, but not counted. Try ...
Print all 12 verses of the popular holiday song.
By 12 verses I mean the repetition of each line as is sung in the song, ie
Verse One:
On the first day of Christmas my true love gave to me
a partridge in a pear tree.
Verse Two
On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree.
...
...
Given a number N, how can I print out a Christmas tree of height N using the least number of code characters? N is assumed constrained to a min val of 3, and a max val of 30 (bounds and error checking are not necessary). N is given as the one and only command line argument to your program or script.
All languages appreciated, if you s...
Write a program that take a single command line argument N and prints out the corresponding Roman Numeral.
Eg N = 2009 should print MMIX.
Let's say this should work for 0 < N < 3000.
(Had fun playing my first ever round of code golf with the Christmas edition, and thought this could fit for New Year. Googled to see if this has come up...
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 in...
Back in the ITAR era, there was a popular sig that performed Diffie-Hellman key exchange:
#!/usr/bin/perl -- -export-a-crypto-system-sig Diffie-Hellman-2-lines
($g,$e,$m)=@ARGV,$m||die"$0 gen exp mod\n";print`echo "16dio1[d2%Sa2/d0<X+d
*La1=z\U$m%0]SX$e"[$g*]\EszlXx+p|dc`
With a modern dc, this can be reduced quite a bit to:
dc -e '1...