33
\$\begingroup\$

Introduction

We -- especially the geeks among us, who tend to be fans -- all remember this old Far Side cartoon:

Hey! Hey! Hey!

Clearly, Professor Schwartzman could have brushed up on his programming skills just a wee bit more before he put his invention to use. But can you replicate it yourself... using as few bytes as possible?

Input Specs

You must create a script that translates dog sounds into appropriately intonated "Hey"s. This is pure code golf; the lowest number of bytes wins.

Your input will consist of some combination of the following tokens:

  • Bark, Baark, Baaark, Baaaark, ... (that is, B*rk with at least 1 a replacing the asterisk)
  • Woof, Wooof, Woooof, ... (W*f with at least 2 os replacing the asterisk)
  • Grr, Grrr, Grrrr, ..., (G followed by at least 2 rs)
  • Any number of . (period), ! (exclamation mark) and/or ? (question mark) characters, which may occur anywhere in the input

Note, again, that the Woof-based and Grr-based tokens always require at least two os and rs respectively; Wof and Gr are not valid tokens.

There is no limit on how long a token can be (e.g., how many repeated as there can be in a Bark token); however, your decoder only needs to work correctly for input tokens with up to 10 total as, os, or rs to pass this challenge.

Output Specs

Faithful to Schwartzman's design, your canine decoder program must process it into output text as follows:

  • Bark, Woof, and Grr become Hey;
  • Baark, Wooof, and Grrr become Heyy;
  • Baaark, Woooof, and Grrrr become Heyyy; etc.
  • For all Bark-based tokens, the number of ys in the output Hey-based token must be equal to the number of as;
  • For all Woof-based tokens, the number of ys in the output Hey-based token must be one less than the number of os;
  • For all Grr-based tokens, the number of ys in the output Hey-based token must be one less than the number of rs;
  • All punctuation (., !, and ?) is left unchanged.

Remember to drop one y from the output for Woofs and Grrs only! The input Baaaaaaaark?, with 8 as, will become Heyyyyyyyy?, with a matching set of 8 ys. However, Woooooooof? becomes only Heyyyyyyy?, with 7 ys.

Again, if you can get your program to work for input tokens of unlimited size, that's great, but for purposes of this challenge, your program will only be checked to ensure that it works properly for input tokens that have no more than 10 repeated letters.

All Bark-, Woof-, and Grr-based tokens in your input are assumed to begin with capital letters. Therefore, there is no need to handle turning Bark grrr into Hey heyy or anything similar.

Example Inputs and Outputs

    • Input: Bark. Bark! Bark!!
    • Output: Hey. Hey! Hey!!
    • Input: Baaaaaark? (six as)
    • Output: Heyyyyyy? (six ys)
    • Input: Grrrrrrrr... (eight rs)
    • Output: Heyyyyyyy... (seven ys)
    • Input: ?...!
    • Output: ?...!
    • Input: Wooof Woof? Grrrr. Baaaark Grr!
    • Output: Heyy Hey? Heyyy. Heyyyy Hey!
\$\endgroup\$
4
  • 10
    \$\begingroup\$ Hi, and welcome to the site! Just so you know, it is customary on this site to wait a while before accepting the shortest answer. I'd encourage you to unaccept the answer you have accepted, and wait until activity on this question dies down before accepting the shortest answer. This will also encourage more answers. \$\endgroup\$
    – DJMcMayhem
    Commented Jun 27, 2016 at 5:28
  • \$\begingroup\$ Related. \$\endgroup\$ Commented Jun 27, 2016 at 9:35
  • \$\begingroup\$ Thanks for the tips regarding the acceptance process. I was actually planning to check the list of answers every few hours and, if I noticed that a better answer had come along, accept it. Sorry for anyone that I may have denied reputation to here. \$\endgroup\$ Commented Jun 27, 2016 at 12:18
  • 5
    \$\begingroup\$ @A.Mirabeau That's honourable and how it would work ideally, but some people aren't that attentive, so most answerers might assume that the winner has already been chosen. While there's nothing wrong with choosing a winner right away if you're planning to update it, you're likely going to get fewer answers because of it along with some complaints, so it's usually better to wait at least a week. \$\endgroup\$ Commented Jun 27, 2016 at 12:40

9 Answers 9

16
\$\begingroup\$

Retina, 24 18 17 16 bytes

1 byte saved based on an idea in MT0's answer.

\wf?k?
y
\byy
He

Try it online!

Explanation

\wf?k?
y

This simply turns all letters into y, but if they are followed by an f or k we immediately replace that as well. By removing f and k we "normalise" the lengths of the words so that they now all have two more ys than they need.

\byy
He

This turns the first two y of every word into He, completing the transformation.

\$\endgroup\$
1
  • \$\begingroup\$ The most efficient answer so far, well done. I'm not going to accept it just yet, but I can tell you that you deserve CAT FUD. \$\endgroup\$ Commented Jun 28, 2016 at 1:14
10
\$\begingroup\$

Perl, 51 41 39 bytes

s/(G.|[BW]..)(\w+)/He."y"x length$2/ge

Usage

perl -pE 's/(G.|[BW]..)(\w+)/He."y"x length$2/ge'

Input

Bark. Bark! Bark!!
Baaaaaark?
Grrrrrrrr...
?...!
Wooof Woof? Grrrr. Baaaark Grr!

