mar
1
8
Code Golf
Programmers are notorious for solving puzzles and games. Essentially code golfing was born from that mind set. The goal of golfing is to get the lowest score. Code golfing as defined from the Code Golf home is:
Based on the original perl golf, Code Golf allows you to show off your code-fu by trying to solve coding problems using the least number of keystrokes.
Essentially the goal is to make your program as small as possible. The fewest bytes, the fewest characters, the smallest it can be. I am rather new to the sport but from what I have gathered it really kicking into action when scripting languages like Perl became popular. The original golf was Perl golf, as mentioned in the quote. Perl is a really cool programming language. Its powerful and robust but most importantly its fun to program. Its syntax is simple and its special variables and dynamic typing make it a joy to use. Interested in learning? However, recently languages like Ruby, PHP, and Python have shown they are just as effective.
How about an example? Lets start with something really basic. Lets add all of the numbers from 1 to N, where N is the number the user provides on the command line. A traditional approach might look like:
#!/usr/bin/perl
use warnings;
use strict;
my $N = $ARGV[0]; # The given upper limit
my $sum = 0; # To hold the summation
# Perform the summation
for ($i = 1; $i < $N; $i++) {
$sum += $i;
}
# Print the result
print $sum, "\n";
Here was my Perl solution weighing in at 28 bytes complete with the terminating newline:
map$i+=$_,1..pop;print$i,$/
Example output:
$ perl sumToN.pl 15 120
It is completely possible that there might even be a smaller solution using a different approach, but you can see what is happening. The code is massively condensed to the point where there is nothing extraneous, nothing but pure code.
For those of you who are wondering why would anyone ever want to do this... you are forgetting that this is merely a game. Its a challenge, a puzzle to occupy your time and entertain your brain. As with all games you get better with practice, you learn new tactics and tricks, my original solution weighed in at 29 bytes using a for loop until I experimented with the map function to cut two characters. Golfing tests your knowledge of the language and helps you learn the secrets at the same time. When is a space or a semicolon necessary? Do I need to create an extra variable? It may not look like it from above, but you will become a better programmer.
More challenges include:
- Converting Roman Decimals to Decimal Numbers
- Drawing an Egg Timer of size N (Very Cool)
- Prime factorizing numbers
The list of games are endless, and the solutions equally immense. So what are you waiting for, take a swing at this challenge inspired by Worse Than Failure (the DailyWTF). Generate a random password of N length of alphanumeric characters (lowercase, uppercase, and numbers). Simply state your language of choice. Remember that the input length N, is provided on the command line as if you were to run the command from your terminal. My solutions weighed in at 59 bytes but I have a very cool 61 byte solution. Here is some example usage for you to work off of:
$ perl genpassword.pl 35 Pm0vfue12fWB45Ctr4Zc2zXNt5wkREGHQan


Joseph Pecoraro on November 28, 2007 at 11:56 am #
Here were my solutions:
The down and dirty. Choose a random character from an array of numbers and upper and lower letters. Do this however many times it is necessary:
$p.=(0..9,'A'..'Z','a'..'z')[62*rand]for 1..pop;print$p,$/This is what I consider my neat solution. It generates a random ASCII character out of the first 122 ASCII characters (z is 122). If that character matches the regular expression (looking for a number or a letter) then we add it to our string. If it was a bad guess we reloop. This is not deterministic but its a really cool solution:
$_=chr 123*rand,/[\da-z]/i?$a.=$_:redo for 1..pop;print$a,$/