Skip to content

Instantly share code, notes, and snippets.

@note103
Last active December 2, 2016 15:13
Show Gist options
  • Save note103/e314dfe4b506032674fb5ae3aaedb299 to your computer and use it in GitHub Desktop.
Save note103/e314dfe4b506032674fb5ae3aaedb299 to your computer and use it in GitHub Desktop.
Perl入学式 #3 復習問題
#!/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