Skip to content

Commit 1bfc61b

Browse files
committed
Add pig-latin to generator
1 parent 91627ac commit 1bfc61b

File tree

7 files changed

+286
-91
lines changed

7 files changed

+286
-91
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
exercise: PigLatin
2+
subs: translate
3+
plan: 23
4+
tests: |
5+
for my $case ( map { @{$_->{cases}} } @{$C_DATA->{cases}} ) {
6+
is translate($case->{input}{phrase}), $case->{expected}, $case->{description};
7+
}
8+
9+
example: |
10+
sub translate {
11+
my ($phrase) = @_;
12+
13+
return join(' ', map {
14+
if (/^(?:[aeiou]|[yx][^aeiou])/) { "${phrase}ay" }
15+
elsif (/^([^aeiou]*qu)(.+)/
16+
|| /^(y|[^aeiouy]+)(.+)/) { "$2${1}ay" }
17+
} split /\s+/, $phrase);
18+
19+
}
20+
21+
stub: |
22+
sub translate {
23+
my ($phrase) = @_;
24+
return undef;
25+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package PigLatin;
2-
3-
use v5.10.1;
42
use strict;
53
use warnings;
6-
no if $] >= 5.018, warnings => 'experimental';
4+
use Exporter 'import';
5+
our @EXPORT_OK = qw(translate);
76

87
sub translate {
9-
my $original = shift;
10-
my @pig_latin;
8+
my ($phrase) = @_;
119

12-
foreach my $orig ( split /\s+/ => $original ) {
13-
given ($orig) {
14-
when (/^[aeiou]/) { push @pig_latin => "${original}ay" }
15-
when (/^y[^aeiou]/) { push @pig_latin => "${original}ay" }
16-
when (/^x[^aeiou]/) { push @pig_latin => "${original}ay" }
17-
when (/^([^aeiou]*qu)(.+)/) { push @pig_latin => "$2$1" . "ay" }
18-
when (/^([^aeiou]+)(.+)/) { push @pig_latin => "$2$1" . "ay" }
19-
}
20-
}
10+
return join(
11+
' ',
12+
map {
13+
if (/^(?:[aeiou]|[yx][^aeiou])/) {"${phrase}ay"}
14+
elsif ( /^([^aeiou]*qu)(.+)/
15+
|| /^(y|[^aeiouy]+)(.+)/ )
16+
{
17+
"$2${1}ay";
18+
}
19+
} split /\s+/,
20+
$phrase
21+
);
2122

22-
return join " " => @pig_latin;
2323
}
2424

25-
__PACKAGE__;
25+
1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../pig-latin.t

exercises/pig-latin/.meta/solutions/piglatin.t

Lines changed: 0 additions & 1 deletion
This file was deleted.

exercises/pig-latin/PigLatin.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package PigLatin;
2+
use strict;
3+
use warnings;
4+
use Exporter 'import';
5+
our @EXPORT_OK = qw(translate);
6+
7+
sub translate {
8+
my ($phrase) = @_;
9+
return undef;
10+
}
11+
12+
1;