Output

Hey. Hey! Hey!!
Heyyyyyy?
Heyyyyyyy...
?...!
Heyy Hey? Heyyy. Heyyyy Hey!

How it works

Simple regexp substitution using the auto-printing -p adding 1 byte to the count. /ge executes the substitution for every pattern, and runs the replacement as code.


An older version used a three-way detection, but Martin Ender noticed that I was not aggressive enough, which saved me 10 bytes.

msh210 informed me that you don't need quotes around the string He, saving two bytes.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Best one so far, congrats. \$\endgroup\$ Commented Jun 27, 2016 at 4:50
  • 1
    \$\begingroup\$ @A.Mirabeau Thanks, that's quite a compliment considering I just registered here a few minutes ago! \$\endgroup\$
    – pipe
    Commented Jun 27, 2016 at 4:52
  • \$\begingroup\$ Having three solves is motivating considering I just registered here a few hours ago. \$\endgroup\$ Commented Jun 27, 2016 at 5:08
  • 1
    \$\begingroup\$ You don't need the quotes around He. \$\endgroup\$
    – msh210
    Commented Jun 27, 2016 at 14:52
  • \$\begingroup\$ @msh210 Interesting, didn't know you were allowed to omit them there! \$\endgroup\$
    – pipe
    Commented Jun 27, 2016 at 15:15
5
\$\begingroup\$

Python, 106 bytes

f=lambda s,a="B,He,Gr,He,Wo,He,a,y,r,y,o,y,f,,yk,".split(","):s if a==[]else f(s.replace(a[0],a[1]),a[2:])

Demo

https://repl.it/C6Rr

\$\endgroup\$
1
  • \$\begingroup\$ Although not the accepted answer, this one deserves an honorable mention for being the most byte-efficient solution that changes only the three necessary dog-language keywords. \$\endgroup\$ Commented Jul 18, 2016 at 16:10
5
\$\begingroup\$

Vimscript, 51 39 37 33 32 29 28 bytes,

%s/\hk\?f\?/y/g|%s/\<yy/He/g

Regex credits for shaving 9 more bytes: MT0, Martin Ender, msh210

asciicast Demo

Explanation:

                               1. Normalize words to same length & replace letters with ys 

%s                             Regex search and replace 
  /                            Regex search begin
   \hk\?f\?                    Find any letter (\h) optionally followed by k or f
           /                   Regex search end and replace start
            y                  Replace with y
             /                 Replace end
              g                Replace globally

               |               New command

                               2. Overwrite first two y of every word with He

                %s             Regex search and replace
                  /            Regex search begin
                   \<yy        Find yy at a word's beginning
                        /      Regex search end and replace start
                         He    replace with He
                           /   Replace end
                            g  Replace globally
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I don't know Vimscript, but can you use \<yy instead of \<\hy? \$\endgroup\$
    – msh210
    Commented Jun 27, 2016 at 19:25
  • \$\begingroup\$ @msh210 yes it's possible and I updated the solution, thanks \$\endgroup\$
    – starcorder
    Commented Jun 27, 2016 at 19:47
4
\$\begingroup\$

JavaScript (ES6) - 57 55 52 51 Bytes

f=s=>s.replace(/\wk?f?/g,'y').replace(/\byy/g,'He')

Test:

f=s=>s.replace(/\wk?f?/g,'y').replace(/\byy/g,'He');

[
  'Bark. Bark! Bark!!',
  'Baaaaaark?',
  'Grrrrrrrr...',
  '?...!',
  'Wooof Woof? Grrrr. Baaaark Grr!'
].forEach( s=>{console.log( f(s) );} );

Thanks to @MartinEnder for bytes 56 & 51 and the inspiration for some of the other shavings.

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

Perl 5, 25 bytes

A Perl copy of Martin Ender's Retina answer. 24 bytes, plus 1 for -pe instead of -e.

s;\wf?k?;y;g;s;\byy;He;g
\$\endgroup\$
2
\$\begingroup\$

Javascript, 72 66 64 Bytes

f=
t=>t.replace(/k|f/g,'').replace(/\w/g,'y').replace(/\byy/g,'He')

Edit: separated f= and function + reduced byte count

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need the f= (as a default of PPCG), so 64 bytes works. \$\endgroup\$
    – Riker
    Commented Jun 28, 2016 at 15:55
  • \$\begingroup\$ @EᴀsᴛᴇʀʟʏIʀᴋ Great, I didn't know this \$\endgroup\$
    – starcorder
    Commented Jun 28, 2016 at 15:56
1
\$\begingroup\$

Pyke, 35 bytes

.cFDlR\G.^+3-\y*"He"R+)Rdc~lL-],AsJ

Try it here!

Generates Heys, Generates punctuation, zips together, joins

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

Python 3, 140 135 134 bytes

from re import*
f=lambda s:''.join('He'+'y'*len(x)+y for x,y in[(a+b+c,d)for a,b,c,d in findall('(?:Wo(o+)f|Gr(r+)|B(a+)rk)(\W+)',s)])

Using regex to find occurences of replacable characters.

Edit: Golfed 1 byte whitespace and 4 bytes on getting the values from findall result.

Edit2: Golfed 1 byte (Bark's "a" was not counted properly)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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