A response to the "wiz bang" question

| No Comments | No TrackBacks

(Ryan Byrd)[http://www.ryanbyrd.net/techramble/] blogged recently with a (programming interview question)[http://www.ryanbyrd.net/techramble/2009/03/03/programming-interview-question-of-the-day/] that I thought I’d take a stab at in Perl.

The question is as follows:

  • when passed in a number that is evenly divisible by 3, return “wiz”
  • when passed in a number that is evenly divisible by 5, return “bang”
  • when passed in a number that is evenly divisible by both 3 and 5, return “wiz bang”
  • otherwise, return the number passed in

My solution exploits Perl’s list type to store potential output as a queue of sorts.

sub function {
    my $num = shift;
    my @output = ();
    unless($num % 3) {    push @output, "wiz"; }
    unless($num % 5 ) {    push @output, "bang"; }
    if(@output) {   return join ' ', @output; }
    return $num; 
}

No TrackBacks

TrackBack URL: http://fozzolog.fozzilinymoo.org/mt-tb.cgi/1640

Leave a comment

About this Entry

This page contains a single entry by Doran L. Barton published on March 3, 2009 10:19 AM.

Perl Basics: Using DBI was the previous entry in this blog.

Learning Perl basics in the Fedora Classroom ... by me! is the next entry in this blog.

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