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
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.
While application you are asked to resolve a problem in perl or Phython.
Here the description of the problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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"); | |
} |
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.