Perl, 66 61 55 54 bytes
includes +1 for -p
/[ao]+/;$\="s go M".$&=~y/a/e/r.o x($+[0]-1).(w)[/w/]
Input is expected to conform to /^C[ao]+[tw]$/
(no trailing newline!)
Usage: /bin/echo -n Caaat | perl -p 55.pl
Breakdown
/[ao]+/;
$\= "s go M" # assign to $OUTPUT_RECORD_SEPARATOR, normally `\n`. Saves 1 vs `$_.=`
. $& # the matched vowels
=~ y/a/e/r # translate `a` to `e`; `/r` returns a copy.
. o x($+[0]-1) # append 'o', repeated. $+[0] is string position of last match end.
. (w)[/w/] # returns 'w' if there is no /w/ in the input, nothing if there is.
###Previous version:
Previous version:
@l=/[ao]/g;$x=$&[email protected] x@l;$y=$x=~y/a/e/?w:'';s/$/s go M$x$y/
Commented:
@l = /[ao]/g; # captures $& as vowel and @l as list of vowels
$x = $& x @l .o x @l; # construct the output vowels
$y = $x =~ y/a/e/ ? w : ''; # correct vowel string for cats (aaaooo->eeeooo); $y='w' if cat.
s/$/s go M$x$y/ # construct the desired output.
###Example: Caaat
Example: Caaat
- Capture
$&
asa
and@l
as(a,a,a)
. - Set
$x
to three timesa
followed by 3 timeso
:aaaooo
. - Translate all
a
in$x
toe
:eeeooo
. The number of replacements (either 0 or positive) serves as a cat-detector: set$y
tow
if so. - Change the input by appending
s go M
,eeeooo
andw
.
- update 61: Save 5 bytes by using list instead of string
- update 55: save 6 bytes by inlining, assigning
$\
rather thans/$/
, and requiring no trailing newline in input. - update 54: save 1 byte by eliminating @l.