Last active
December 2, 2016 15:13
-
-
Save note103/e314dfe4b506032674fb5ae3aaedb299 to your computer and use it in GitHub Desktop.
Perl入学式 #3 復習問題
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature 'say'; | |
# 1-1 | |
my $foo = {name => 'note103', favorite_foods => [qw( ramen rice apple )]}; | |
my $bar = {name => 'alice' , favorite_foods => [qw( ramen kimuchi orange apple )]}; | |
my $baz = {name => 'bob' , favorite_foods => [qw( ramen meet kimuchi orange banana )]}; | |
# 1-2 | |
my @member = ($foo, $bar, $baz); | |
# 1-3 | |
my %count; | |
for my $ref (@member) { | |
for (@{$ref->{favorite_foods}}) { | |
$count{$_}++; | |
} | |
} | |
my @votes = reverse sort {$a <=> $b} values %count; | |
my $rank = 1; | |
for my $v (@votes) { | |
for my $fav_food (keys %count) { | |
if ($count{$fav_food} == $v) { | |
say "$rank. $fav_food\t[$v票]"; | |
delete $count{$fav_food}; | |
$rank++; | |
} | |
} | |
} | |
__END__ | |
1. ramen [3票] | |
2. orange [2票] | |
3. apple [2票] | |
4. kimuchi [2票] | |
5. meet [1票] | |
6. banana [1票] | |
7. rice [1票] | |
※同票の食べ物は順不同(実行するごとに変化) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/perl-entrance-org/workshop-2016/blob/master/3rd/practice.md#1-votepl