20
\$\begingroup\$

Língua do Pê, or P Language, is a language game spoken in Brazil and Portugal with Portuguese. It is also known in other languages, such as Dutch and Afrikaans. (Wikipedia)

There are some dialects in this language game. The different languages the game is played with even have their own unique dialects. Some people are fluent in speaking P Language and the best can even translate any text to their preferred dialect on the spot!

P Language

In this challenge, we will use the Double Talk dialect.

To translate text into P Language, any sequence of vowels in the text is appended with a single p character followed by a copy of the sequence of vowels.

Challenge

Write a function or program that accepts a string as input and outputs its translation in P Language.

  • The input consists only of printable ASCII characters.
  • The output consists only of the translated input and optionally a trailing newline.
  • Vowels are any of the following characters aeiouyAEIOUY.
  • A sequence of vowels is delimited by any other character. The string "Aa aa-aa" has three vowel sequences.
  • Leading and trailing whitespace may optionally be omitted from the translated output string.

Examples

""                              =>   ""
"Lingua do Pe"                  =>   "Lipinguapua dopo Pepe"
"Hello world!"                  =>   "Hepellopo woporld!"
"Aa aa-aa"                      =>   "AapAa aapaa-aapaa"
"This should be easy, right?"   =>   "Thipis shoupould bepe eapeasypy, ripight?"
"WHAT ABOUT CAPS?"              =>   "WHApAT ApABOUpOUT CApAPS?"
"   Hi "                        =>   "   Hipi " or "Hipi"

The double quotes character " is used to delimit the input and output strings in the examples but obviously this character may also appear in any valid input string.

New contributor
Maarten Bamelis is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • 2
    \$\begingroup\$ I'm not sure if anyone has welcomed you to the stack yet, so: welcome to Code Golf Stack Exchange! Nicely specified first challenge. :-) \$\endgroup\$ – Giuseppe Aug 14 at 15:05
  • \$\begingroup\$ @Giuseppe Thanks for the kind words! \$\endgroup\$ – Maarten Bamelis Aug 16 at 8:21
  • \$\begingroup\$ Also in Spanish es.wikipedia.org/wiki/Jerigonza \$\endgroup\$ – leonbloy Aug 16 at 15:12

12 Answers 12

9
\$\begingroup\$

JavaScript (ES6), 35 bytes

s=>s.replace(/[aeiouy]+/gi,'$&p$&')

Try it online!

Where the special replacement pattern $& means matched substring.

\$\endgroup\$
  • 8
    \$\begingroup\$ I did not know about $&. All these years, I've been wrapping the whole regex in a capture group. Who said code golf is impractical?! \$\endgroup\$ – recursive Aug 13 at 22:50
  • 1
    \$\begingroup\$ Is $& the more common way? In Java it's $0 a.f.a.i.k., and Retina allows both. Didn't knew $& came from JavaScript. Or is it a .NET flavored regex, and JavaScript also uses it? \$\endgroup\$ – Kevin Cruijssen Aug 14 at 6:37
9
\$\begingroup\$

Sed, 30, 25, 21, 19 Bytes

-5 Bytes Thanks to Arnauld!
-4 Bytes Thanks to Shaggy!
-2 Bytes Thanks to Leo Tenenbaum!

s/[aeiouy]\+/&p&/gi

Try it Online!

\$\endgroup\$
7
\$\begingroup\$

Japt, 10 bytes

r"%y+"_+ip

Try it

r"%y+"_+ip     :Implicit input of string
r              :Replace
 "%y+"         :RegEx /[aeiouy]+/gi
      _        :Pass each match through a function
       +       :  Append a copy of the match
        ip     :  Prepended with "p"
\$\endgroup\$
  • \$\begingroup\$ Darn it, 2 minutes too late! I had the exact same answer, except it used Japt 2.0 short regexes "%v" -> \v \$\endgroup\$ – Embodiment of Ignorance Aug 13 at 21:39
  • \$\begingroup\$ @EmbodimentofIgnorance, you'd need \y instead of \v. \$\endgroup\$ – Shaggy Aug 13 at 22:44
6
\$\begingroup\$

Java 8, 40 bytes

s->s.replaceAll("(?i)[aeiouy]+","$0p$0")

Try it online.

Explanation:

s->                              // Method with String as both parameter and return-type
  s.replaceAll("(?i)[aeiouy]+",  //  Replace the regex matches
               "$0p$0")          //  With this replacement

Regex explanation:

(?i)[aeiouy]+                    // MATCH:
(?i)                             //  Enable case insensitivity
            +                    //  Match one or more
    [aeiouy]                     //  Adjacent vowel characters

$0p$0                            // REPLACEMENT:
$0                               //  The entire match (the vowel 'sequence')
  p                              //  Appended with a literal "p"
   $0                            //  Appended with the entire match again
\$\endgroup\$
4
\$\begingroup\$

Perl 5 -p, 20 bytes

