Professor Schwartzman's Acme Canine Decoder 2000

31

1

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!

A. Mirabeau

Posted 2016-06-27T00:21:45.007

Reputation: 565

10Hi, 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. – James – 2016-06-27T05:28:36.367

Related. – Martin Ender – 2016-06-27T09:35:00.407

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. – A. Mirabeau – 2016-06-27T12:18:18.793

5@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. – Martin Ender – 2016-06-27T12:40:20.840

Answers

16

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.

Martin Ender

Posted 2016-06-27T00:21:45.007

Reputation: 184 808

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. – A. Mirabeau – 2016-06-28T01:14:08.743

10

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.

pipe

Posted 2016-06-27T00:21:45.007

Reputation: 219

2Best one so far, congrats. – A. Mirabeau – 2016-06-27T04:50:01.837

1@A.Mirabeau Thanks, that's quite a compliment considering I just registered here a few minutes ago! – pipe – 2016-06-27T04:52:51.687

Having three solves is motivating considering I just registered here a few hours ago. – A. Mirabeau – 2016-06-27T05:08:42.797

1You don't need the quotes around He. – msh210 – 2016-06-27T14:52:02.843

@msh210 Interesting, didn't know you were allowed to omit them there! – pipe – 2016-06-27T15:15:42.510

Yeah, it's an instance of a general rule that any string that has only letters, numbers, and underscores (and doesn't start with a number) is the name of a variable whose value is itself. (Or something like that, anyway.) Thus, you can use hello instead of 'hello'. (Note that it doesn't work with function names like y or operators like x.) It's a bad idea in general, of course, but useful in golfing. – msh210 – 2016-06-27T15:23:18.373

5

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

Chuck Morris

Posted 2016-06-27T00:21:45.007

Reputation: 456

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. – A. Mirabeau – 2016-07-18T16:10:32.180

4

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

starcorder

Posted 2016-06-27T00:21:45.007

Reputation: 461

1I don't know Vimscript, but can you use \<yy instead of \<\hy? – msh210 – 2016-06-27T19:25:33.050

@msh210 yes it's possible and I updated the solution, thanks – starcorder – 2016-06-27T19:47:40.377

4

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.

MT0

Posted 2016-06-27T00:21:45.007

Reputation: 3 373

2

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

starcorder

Posted 2016-06-27T00:21:45.007

Reputation: 461

1You don't need the f= (as a default of PPCG), so 64 bytes works. – Rɪᴋᴇʀ – 2016-06-28T15:55:27.430

@EᴀsᴛᴇʀʟʏIʀᴋ Great, I didn't know this – starcorder – 2016-06-28T15:56:09.143

2

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

msh210

Posted 2016-06-27T00:21:45.007

Reputation: 3 094

1

Pyke, 35 bytes

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

Try it here!

Generates Heys, Generates punctuation, zips together, joins

Blue

Posted 2016-06-27T00:21:45.007

Reputation: 26 661

1

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)

Gábor Fekete

Posted 2016-06-27T00:21:45.007

Reputation: 2 809