exercises/pig-latin/pig-latin.t

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#!/usr/bin/env perl
2+
use Test2::V0;
3+
use JSON::PP;
4+
5+
use FindBin qw($Bin);
6+
use lib $Bin, "$Bin/local/lib/perl5";
7+
8+
use PigLatin qw(translate);
9+
10+
my $C_DATA = do { local $/; decode_json(<DATA>); };
11+
plan 23;
12+
13+
imported_ok qw(translate) or bail_out;
14+
15+
for my $case ( map { @{ $_->{cases} } } @{ $C_DATA->{cases} } ) {
16+
is translate( $case->{input}{phrase} ), $case->{expected},
17+
$case->{description};
18+
}
19+
20+
__DATA__
21+
{
22+
"exercise": "pig-latin",
23+
"version": "1.2.0",
24+
"cases": [
25+
{
26+
"description": "ay is added to words that start with vowels",
27+
"cases": [
28+
{
29+
"description": "word beginning with a",
30+
"property": "translate",
31+
"input": {
32+
"phrase": "apple"
33+
},
34+
"expected": "appleay"
35+
},
36+
{
37+
"description": "word beginning with e",
38+
"property": "translate",
39+
"input": {
40+
"phrase": "ear"
41+
},
42+
"expected": "earay"
43+
},
44+
{
45+
"description": "word beginning with i",
46+
"property": "translate",
47+
"input": {
48+
"phrase": "igloo"
49+
},
50+
"expected": "iglooay"
51+
},
52+
{
53+
"description": "word beginning with o",
54+
"property": "translate",
55+
"input": {
56+
"phrase": "object"
57+
},
58+
"expected": "objectay"
59+
},
60+
{
61+
"description": "word beginning with u",
62+
"property": "translate",
63+
"input": {
64+
"phrase": "under"
65+
},
66+
"expected": "underay"
67+
},
68+
{
69+
"description": "word beginning with a vowel and followed by a qu",
70+
"property": "translate",
71+
"input": {
72+
"phrase": "equal"
73+
},
74+
"expected": "equalay"
75+
}
76+
]
77+
},
78+
{
79+
"description": "first letter and ay are moved to the end of words that start with consonants",
80+
"cases": [
81+
{
82+
"description": "word beginning with p",
83+
"property": "translate",
84+
"input": {
85+
"phrase": "pig"
86+
},
87+
"expected": "igpay"
88+
},
89+
{
90+
"description": "word beginning with k",
91+
"property": "translate",
92+
"input": {
93+
"phrase": "koala"
94+
},
95+
"expected": "oalakay"
96+
},
97+
{
98+
"description": "word beginning with x",
99+
"property": "translate",
100+
"input": {
101+
"phrase": "xenon"
102+
},
103+
"expected": "enonxay"
104+
},
105+
{
106+
"description": "word beginning with q without a following u",
107+
"property": "translate",
108+
"input": {
109+
"phrase": "qat"
110+
},
111+
"expected": "atqay"
112+
}
113+
]
114+
},
115+
{
116+
"description": "some letter clusters are treated like a single consonant",
117+
"cases": [
118+
{
119+
"description": "word beginning with ch",
120+
"property": "translate",
121+
"input": {
122+
"phrase": "chair"
123+
},
124+
"expected": "airchay"
125+
},
126+
{
127+
"description": "word beginning with qu",
128+
"property": "translate",
129+
"input": {
130+
"phrase": "queen"
131+
},
132+
"expected": "eenquay"
133+
},
134+
{
135+
"description": "word beginning with qu and a preceding consonant",
136+
"property": "translate",
137+
"input": {
138+
"phrase": "square"
139+
},
140+
"expected": "aresquay"
141+
},
142+
{
143+
"description": "word beginning with th",
144+
"property": "translate",
145+
"input": {
146+
"phrase": "therapy"
147+
},
148+
"expected": "erapythay"
149+
},
150+
{
151+
"description": "word beginning with thr",
152+
"property": "translate",
153+
"input": {
154+
"phrase": "thrush"
155+
},
156+
"expected": "ushthray"
157+
},
158+
{
159+
"description": "word beginning with sch",
160+
"property": "translate",
161+
"input": {
162+
"phrase": "school"
163+
},
164+
"expected": "oolschay"
165+
}
166+
]
167+
},
168+
{
169+
"description": "some letter clusters are treated like a single vowel",
170+
"cases": [
171+
{
172+
"description": "word beginning with yt",
173+
"property": "translate",
174+
"input": {
175+
"phrase": "yttria"
176+
},
177+
"expected": "yttriaay"
178+
},
179+
{
180+
"description": "word beginning with xr",
181+
"property": "translate",
182+
"input": {
183+
"phrase": "xray"
184+
},
185+
"expected": "xrayay"
186+
}
187+
]
188+
},
189+
{
190+
"description": "position of y in a word determines if it is a consonant or a vowel",
191+
"cases": [
192+
{
193+
"description": "y is treated like a consonant at the beginning of a word",
194+
"property": "translate",
195+
"input": {
196+
"phrase": "yellow"
197+
},
198+
"expected": "ellowyay"
199+
},
200+
{
201+
"description": "y is treated like a vowel at the end of a consonant cluster",
202+
"property": "translate",
203+
"input": {
204+
"phrase": "rhythm"
205+
},
206+
"expected": "ythmrhay"
207+
},
208+
{
209+
"description": "y as second letter in two letter word",
210+
"property": "translate",
211+
"input": {
212+
"phrase": "my"
213+
},
214+
"expected": "ymay"
215+
}
216+
]
217+
},
218+
{
219+
"description": "phrases are translated",
220+
"cases": [
221+
{
222+
"description": "a whole phrase",
223+
"property": "translate",
224+
"input": {
225+
"phrase": "quick fast run"
226+
},
227+
"expected": "ickquay astfay unray"
228+
}
229+
]
230+
}
231+
]
232+
}

exercises/pig-latin/piglatin.t

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)