Snippets

sironekotoro Perl入学式 2018 第3回 復習問題 score.pl 2-3

Created by sironekotoro
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

# Perl入学式 2018 第3回 復習問題 score.pl
# https://github.com/perl-entrance-org/workshop-2018/blob/master/3rd/practice.md

# 2-3. 五段階評価
# 下記を参考に、各人物の言語ごとの成績を5段階で出力して下さい。
# bob
#   perl   : **
#   python :
#   ruby   : *
#   php    : *
#   binary : **

my $alice = {
    name    => 'Alice',
    country => 'England',
    perl    => 60,
    python  => 80,
    ruby    => 80,
    php     => 50,
    binary  => 30,
};
my $bob = {
    name    => 'Bob',
    country => 'America',
    perl    => 40,
    python  => 10,
    ruby    => 20,
    php     => 30,
    binary  => 50,
};
my $carol = {
    name    => 'Carol',
    country => 'England',
    perl    => 100,
    python  => 70,
    ruby    => 80,
    php     => 50,
    binary  => 50,
};
my $dave = {
    name    => 'Dave',
    country => 'Canada',
    perl    => 60,
    python  => 11,
    ruby    => 1,
    php     => 100,
    binary  => 100,
};
my $ellen = {
    name    => 'Ellen',
    country => 'America',
    perl    => 1,
    python  => 15,
    ruby    => 0.5,
    php     => 60,
    binary  => 0.01,
};

# それぞれの人物を表したハッシュリファレンスを集めた配列を作成
my @people = ( $alice, $bob, $carol, $dave, $ellen );

for my $man (@people) {
    print $man->{name}, "\n";

# 出力例は固定、かつアルファベット順ではなく、sprintfは
# Perl入学式の範囲にない・・・ような気がするので、ベタ書きする

# * の表示は繰り返し演算子を使う
# http://d.hatena.ne.jp/perlcodesample/20080221/1203613344

    print '  perl   :', '*' x ( $man->{perl} / 20 ),   "\n";
    print '  python :', '*' x ( $man->{python} / 20 ), "\n";
    print '  ruby   :', '*' x ( $man->{ruby} / 20 ),   "\n";
    print '  php    :', '*' x ( $man->{php} / 20 ),    "\n";
    print '  binary :', '*' x ( $man->{binary} / 20 ), "\n";

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.