2005-11-09

My contribution to pop culture

UHF is one of my favorite movies. I own it, you should too. I found a gem on the DVD today and I put it on the YTMND (short for "You're The Man Now, Dog!") site.

Here's the link: bethere.ytmnd.com
Make a new fad, vote for me.


2005-11-07

Permutations and Combinations

Almost a year ago, I posted an interesting problem that I wanted to solve with permutations (see Math Goodness.) A few months ago, the college I work for was hiring a new programmer and part of the hiring process involved a web-based test (to measure problem-solving skills, database knowledge, etc.) I was asked to write the test (as well as the test-taking application) and I included my permutation question as extra credit.

The question was:
a.) Write a program that will calculate the solution to the following problem:
Given 5 numbers, use basic math in various ways to match the target number.
[?] +-*/ [?] +-*/ [?] +-*/ [?] +-*/ [?] = [target]
*** Example **********************************************
*   Target:         12                                   *
*   Numbers:        3, 4, 6, 9, 10                       *
*   Solution:       [6] * [9] / [3] - [10] + [4] = [12]  *
**********************************************************
b.) Given a target of 5 and a list of numbers [2, 4, 6, 8, 10], what is a solution?

The other programmers I work with thought I should remove it from the test because it was too hard. I felt I should show them that it wasn't too difficult and so I finally got around to solving the problem (using Perl, for simplicity.)

Here is my source code (part a) and answer (part b)... enjoy:
a.)
#!/usr/bin/perl

sub combinations {
    my @ret;
    foreach $a (split(//, shift)) {
        push @ret, $a . $_ foreach (@_ ? combinations(@_) : "");
    }
    @ret;
}

sub permutations {
    my @ret;
    my @chars = split(//, shift);

    for(my $i = 0; $i < @chars; $i++) {
        my($char) = splice(@chars, $i, 1);
        push @ret, $char . $_ foreach @chars ? permutations(join("", @chars)) : ("");
        splice(@chars, $i, 0, $char);
    }
    @ret;
}

$numSolutions = 0;
$target = 5;
@nums = qw/2 4 6 8 10/;
@perms = permutations("01234");
@ops = combinations("*/+-", "*/+-", "*/+-", "*/+-");

foreach $perm (@perms){
 foreach $op (@ops){
  @pToken = split(//, $perm);
  @oToken = split(//, $op);
  $exp = $nums[$pToken[0]] . $oToken[0] . $nums[$pToken[1]] . $oToken[1] .
    $nums[$pToken[2]] . $oToken[2] . $nums[$pToken[3]] . $oToken[3] .
    $nums[$pToken[4]];
  if(eval($exp) == $target){
   print "$exp = $target, ";
   $numSolutions++;
  }
 }
}
print "$numSolutions solution(s) found...";

b.)
2/4*6-8+10 = 5, 2/4*6+10-8 = 5, 2*4/8-6+10 = 5, 2*4/8+10-6 = 5, 2*6/4-8+10 = 5, 2*6/4+10-8 = 5, 2/8*4-6+10 = 5, 2/8*4+10-6 = 5, 4*2/8-6+10 = 5, 4*2/8+10-6 = 5, 4+6/2+8-10 = 5, 4+6/2-10+8 = 5, 4/8*2-6+10 = 5, 4/8*2+10-6 = 5, 4+8+6/2-10 = 5, 4+8-10+6/2 = 5, 4-10+6/2+8 = 5, 4-10+8+6/2 = 5, 6*2/4-8+10 = 5, 6/2+4+8-10 = 5, 6*2/4+10-8 = 5, 6/2+4-10+8 = 5, 6/2+8+4-10 = 5, 6/2+8-10+4 = 5, 6/2-10+4+8 = 5, 6/2-10+8+4 = 5, 6/4*2-8+10 = 5, 6/4*2+10-8 = 5, 6-4+8-10/2 = 5, 6/4*10-2-8 = 5, 6-4-10/2+8 = 5, 6/4*10-8-2 = 5, 6+8-4-10/2 = 5, 6+8-10/2-4 = 5, 6-10/2-4+8 = 5, 6-10/2+8-4 = 5, 6*10/4-2-8 = 5, 6*10/4-8-2 = 5, 8/2/4-6+10 = 5, 8/2/4+10-6 = 5, 8/4/2-6+10 = 5, 8/4/2+10-6 = 5, 8+4+6/2-10 = 5, 8-4+6-10/2 = 5, 8-4-10/2+6 = 5, 8+4-10+6/2 = 5, 8+6/2+4-10 = 5, 8+6/2-10+4 = 5, 8+6-4-10/2 = 5, 8+6-10/2-4 = 5, 8-10/2-4+6 = 5, 8-10/2+6-4 = 5, 8-10+4+6/2 = 5, 8-10+6/2+4 = 5, 10+2/4*6-8 = 5, 10-2-4*6/8 = 5, 10+2*4/8-6 = 5, 10-2-4/8*6 = 5, 10+2*6/4-8 = 5, 10-2-6*4/8 = 5, 10-2-6/8*4 = 5, 10+2/8*4-6 = 5, 10+4*2/8-6 = 5, 10/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