File tree Expand file tree Collapse file tree 4 files changed +577
-123
lines changed Expand file tree Collapse file tree 4 files changed +577
-123
lines changed Original file line number Diff line number Diff line change 1+ exercise : Allergies
2+ plan : 50
3+ subs : allergic_to list_allergies
4+ tests : |-
5+ for my $case (@test_cases) {
6+ if ( $case->{property} eq 'allergicTo' ) {
7+ is allergic_to( $case->{input} ), $case->{expected} ? T : DF,
8+ $case->{description};
9+ }
10+ elsif ( $case->{property} eq 'list' ) {
11+ is
12+ list_allergies( $case->{input}{score} ),
13+ bag {
14+ item $_ for @{ $case->{expected} };
15+ end;
16+ },
17+ $case->{description};
18+ }
19+ }
20+
21+ example : |-
22+ use constant ALLERGENS => {
23+ eggs => 1,
24+ peanuts => 2,
25+ shellfish => 4,
26+ strawberries => 8,
27+ tomatoes => 16,
28+ chocolate => 32,
29+ pollen => 64,
30+ cats => 128,
31+ };
32+
33+ sub allergic_to {
34+ my ($input) = @_;
35+ ALLERGENS->{ $input->{item} } & $input->{score};
36+ }
37+
38+ sub list_allergies {
39+ my ($score) = @_;
40+ return [ grep { allergic_to { item => $_, score => $score } }
41+ keys %{ +ALLERGENS } ];
42+ }
43+
44+ stub : |-
45+ sub allergic_to {
46+ my ($input) = @_;
47+ return undef;
48+ }
49+
50+ sub list_allergies {
51+ my ($score) = @_;
52+ return undef;
53+ }
Original file line number Diff line number Diff line change 11package Allergies ;
2-
32use strict;
43use warnings;
5-
6- use List::Util ' first ' ;
7-
8- my @allergens
9- = qw( eggs peanuts shellfish strawberries tomatoes chocolate pollen cats ) ;
10-
11- sub new {
12- my ( $class , $score ) = @_ ;
13- my $self = bless {} => $class ;
14- $self -> { score } = reverse sprintf " %08b " , $score ;
15-
16- return $self ;
17- }
4+ use Exporter qw< import > ;
5+ our @EXPORT_OK = qw< allergic_to list_allergies > ;
6+
7+ use constant ALLERGENS => {
8+ eggs => 1,
9+ peanuts => 2,
10+ shellfish => 4,
11+ strawberries => 8,
12+ tomatoes => 16,
13+ chocolate => 32,
14+ pollen => 64,
15+ cats => 128,
16+ };
1817
1918sub allergic_to {
20- my ( $self , $allergen ) = @_ ;
21-
22- my $index = first { $allergens [$_ ] eq $allergen } 0 .. $#allergens ;
23-
24- return substr $self -> {score }, $index , 1;
19+ my ($input ) = @_ ;
20+ ALLERGENS-> { $input -> {item } } & $input -> {score };
2521}
2622
27- sub list {
28- [ grep { $_ [0]-> allergic_to($_ ) } @allergens ]
23+ sub list_allergies {
24+ my ($score ) = @_ ;
25+ return [
26+ grep { allergic_to { item => $_ , score => $score } }
27+ keys %{ +ALLERGENS }
28+ ];
2929}
3030
31- __PACKAGE__ ;
32-
31+ 1;
Original file line number Diff line number Diff line change 1+ package Allergies ;
2+ use strict;
3+ use warnings;
4+ use Exporter qw< import> ;
5+ our @EXPORT_OK = qw< allergic_to list_allergies> ;
6+
7+ sub allergic_to {
8+ my ($input ) = @_ ;
9+ return undef ;
10+ }
11+
12+ sub list_allergies {
13+ my ($score ) = @_ ;
14+ return undef ;
15+ }
16+
17+ 1;
You can’t perform that action at this time.
0 commit comments