s/[aeiouy]+/$&p$&/gi

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 3, 55 bytes

lambda s:re.sub('([aeiouy]+)',r'\1p\1',s,0,2)
import re

Try it online!


Sans regex:

Python 3, 101 bytes

def f(s,q=''):i=s[:1];t=i in{*'aeiouyAEIOUY'};return(q+(q!='')*'p'+q+i)*0**t+(s and f(s[1:],(q+i)*t))

Try it online!

Python 3.8 (pre-release): 99 bytes

Explanation

Recursive function, accepting a string s and an optional argument q. If the first character of s (i) is a vowel, it is stored in the queue q. If not, a string is returned which is composed of q, the letter 'p', q again, the character i and the result of the recursive function with the first character of the string stripped off. Recursion stops when the function encounters an empty string s.

\$\endgroup\$
  • 2
    \$\begingroup\$ Love the solution without regex! \$\endgroup\$ – Maarten Bamelis Aug 14 at 10:34
3
\$\begingroup\$

05AB1E, 22 20 bytes

.γžÁyå}vyžÁyнåi'py}J

05AB1E doesn't have any regexes unfortunately.
I don't really like the duplicated žÁyнå, but I'm currently a bit too busy to look for alternatives..

-2 bytes thanks to @Grimy for showing me a constant I didn't even knew existed (and was missing from the Wiki page.. >.>)

Try it online or verify all test cases.

Explanation:

.γ               # Group the characters in the (implicit) input-string by:
  žÁ             #  Push vowels builtin: "aeiouyAEIOUY"
    yå           #  And check if the current character is in this string
 }v              # After grouping: loop over each group `y`:
   y             #  Push group `y`
    žÁyнåi   }   #  If the first character of the group is a vowel:
          'p    '#   Push a "p"
            y    #   And push group `y` again
              J  #  Join everything on the stack together to a single string
                 # (after the loop, implicitly output the result)
\$\endgroup\$
  • \$\begingroup\$ žÁ instead of žO lets you drop the l. \$\endgroup\$ – Grimy Aug 14 at 12:09
  • \$\begingroup\$ @Grimy Maybe I should start using the info.txt file instead of the Wiki page.. -_-' \$\endgroup\$ – Kevin Cruijssen Aug 14 at 12:29
  • \$\begingroup\$ They're both missing lots of commands, though not the same ones (hopefully. Sometimes I wonder if there are secret commands missing from both). \$\endgroup\$ – Grimy Aug 14 at 12:31
  • 1
    \$\begingroup\$ @Grimy Yeah, maybe we should use the source code instead of the info.txt or Wiki pages. ;) \$\endgroup\$ – Kevin Cruijssen Aug 14 at 12:32
2
\$\begingroup\$

Retina 0.8.2, 17 bytes

i`[aeiouy]+
$&p$&

Try it online! Link includes test cases. Explanation: Trivial regexp approach; the i flag turns on case insensitivity (Retina already defaults to a global match).

\$\endgroup\$
1
\$\begingroup\$

Jelly, 20 bytes

Żṁe€ØyŒg$$,j”pƊ€ÐeẎḊ

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Red, 92 bytes

func[s][v: charset"aeiouyAEIOUY"parse s[any[to v copy t any v insert(rejoin["p"t])| skip]]s]

Try it online!

Of course Red's Parse is much more verbose than regex.

\$\endgroup\$
  • 1
    \$\begingroup\$ More verbose than regex for sure, but perhaps that makes it more interesting as well. I like it! \$\endgroup\$ – Maarten Bamelis Aug 14 at 9:07
  • \$\begingroup\$ @MaartenBamelis Thank you! Yes, Red (like Rebol) is an interesting language. \$\endgroup\$ – Galen Ivanov Aug 14 at 9:16
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 97 bytes

	I =INPUT
S	I ARB . L SPAN('AEIOUYaeiouy') . V REM . I	:F(O)
	O =O L V 'p' V	:(S)
O	OUTPUT =O
END

Try it online!

Vowel clusters match SPAN('AEIOUYaeiouy').

\$\endgroup\$
1
\$\begingroup\$

Stax, 17 bytes

àº`≈Zö=q╦ⁿ↔èblTï÷

Run and debug it at staxlang.xyz!

Unpacked (20 bytes) and explanation:

Vv'y+c^+:}'++{'pnL}R
                   R    Regex replace using
                        Pattern:
Vv'y+                     Push "aeiou", push "y", and concatenate
     c^+                  Copy, convert copy to all caps, and concatenate
        :}                Enclose in []
          '++             Push "+" and concatenate
                        And replacement:
             {    }       Block:
              'p            Push "p"
                n           Copy second item (matching substring) to top
                 L          Listify
                          Implicit concatenate
                        Implicit print

No case-insensitive regular expressions in Stax, and the vowel builtins don't include Y. The documentation won't tell you about using a block as a replacement, but it's a working feature nevertheless.

\$\endgroup\$

Your Answer

Maarten Bamelis is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.