A Dozen Lines Of Code
January 24, 2012
Today’s task will require your imagination and creativity.
A high-school programming teacher recently asked for examples of short programs with a high “cool” factor, the idea being to get his students interested in programming computers. I’m not sure the suggestions would work; today’s high-school students have been surrounded by computers their entire lives, and it takes a lot to make them think a program is cool. Being from a different generation, I can remember when I thought it was cool that a program properly skipped over the perforation on a stack of green-bar paper — many programs didn’t!
Your task is to write a cool program in a dozen lines of code. You can define cool in any way that you wish. Try not to abuse the definition of “line of code,” at least not too badly; to be concrete, we will say that your solution must not exceed 12 lines, and each line must not exceed 80 characters including white space. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
In ruby, less than a dozen lines you can write a program to display the frequency all the words in a file (“word” defined loosely here as C/C++ style identifier, except it also accepts leading digits as “words” so it should do a reasonable job for text (i.e. not programming language) documents)
Analyzing itself:
Here’s my solution:
;; A simple Boss Key app. My contribution to the dozen-line program contest.
NeedToHide = .*(Mozilla|Explorer|Chrome).*
WantToShow = .*emacs.*
#b::
SetTitleMatchMode, RegEx
WinGet, id, list, %NeedToHide%
Loop, %id% {
this_id := id%A_Index%
WinHide, ahk_id %this_id%
}
WinActivate, %WantToShow%
Return,
And some explanation behind it: http://benjisimon.blogspot.com/2012/01/my-dozen-lines-of-code.html
[…] today’s Programming Praxis exercise, our goal is to make any program we want, as long as it’s cool […]
My Haskell program, which is an 11-line implementation of Conway’s Game of Life (see http://bonsaicode.wordpress.com/2012/01/24/programming-praxis-a-dozen-lines-of-code/ for a version with comments):
Note that I am not counting the import statements as lines of code.
For the above, substitute od.path for os.path.
To use pinc.py as a quine, try: [tt]echo “(> pinc.py >)” | python pinc.py[/tt]
def add(x,y): #assumes both numbers are postive.
while y > 0:
x,y = x^y, (x&y)<0:
if y&1 == 1:
total = add(total,x)
x = x<>1
return x
{/sourcecode]
hmm the source code optino didnt work very well. heres the pastebin option
code
This computes a Brent-Salamin approximation to pi with k digits base b.
Edited from the Gambit examples.
> (pi-brent-salamin 10 100)
31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170374
I’ve modified one of my old snippets… (see my website for the original)
Okay, I admit that the lines are a bit overfull… but the result quite worths it,
see an image here.
A python program that generates high frequencies can be used as a dog whistle and also to annoy your friends.
from winsound import *;import random
while True:Beep(random.randint(300,1000),random.randint(500,1000))
Perl iterative permutation algorithm (Fischer-Krause) to permute the characters of a string.
11 lines (although slightly abused with the 1-line while loops), max width of 67 characters:
Driver/demo program:
Output:
The ‘Divisors’ Problem (https://programmingpraxis.com/2012/02/14/divisors/) under 12 lines :P
How can we not have here a linearithmic (more or less) primes generating code in 8 lines of Haskell: :)
Test it at http://codepad.org/Z62VCphw.
Forgot to mention that the code itself is a _genuine one-liner_; all the rest are general auxiliary utilities. :)
For instance, the intersection of two ordered increasing lists – a function not used in `primes` – is