Skip to content

Instantly share code, notes, and snippets.

@atiw003
Created January 26, 2012 02:38
Show Gist options
  • Select an option

  • Save atiw003/1680608 to your computer and use it in GitHub Desktop.

Select an option

Save atiw003/1680608 to your computer and use it in GitHub Desktop.
Silly function
#
# Instructions
#
# 1. Figure out what this function does
# 2. Write a unit test for it
# 3. Refactor for readability and efficiency
#
# NOTE: Do regular commits that show agile style iterations through the problem.
# If you prefer a different language, you can provide your solution in PHP,
# Python or Javascript too.
#
package SillyFunction;
sub group_products {
my $products = shift;
my %brand_type = ();
my $grouped_products = [];
foreach (@{$products})
{
$brand_type{$_->{brand}} ||= {};
$brand_type{$_->{brand}}->{$_->{type}} = 1;
}
foreach (sort keys %brand_type)
{
my $brand = $_;
foreach (sort keys %{$brand_type{$brand}}) {
push(@{$grouped_products}, { brand => $brand, type => $_});
}
}
$grouped_products;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment