July 2010 Archives

Perl Golf My solutions

sub i{($i,$j)=@_;$e=$i[$j];$i[$j]=$i[$i];$i[$i]=$e}$w=$i=0;@i=@ARGV;while($w<@i){$w++;$i=0if($i+2)>@i;if($i[$i]>$i[$i+1]){i($i,$i+1);$w=0}$i++}print@i
sub i{

(150 chars)
Looping through the array and swapping elements until the lowest is in the first position. Then move to the second element and loop again. O(n^2), it works, but is confusing to look at.

sub i{($i,$j)=@_;$t=$i[$j];$i[$j]=$i[$i];$i[$i]=$t}@i=@ARGV;for($i=0;$i<@i;$i++){for($j=$i;$j<@i;$j++){i($i,$j)if($i[$i]>$i[$j])>0}}print@i
sub i{

(139 chars)
I used two nested loops that Ran in O(n^2) time to swap items around as necessary. fairly straight forward

sub A{$R=shift;@A=@_;return$R if!@A;my$A=shift@A;return($R,$A,@A)if$R<$A;return($A,A($R,@A))}foreach$A(@ARGV){@A=A($A,@A)}print@A
sub A{
  return$R if!@A;

(129 chars)
Here I used an insertion sort idea with recursion. This one was probably the most fun to write, and runs in O(n^2) time. It could be modified to run in O(n log(n)) time without too much hassle

#slow but works:
foreach$A(@ARGV){$R{$A}++;$V=$A if$A<$V}while($G<@ARGV){for($A=0;$A<$R{$V};$A++){print$V;$G++}$V++}
#slow but works
  $V=$A if$A<$V
(99 chars)
This is limited to integers, and will only work when the numbers are within a 10^10 range from min to max.
I loop through the array looking for the smallest integer, and increment a hash element for each item.
Then I start at my minimum and loop through each index, if the hash >0 I print the number that many times, and then I add one to my minimum.

I not sure what to say for O notation here. The runtime of this algorithm depends more on the range of your numbers than the number of numbers. In fact. for the *right datasets* this would run in O(n), but for others, it would do much worse than O(n^2).

Example of the first: a million numbers, all between 1 and 100, would sort in the lenght of time it took to loop through the numbers twice.
Example of the second: a hundred numbers, ranging from -100 trillion to 100 trillion. It would be faster to sort these numbers by hand.



(94 characters) My shortest so far.O(n^2)for each number, compare it to each number and count the number that are smaller than it. After each number, you add the position to the result array.
This runs in O(n^2) time but is clever in that you build the result array as you do your double loop the first time around. This makes for less typing.

The only problem with this approach is that you need to consider duplicates in your dataset when printing. If your dataset is entirely unique, you could get away with 71 characters here:

Perl Golf Part 2.1

It seems that I missed a crucial part to the rules for my first round of Perl golf:

The challenge is to sort inputs in @ARGV without using Perl's "sort", in as few characters as possible.

To those who put brain cycles into this, I am sorry, but really the shortest solution without this rule is probably just:

print sort@ARGV

So here's to another try at this.

Additionally, to anyone who wants to say this is produces bad code: I KNOW! The point is to exercise the noggin, and think of new ways to solve problems using our favorite language.

Perl Golf Part 2

Before submitting, please see my correction above

Earlier this week I posted about perl golf at my work: Well my deadline has passed so I will pass this onto the rest of you. If you want to put your code in the comments below I will judge them.

The goal of course is to accomplish this using the fewest characters possible in your source code.

The rules:

*) The program must compile on perl 5.8.5 (to accommodate the test env)
*) The program must accept an arbitrary number of command-line arguments, which will all be numbers that your program should sort
*) The program must produce a list of the sorted inputs, sorted from lowest to highest
*) Any program that crashes (itself or the computer) loses -- even if it produces the right output
*) After 3 minutes of execution the running of the program will be terminated. (I will consider load on the host computer for this requirement).
*) No other executables may be used (no system calls or backticks, etc)
*) You may NOT use any additional Perl modules
*) The program exit code doesn't matter
*) You should not print anything to STDERR, only STDOUT
*) Your program should not be dependent on filename (it will be run from inside a file)
*) Team entries can be submitted. Please list all members of the team in the e-mail containing the code.
*) I will post my solutions on the 30th. Submissions accepted untill 8:00 AM CDT July 30th
*) I will post the results of the competition on the August 8th time permitting.
*) The shortest solution in bytes will win, so feel free to strip out the whitespace.
*) The tiebreaker will be the number of distinct characters used. For example: if 2 programs clock in at 70 characters, but one uses 12 distinct characters and the other 15, the one using 12 distinct characters will win.

perl golf

At my new job I have my first opportunity to play perl golf. Its an interesting concept and I think my solutions are clever, but the shortest is just about 100 chars. I am sure that someone else will beat me, but I don't think I can shorten it too much more.

I will post the challenge on my next post and my solutions following that. I will not post the challenge before my deadline has passed, so that nobody can accuse me of cheating.

Reporting webpages

I will soon be tasked with re-vamping a reporting page where users will be able to build their own reports to pull data from their database. My question to you is this: how would _you_ _actually_ build reports dynamically for users?

I want to know how YOU would actually do it, because I have Damien's book, and I have been to MST's talks. I know how THEY would do it. I want to know how other 'in the trench' programmers would do it.

The interface is static, where they can select checkboxes to bring in more data, fill in date range fields. The selection of data is actually pretty mundane, except that a checkbox means that I want all rows of type 'X' where 'X' actually has a number of conditions attached. In the end it isn't that difficult of a concept, except it isn't trivial enough for simple DBIx::Class abstractions.

Weekend/free time

I have asked this before but I like to see the answers as they may change over time.

What programming do you do in your free time, when you are not programming for $work, or $money?

I find that most of free time programs revolve around making my entertainment more efficient. Thus far, it usually has been focused around playing travian. I built a webapp that uses the provided map.sql file to find and track players that I take resources from. I have also twiddled with using LWP to make a remote control for VLC, not all that different in concept and purpose from Apple's FrontRow.

So, what do you program when its just for you? Does it use Perl, or do you use your freetime to dabble in other languages

About this Archive

This page is an archive of entries from July 2010 listed from newest to oldest.

June 2010 is the previous archive.

August 2010 is the next archive.

Find recent content on the main index or look in the archives to find all content.