Friday, February 19, 2016

Perl problem of the ziprecuiter while submitting the application

Recently I had chance to apply for the zip reciter (USA base startup.)
While application you are asked to resolve a problem in perl or Phython.


Here the description of the problem


=head
#problem
#You have a json data
[
{
name => 'apple',
basket => [1,2,3]
},
{
name => 'pineapple',
basket => [5 , 6 , 7]
}
]
Get output like
Output
fruit_name no_basket fruits_in_basket
-----------------------------
apple 3 6
pineapple 3 18
=cutt
$json_arr = [
{
name => 'apple',
basket => [ 1 ,2 3]
},
{
name => 'pineapple',
basket => [ 8 ,2 3]
}
];
print "fuuit_name basket_count fruit_count";
print "----------------------\n";
foreach my $hash_ref (@$json_arr) {
my $fruit_name = $hash_ref->{'name'};
my $basket_count = scalar(@{ $ref_hash->{'basket'} });
my $fruit_sum = 0;
foreach (@{ $ref_hash->{'basket'} }) {
$fruit_sum += $_;
}
print join(" ",$fruit_name,$basket_count,$fruit_sum,"\n");
}
view raw zip.pl hosted with ❤ by GitHub

Though when I submitted the application , I did not thought and made used of JSON module.
But I think , It can be solved too without module , as it an ref to array.