Skip to main content
Commonmark migration
Source Link

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 $& as a and @l as (a,a,a).
  • Set $x to three times a followed by 3 times o: aaaooo.
  • Translate all a in $x to e: eeeooo. The number of replacements (either 0 or positive) serves as a cat-detector: set $y to w if so.
  • Change the input by appending s go M, eeeooo and w.

  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.
  • update 54: save 1 byte by eliminating @l.

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:

@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

  • Capture $& as a and @l as (a,a,a).
  • Set $x to three times a followed by 3 times o: aaaooo.
  • Translate all a in $x to e: eeeooo. The number of replacements (either 0 or positive) serves as a cat-detector: set $y to w if so.
  • Change the input by appending s go M, eeeooo and w.

  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.
  • update 54: save 1 byte by eliminating @l.

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:

@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

  • Capture $& as a and @l as (a,a,a).
  • Set $x to three times a followed by 3 times o: aaaooo.
  • Translate all a in $x to e: eeeooo. The number of replacements (either 0 or positive) serves as a cat-detector: set $y to w if so.
  • Change the input by appending s go M, eeeooo and w.

  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.
  • update 54: save 1 byte by eliminating @l.
save 1 byte by using @+
Source Link
Kenney
  • 956
  • 4
  • 4

Perl, 66 61 5555 54 bytes

@l=/[ao][ao]+/g;$\="s;$\="s go M".($&x@l)=~y$&=~y/a/e/r.o x@lx($+[0]-1).(w)[/w/]
@l=/[ao][ao]+/g;        # $& = vowel, @l = list of vowels;
$\= "s go M"        # assign to $OUTPUT_RECORD_SEPARATOR, normally `\n`. Saves 1 vs `$_.=`
   . ( $& x @l )   # reconstruct      # the matched vowels
     =~ y/a/e/r     # translate `a` to `e`; `/r` returns a copy.
   . o x($+[0]-1) @l  # append 'o', repeated. $+[0] is #string appendposition 'o',of repeatedlast match end.
   . (w)[/w/]       # returns 'w' if there is no /w/ in the input, nothing if there is.
  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.
  • update 54: save 1 byte by eliminating @l.

Perl, 66 61 55 bytes

@l=/[ao]/g;$\="s go M".($&x@l)=~y/a/e/r.o x@l.(w)[/w/]
@l=/[ao]/g;        # $& = vowel, @l = list of vowels
$\= "s go M"       # assign to $OUTPUT_RECORD_SEPARATOR, normally `\n`. Saves 1 vs `$_.=`
   . ( $& x @l )   # reconstruct the matched vowels
     =~ y/a/e/r    # translate `a` to `e`; `/r` returns a copy.
   . o x @l        # append 'o', repeated
   . (w)[/w/]      # returns 'w' if there is no /w/ in the input, nothing if there is.
  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.

Perl, 66 61 55 54 bytes

/[ao]+/;$\="s go M".$&=~y/a/e/r.o x($+[0]-1).(w)[/w/]
/[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.
  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.
  • update 54: save 1 byte by eliminating @l.
save 6 bytes by refactoring
Source Link
Kenney
  • 956
  • 4
  • 4

Perl, 66 6161 55 bytes

includes +1 for -p

@l=/[ao]/g;$\="s go M".($&x@l)=~y/a/e/r.o x@l.(w)[/w/]

Input is expected to conform to /^C[ao]+[tw]$/ (no trailing newline!)
Usage: /bin/echo -n Caaat | perl -p 55.pl

Breakdown

@l=/[ao]/g;        # $& = vowel, @l = list of vowels
$\= "s go M"       # assign to $OUTPUT_RECORD_SEPARATOR, normally `\n`. Saves 1 vs `$_.=`
   . ( $& x @l )\1+   # reconstruct the matched vowels
     =~ y/;$x=$&a/e/r    # translate `a` to `e`; `/r` returns a copy.
   . o x length$&;$y=$x=~y@l        # append 'o', repeated
   . (w)[/w/]      # returns 'w' if there is no /w/ in the input, nothing if there is.

###Previous version:

@l=/[ao]/g;$x=$&[email protected] x@l;$y=$x=~y/a/e/?w:'';s/$/s go M$x$y/

Input is expected to conform to /C[ao]+[tw]\s*/.

###BreakdownCommented:

@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

  • Capture $& as a and @l as (a,a,a).
  • Set $x to three times a followed by 3 times o: aaaooo.
  • Translate all a in $x to e: eeeooo. The number of replacements (either 0 or positive) serves as a cat-detector: set $y to w if so.
  • Change the input by appending s go M, eeeooo and w.

  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.

Perl, 66 61 bytes

includes +1 for -p

/([ao])\1+/;$x=$&.o x length$&;$y=$x=~y/a/e/?w:'';s/$/s go M$x$y/

Input is expected to conform to /C[ao]+[tw]\s*/.

###Breakdown:

@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

  • Capture $& as a and @l as (a,a,a).
  • Set $x to three times a followed by 3 times o: aaaooo.
  • Translate all a in $x to e: eeeooo. The number of replacements (either 0 or positive) serves as a cat-detector: set $y to w if so.
  • Change the input by appending s go M, eeeooo and w.

  • update 61: Save 5 bytes by using list instead of string

Perl, 66 61 55 bytes

includes +1 for -p

@l=/[ao]/g;$\="s go M".($&x@l)=~y/a/e/r.o x@l.(w)[/w/]

Input is expected to conform to /^C[ao]+[tw]$/ (no trailing newline!)
Usage: /bin/echo -n Caaat | perl -p 55.pl

Breakdown

@l=/[ao]/g;        # $& = vowel, @l = list of vowels
$\= "s go M"       # assign to $OUTPUT_RECORD_SEPARATOR, normally `\n`. Saves 1 vs `$_.=`
   . ( $& x @l )   # reconstruct the matched vowels
     =~ y/a/e/r    # translate `a` to `e`; `/r` returns a copy.
   . o x @l        # append 'o', repeated
   . (w)[/w/]      # returns 'w' if there is no /w/ in the input, nothing if there is.

###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

  • Capture $& as a and @l as (a,a,a).
  • Set $x to three times a followed by 3 times o: aaaooo.
  • Translate all a in $x to e: eeeooo. The number of replacements (either 0 or positive) serves as a cat-detector: set $y to w if so.
  • Change the input by appending s go M, eeeooo and w.

  • update 61: Save 5 bytes by using list instead of string
  • update 55: save 6 bytes by inlining, assigning $\ rather than s/$/, and requiring no trailing newline in input.
save 5 bytes
Source Link
Kenney
  • 956
  • 4
  • 4
Loading
update: misread question, the input word should keep same number of vowels
Source Link
Kenney
  • 956
  • 4
  • 4
Loading
Source Link
Kenney
  • 956
  • 4
  • 4
Loading