Code Johnny Code, Code!

75

9

Rock-and-roll founding father Chuck Berry sadly passed away today.

Consider the chorus of his famous song "Johnny B. Goode":

Go, go
Go Johnny go, go
Go Johnny go, go
Go Johnny go, go
Go Johnny go, go
Johnny B. Goode

(There are other ways it has been punctuated but the above will serve for the purposes of the challenge.)

Challenge

Given a nonempty, lowercase string of letters a-z, output the chorus of "Johnny B. Goode" with all the instances of Go or go replaced with the input string, capitalized in the same way.

A trailing newline may optionally follow. Nothing else in the chorus should change.

For example, if the input is code the output must be exactly

Code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Johnny B. Codeode

optionally followed by a newline.

Note that the capitalization of all words matches the original chorus, and (despite lack of rhythm) the Go in Goode is replaced as well as the individual words Go and go.

The shortest code in bytes wins.

Test Cases

"input"
output

"go"
Go, go
Go Johnny go, go
Go Johnny go, go
Go Johnny go, go
Go Johnny go, go
Johnny B. Goode

"code"
Code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Johnny B. Codeode

"a"
A, a
A Johnny a, a
A Johnny a, a
A Johnny a, a
A Johnny a, a
Johnny B. Aode

"johnny"
Johnny, johnny
Johnny Johnny johnny, johnny
Johnny Johnny johnny, johnny
Johnny Johnny johnny, johnny
Johnny Johnny johnny, johnny
Johnny B. Johnnyode

"fantastic"
Fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Johnny B. Fantasticode

Calvin's Hobbies

Posted 2017-03-19T02:00:25.113

Reputation: 84 000

46Missed opportunities for test cases: an, c, cath – Neil – 2017-03-19T10:08:47.093

54Somebody please do a Go version. – jl6 – 2017-03-19T10:59:42.970

2How should the program handle multi-word strings? – Comrade SparklePony – 2017-03-19T17:54:49.983

6Let's just observe a minute or two of silence, and wish Rest In Peace to Chuck. – Erik the Outgolfer – 2017-03-19T19:51:34.623

@SparklePony The input only contains letters so it will only be one "word" as far as the program knows. – Calvin's Hobbies – 2017-03-19T21:03:21.820

What about coding the card game? https://boardgamegeek.com/thread/1156670/rules-explanation-rather-simple-go-johnny-go-go-go

– Mawg says reinstate Monica – 2017-03-20T09:18:49.197

Simple method, Original poem with [tag:kolmogorov-complexity] skills, then swap Go with input – Matthew Roh – 2017-03-23T07:45:49.080

Answers

86

Go, 123 bytes

Go Johnny, Go!

Try it online!

import."strings"
func(s string)string{t,e:=Title(s),", "+s+"\n";return t+e+Repeat(t+" Johnny "+s+e,4)+"Johnny B. "+t+"ode"}

Uriel

Posted 2017-03-19T02:00:25.113

Reputation: 11 708

12The only thing missing is 90 bytes. – Uriel – 2017-03-19T14:53:00.180

14

VIM, 54 49 Keystrokes (saved 1 keystroke from Kritixi Lithos)

yw~hC<Ctrl-R>", <Ctrl-R>0<Enter>Johnny B. <Ctrl-R>"ode<Esc>}O<Ctrl-R>", Johnny <Ctrl-R>0, <Ctrl-R>0<Esc>3.         

Start with the word on a line on a file with the cursor at the first character, then this will replace it all with the text Explanation

  1. Copy the word into a register, then change the first letter to be capitalized and save that to a register.
  2. Write the first line using the registers to fill in the replacements and last lines
  3. Write the second line using the registers to fill in the replacements
  4. Repeat the middle line 3 times

Try it online! (Thanks DJMcMayhem!)

Dominic A.

Posted 2017-03-19T02:00:25.113

Reputation: 533

I think you can do Y instead of yy and maybe even G instead of 2j – user41805 – 2017-03-19T13:51:29.597

And you can do <CR> instead of <esc>o – user41805 – 2017-03-19T13:54:24.437

Also hD works instead of diw – user41805 – 2017-03-19T13:57:03.317

Thanks for the tips! I was able to work in your last one into the current version. I also saved a few more by writing the first and last line in one go, then filling in the middle. – Dominic A. – 2017-03-19T16:21:18.250

Try it online! – James – 2017-03-19T20:57:41.857

Typo: <Esc}0 should be <Esc>} It's correct in the TIO link. – Ray – 2017-03-22T04:48:16.440

11

Pure Bash, 69 76 bytes

M=aaaa;echo -e ${1^}, $1 ${M//a/\\n${1^} Johnny $1, $1}\\nJohnny B. ${1^}ode

Try it online!

R. Kap

Posted 2017-03-19T02:00:25.113

Reputation: 4 730

2In your try-it-online, it doesn't do the required capitalisation. For example if you feed in code all lower case, you don't get the required capitalisation. – Tom Carpenter – 2017-03-19T09:38:33.063

2@TomCarpenter Fixed! :) – R. Kap – 2017-03-19T09:47:56.080

11

05AB1E, 37 bytes

™„, ¹J¹Ð™”ÿºÇ ÿ, ÿ”4.D¹™”ºÇ B. ÿode”»

Try it online!

Explanation

™„, ¹J                                # concatenate title-cased input with ", " and input
     ¹Ð™                              # push input, input, title-cased input
        ”ÿºÇ ÿ, ÿ”                    # push the string "ÿ Johnny ÿ, ÿ" with "ÿ" replaced 
                                      # by title-cased input, input, input
                  4.D                 # push 3 copies of that string
                     ¹™               # push title-cased input
                       ”ºÇ B. ÿode”   # push the string "Johnny B. ÿode" with "ÿ" replaced 
                                      # by title-case input
                                   »  # join the strings by newlines

Emigna

Posted 2017-03-19T02:00:25.113

Reputation: 50 798

6

V, 41, 38 bytes

ÄJé,Ùäwa johnny 5ÄGdwwcWB.W~Aode.Î~

Try it online!

The perfect challenge for V!

Explanation:

ä$                              " Duplicate the input ('go' --> 'gogo', and cursor is on the first 'o')
  a, <esc>                      " Append ', '
                                " Now the buffer holds 'go, go'
          Ù                     " Duplicate this line
           äw                   " Duplicate this word (Buffer: 'gogo, go')
             a Johnny <esc>     " Append ' Johnny ' (Buffer: 'go Johnny go, go')
                           5Ä   " Make 5 copies of this line
G                               " Go to the very last line in the buffer
 dw                             " Delete a word
   w                            " Move forward one word (past 'Johnny')
    cW                          " Change this WORD (including the comma), to
      B.<esc>                   "   'B.'
             W                  " Move forward a WORD
              ~                 " Toggle the case of the character under the cursor
               Aode.<esc>       " Apppend 'ode.'
                         ÎvU    " Capitalize the first letter of each line

James

Posted 2017-03-19T02:00:25.113

Reputation: 54 537

5Explanation please? – ckjbgames – 2017-03-19T21:00:57.073

@ckjbgames Done! – James – 2017-03-20T16:42:48.883

6

Batch, 207 bytes

@set s= %1
@for %%l in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)do @call set s=%%s: %%l=%%l%%
@set j="%s% Johnny %1, %1"
@for %%l in ("%s%, %1" %j% %j% %j% %j% "Johnny B. %s%ode")do @echo %%~l

Neil

Posted 2017-03-19T02:00:25.113

Reputation: 95 035

6

JavaScript (ES6), 104 101 99 bytes

(i,u=i[0].toUpperCase()+i.slice(1),x=`, ${i}
${u} Johnny `+i)=>u+x+x+x+x+`, ${i}
Johnny B. ${u}ode`

Previous version:

(i,u=i[0].toUpperCase()+i.slice(1))=>u+`, ${i}
${u} Johnny ${i}`.repeat(4)+`, ${i}
Johnny B. ${u}ode`

How it works:

  • It's an anonymous function that takes the input as the parameter i

  • Defines a variable u as the input i with the first letter capitalized (Note that this assumes input is nonempty, which is OK)

  • Just directly construct the string to be returned from those two variables.

  • Repeating the string "go, \nGo Johnny go" four times instead of repeating "Go Johnny go, go" saves one byte.


Edit 1: Forgot to golf out the semicolon, haha!! Also miscounted the bytes, it was originally 102, not 104. Thanks apsillers.

Edit 2: Instead of .repeat(4), by putting that string in a variable x and doing x+x+x+x allows saving two bytes.


Test snippet

let f = (i,u=i[0].toUpperCase()+i.slice(1),x=`, ${i}
${u} Johnny `+i)=>u+x+x+x+x+`, ${i}
Johnny B. ${u}ode`;
<input id=I type="text" size=70 value="code"><button onclick="console.log(f(I.value))">Run</button>

Pedro A

Posted 2017-03-19T02:00:25.113

Reputation: 221

By the way, I'm new here, I have a question: do the two newlines in my program count as bytes? If not, it's actually 102 bytes, but I think it probably counts... Right? – Pedro A – 2017-03-19T16:17:41.060

Yep, they each use a byte. – Jonathan Allan – 2017-03-19T20:37:04.160

I only count 102 bytes here (using https://mothereff.in/byte-counter), and also there's no need to use a final semicolon, so it's really 101 bytes.

– apsillers – 2017-03-21T13:52:59.187

@apsillers You're right, I miscounted! And worse, haha I forgot to golf out the semicolon. Thanks. – Pedro A – 2017-03-21T23:58:07.707

6

JavaScript, 98

s=>[S=s[0].toUpperCase()+s.slice(1),[,,,].fill(` ${s}
${S} Johnny `+s)]+`, ${s}
Johnny B. ${S}ode`

Abuses array-to-string serialization to create commas. Builds an array of the form:

["Go",
 " go\nGo Johnny go", (repeated...)]

And concatenates it to the string of the form ", go\nJohnny B. Goode":

["Go",
 " go\nGo Johnny go",
 " go\nGo Johnny go",
 " go\nGo Johnny go",
 " go\nGo Johnny go"] + ", go\nJohnny B. Goode"

apsillers

Posted 2017-03-19T02:00:25.113

Reputation: 3 632

5

Pyth - 52 bytes

j", "_ArBQ3V4s[H" Johnny "G", "G;%"Johnny B. %sode"H

Test Suite.

Maltysen

Posted 2017-03-19T02:00:25.113

Reputation: 25 023

5

C, 156 151 bytes

i,a,b;B(char*s){a=*s++;printf("%c%s, %c%s\n",b=a-32,s,a,s);for(;++i%4;)printf("%c%s Johnny %c%s, %c%s\n",b,s,a,s,a,s);printf("Johnny B. %c%sode",b,s);}

Steadybox

Posted 2017-03-19T02:00:25.113

Reputation: 15 798

This doesn't compile in gcc 4.4.7 – villapx – 2017-03-22T19:37:38.290

@villapx I don't see why it wouldn't. It compiles fine on MinGW and works online too. Maybe some compiler flag is disabling implicit type or function declaration?

– Steadybox – 2017-03-22T20:33:24.803

It generates some warnings, but when you add a main() that actually calls the function it works... – moooeeeep – 2017-03-23T07:47:25.033

@moooeeeep Ok, that's what I was missing--I was under the understanding that this challenge required a complete program, but indeed, it doesn't explicitly say that in the OP. – villapx – 2017-03-24T17:56:55.487

5

Python 3, 88 bytes

lambda x:("{0}, {1}\n"+4*"{0} Johnny {1}, {1}\n"+"Johnny B. {0}ode").format(x.title(),x)

A simple format string, with positional arguments.

wizzwizz4

Posted 2017-03-19T02:00:25.113

Reputation: 1 895

@EricDuminil Thanks. Fixed. – wizzwizz4 – 2017-03-19T22:06:24.933

1@EricDuminil I knew it was at the end of the line, but there was line wrap in the editor window... :-/ – wizzwizz4 – 2017-03-19T22:17:20.800

I'm counting 88 bytes – Felipe Nardi Batista – 2017-03-20T10:41:30.127

@FelipeNardiBatista: len("\n") is 1. I don't know how it should be counted for golf submissions. – Eric Duminil – 2017-03-20T18:34:12.560

1@EricDuminil len("\n".__repr__()[1:-2]) is 2. I forgot the __repr__() when measuring the program's length. – wizzwizz4 – 2017-03-20T18:52:44.317

It's not possible to replace "\n" with a literal newline in Python, right? – Eric Duminil – 2017-03-20T19:10:23.980

1@EricDuminil Only if you wrap it with """ marks instead of " marks. – wizzwizz4 – 2017-03-20T20:18:40.743

4

Python, 94 bytes

lambda s:("|, #\n"+"| Johnny #, #\n"*4+"Johnny B. |ode").replace("|",s.title()).replace("#",s)

Trelzevir

Posted 2017-03-19T02:00:25.113

Reputation: 987

4

Retina, 65 bytes

Byte count assumes ISO 8859-1 encoding.

^
$', 
:T01`l`L
:`,
 Johnny$',
:`$
¶$`
(\S+) (\S+ ).+$
$2B. $1ode

Try it online!

Martin Ender

Posted 2017-03-19T02:00:25.113

Reputation: 184 808

4

Jelly, 41 bytes

5“ Johnny “, “¶”ẋj¹Ḋṙ7ỴŒu1¦€Y“B. ”⁸Œt“ode

Try it online!

Dennis

Posted 2017-03-19T02:00:25.113

Reputation: 196 637

4

C#, 219 211 212 146 122 Bytes

Implemented multiple suggestions from comments with additional optimization This is the Endresult:

a=>{string x=(char)(a[0]^32)+a.Remove(0,1),n=a+"\n",c=", ",r=n+x+" Johnny "+a+c;return x+c+r+r+r+r+n+$"Johnny B. {x}ode";}

Try it online!

Explantation:

a=>//Input parameter Explanation assumes "go" was passed
{
string x = (char)(a[0] ^ 32) + a.Remove(0, 1)// Input with first letter Uppercase "go"->"Go"
,
n = a + "\n",                               //"go" followed by newline
c = ", "                                    //Hard to believe: Comma followed by space
,
r = n + x + " Johnny " + a + c             //"go" follwed by newline followed by "Go Johnny go, "
;
return x + c + r + r + r + r + n + $"Johnny B. {x}ode"; };//return in the right order                              //Johnny B. Goode

Output for testcases:

Go, go
Go Johnny go, go
Go Johnny go, go
Go Johnny go, go
Go Johnny go, go
Johnny B. Goode

Code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Johnny B. Codeode

A, a
A Johnny a, a
A Johnny a, a
A Johnny a, a
A Johnny a, a
Johnny B. Aode

Johnny, johnny
Johnny Johnny johnny, johnny
Johnny Johnny johnny, johnny
Johnny Johnny johnny, johnny
Johnny Johnny johnny, johnny
Johnny B. Johnnyode

Fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Fantastic Johnny fantastic, fantastic
Johnny B. Fantasticode

Edit: Thanks to weston for suggesting using a function

Sir Bitesalot

Posted 2017-03-19T02:00:25.113

Reputation: 41

1You don't need to provide a whole program, a function or better yet, lambda, will suffice. – weston – 2017-03-21T02:00:37.093

1

+1 Thanks for the ^32. That's shorter than the &~32 I was using. Also, a port of my Java 7 answer seems to be shorter: string x(string a){string x=(char)(a[0]^32)+a.Remove(0,1),n=a+"\n",c=", ",r=n+x+" Johnny "+a+c;return x+c+r+r+r+r+n+"Johnny B. "+x+"ode";}} (139 bytes) Try it here.

– Kevin Cruijssen – 2017-03-21T14:19:00.877

1Hope you don't mind, but I've stole from you an idea. :P For not being known as a bad guy, I'll leave here a tip: You could convert your method into a lambda expression ( string x(string a) -> (a)=>, -13 bytes ), only 1 byte behind me ;) – auhmaan – 2017-03-21T14:45:46.947

1@auhmaan If you're compiling to a Func<string, string> you can just do a=> no need for the (). – TheLethalCoder – 2017-03-21T17:26:20.877

3

PHP, 86 Bytes

echo strtr("1, 0\n2222Johnny B. 1ode",[$l=$argn,$u=ucfirst($l),"$u Johnny $l, $l\n"]);

Jörg Hülsermann

Posted 2017-03-19T02:00:25.113

Reputation: 13 026

3

MATLAB/Octave, 133 111 bytes

@(a)regexprep(['1, 2' 10 repmat(['1 32, 2' 10],1,4) '3B. 1ode'],{'1','2','3'},{[a(1)-32 a(2:end)],a,'Johnny '})

It's a start. Can hopefully be reduced further.

Basically it's an anonymous function which takes a string input and then uses regex to create the required output.

@(a)                                                    %Anonymous Function
    regexprep(                                          %Regex Replace
        ['1, 2' 10                                      %First line is: Code, code
            repmat(['1 32, 2' 10],1,4)                  %Then four lines of: Code Johnny code, code 
                               '3B. 1ode'],             %Final line: Johnny B. Codeode
         {'1','2','3'},                                 %1,2,3 are our replace strings in the lines above
         {[a(1)-32 a(2:end)],a,'Johnny '}               %We replace with '(I)nput', 'input' and 'Johnny ' respectively.
    )

An example:

@(a)regexprep(['1, 2' 10 repmat(['1 32, 2' 10],1,4) '3B. 1ode'],{'1','2','3'},{[a(1)-32 a(2:end)],a,'Johnny '});
ans('hi')
ans =

Hi, hi
Hi Johnny hi, hi
Hi Johnny hi, hi
Hi Johnny hi, hi
Hi Johnny hi, hi
Johnny B. Hiode

You can sort of Try it online!. The code doesn't quite work with Octave as all the upper case letters become ${upper($0)}, whereas in MATLAB this is converted to the actual upper case letter.

Given the input is guaranteed to only be a-z (lowercase alphabet), I can save 22 bytes by using a simple subtraction of 32 to convert the first letter in the string to capital, rather than using regex with the upper() function.

As a result, the code now works with Octave as well, so you can now Try it online!

Tom Carpenter

Posted 2017-03-19T02:00:25.113

Reputation: 3 990

3

Ruby, 89 88 86 79 bytes

My first golf submission :

->x{"^, *
#{"^ Johnny *, *
"*4}Johnny B. ^ode".gsub(?^,x.capitalize).gsub ?*,x}

Thanks a lot to @manatwork for his awesome comment : 7 bytes less!

Eric Duminil

Posted 2017-03-19T02:00:25.113

Reputation: 701

1

Nice. The parenthesis around the proc parameter are not needed; you can use literal newline characters instead of character escape; single letter string literals can be written with the ? notation; the parenthesis around the last .gsub's parameters are not needed. http://pastebin.com/6C6np5Df

– manatwork – 2017-03-20T09:59:19.680

@manatwork: Wow, very impressive and nice of you. Thanks! – Eric Duminil – 2017-03-20T10:08:06.070

3

Java 8, 151 147 146 130 bytes

s->{String x=(char)(s.charAt(0)^32)+s.substring(1),n=s+"\n",r=n+x+" Johnny "+s+", ";return x+", "+r+r+r+r+n+"Johnny B. "+x+"ode";}

Explanation:

Try it here.

s->{                               // Method with String as both parameter and return-type
  String x=                        //  Temp String with: 
           (char)(s.charAt(0)^32)  //   The first letter capitalized
    +s.substring(1),               //   + the rest of the String
         n=s+"\n",                 //  Temp String with input + new-line
         c=", ",                   //  Temp String with ", "
         r=n+x+" Johnny "+s+c;     //  Temp String with "input\nInput Johnny input, "
  return x+c+r+r+r+r+n+"Johnny B. "+x+"ode";
                                   //  Return output by putting together the temp Strings
}                                  // End of method

Kevin Cruijssen

Posted 2017-03-19T02:00:25.113

Reputation: 67 575

3

Nova, 105 bytes

a(String s)=>"#{s.capitalize()+", #s\n"+"#s.capitalize() Johnny #s, #s\n"*4}Johnny B. #s.capitalize()ode"

Because Nova (http://nova-lang.org) is extremely early beta (and buggy), there are some obvious handicaps that are in place here keeping it from using even less bytes.

For example, could have saved capitalized function call (which is called 3 times) in a local variable like this:

a(String s)=>"#{(let c=s.capitalize())+", #s\n"+"#c Johnny #s, #s\n"*4}Johnny B. #{c}ode"

which would have taken the byte count down to 89 bytes. The reason this doesn't work now can be blamed on the argument evaluation order in the C language, because Nova is compiled to C. (The argument evaluation order will be fixed in a future update)

Even more, I could have introduced a "title" property in the String class (and I will after doing this lol) to reduce the count from the capitalization function call:

a(String s)=>"#{(let c=s.title)+", #s\n"+"#c Johnny #s, #s\n"*4}Johnny B. #{c}ode"

and that would free up 7 bytes to a new total of 82 bytes.

Furthermore (and further off), once lambda variable type inference is added, this would be valid:

s=>"#{(let c=s.title)+", #s\n"+"#c Johnny #s, #s\n"*4}Johnny B. #{c}ode"

The count could be brought down to 72 bytes.

By the way, this is my first code golf, so I probably have missed even more optimizations that could have been made. And this being a non-golf centric, general purpose language, I think it's pretty impressive.

The first 105 byte code works in the current Nova Beta v0.3.8 build available on http://nova-lang.org

class Test {
    static a(String s)=>"#{s.capitalize()+", #s\n"+"#s.capitalize() Johnny #s, #s\n"*4}Johnny B. #s.capitalize()ode"

    public static main(String[] args) {
        Console.log(a("expl"))
    }
}

outputs:

Expl, expl
Expl Johnny expl, expl
Expl Johnny expl, expl
Expl Johnny expl, expl
Expl Johnny expl, expl
Johnny B. Explode

Thank you for listening to my shameless advertisement for the general purpose language Nova (found at http://nova-lang.org ...get it now!!)

Braden Steffaniak

Posted 2017-03-19T02:00:25.113

Reputation: 141

3

Brainfuck, 352 bytes

,[>+>+<<-]++++++++[>----<-]>.>>>,[.>,]++++++[>+++++++>+++++>++<<<-]>++.>++.>--<<<<[<]<.>>[.>]>>>.>++++++++[>+++++++++>+++++++<<-]>>[<<++>>-]<<[>>+>+>+>+>+<<<<<<-]>++>->-------->-->-->+++++++++>>++++[<<[<]<<<<<[<]<<.>>>[.>]>>.>>>[.>]<[<]<<.<<<[<]<.>>[.>]>.>.<<<[<]<.>>[.>]>>>.>>[>]>-]<<[<]>[.>]<[<]<<.>>>--------.<<<<++.>.<<<[<]<<.>>>[.>]>>>>>>.>----.+.

Try it online!

  1. Get the input.
  2. Save special characters for later.
  3. Write the first part.
  4. Save "Johnny" for later
  5. Print "Go Johnny go, go" four times
  6. Write The last part

I always like challenges in Brainfuck so it was fun. It can probably be golfed more but golfing Brainfuck is kind of long.

AboveFire

Posted 2017-03-19T02:00:25.113

Reputation: 623

2

Stacked, 64 bytes

:@n tc@N('%N, %n
'!'%N Johnny %n, %n
'!4*'Johnny B. 'N'ode'!)sum

Try it online!

Conor O'Brien

Posted 2017-03-19T02:00:25.113

Reputation: 36 228

2

Excel VBA, 137 121 112 89 87 84 Bytes

Anonymous VBE immediate window function that takes input of type Variant/String from cell [A1] and outputs by printing the the VBE immediate window

c=[Proper(A1)]:s=vbCr+c+[" Johnny "&A1&", "&A1]:?c", "[A1]s;s;s;s:?"Johnny B. "c"ode

-16 Bytes for converting to Immediate window function

-9 Bytes for using [PROPER(A1)]

-23 Bytes for dropping For ... loop and abusing the ? statement

-2 Bytes for replacing " Johnny "&[A1]&", "&[A1] with [" Johnny "&A1&", "&A1]

-3 Bytes for using + over & for String concatenation and leaving the terminal string unclosed

Example Case

[A1]="an"          ''  <- Setting [A1] (may be done manually)
                   '' (Below) Anonymous VBE function
c=[Proper(A1)]:s=vbCr+c+[" Johnny "&A1&", "&A1]:?c", "[A1]s;s;s;s:?"Johnny B. "c"ode"
 An, an            ''  <- output
 An Johnny an, an
 An Johnny an, an
 An Johnny an, an
 An Johnny an, an
 Johnny B. Anode

Taylor Scott

Posted 2017-03-19T02:00:25.113

Reputation: 6 709

1Is Debug.?s effectively Debug.Print? How does that work? – BruceWayne – 2017-03-21T05:05:37.017

1

@BruceWayne, nice cape. VBA is supports autoformatting, which means that things like ?,i=1To and &c are digested into more verbose but readable terms like Print, i = 1 To and & c. The community has decided that the compressed version of code in languages is acceptable for responses (see https://codegolf.meta.stackexchange.com/questions/10258/how-to-count-bytes-in-languages-with-autoformatters/10259#10259 )

– Taylor Scott – 2017-03-21T21:38:57.740

1@BruceWayne As for ?, I believe it is a remnant from old version of Excel (4.0 and below) where Macro Sheets were used in place VBA via the VBE where it was used as a keyword for printing a string provided after it to a pre-indicated text file. The ? keyword itself is still very useful for code golfing as the Print keyword is used to write strings to a text file in current versions of VBA. Note, I am going off of memory with one so take that with a grain of salt. – Taylor Scott – 2017-03-21T21:48:57.950

1Good to know! I just asked, not for CodeGolf purposes, but because I'm constantly learning VBA and this was super new to me, so I was curious. Always like to learn neat little tricks in VBA. Cheers! – BruceWayne – 2017-03-21T21:58:02.693

2

C#, 159 130 128 bytes


Golfed

i=>string.Format("{0},{1}????\n{2} B. {0}ode".Replace("?","\n{0} {2}{1},{1}"),(i[0]+"").ToUpper()+i.Substring(1)," "+i,"Johnny");

Ungolfed

i => string.Format( "{0},{1}????\n{2} B. {0}ode"
    .Replace( "?", "\n{0} {2}{1},{1}" ),

    ( i[ 0 ] + "" ).ToUpper() + i.Substring( 1 ), // {0}
    " " + i,                                      // {1}
    "Johnny" );                                   // {2}

Ungolfed readable

i => string.Format( @"{0},{1}
    ????
    {2} B. {0}ode"

    // Replaces the '?' for the string format that contains the 
    // repetition of "___ Johnny ___, ___".
    .Replace( "?", "\n{0} {2}{1},{1}" ),

    // {0} - Converts the first letter to upper,
    //       then joins to the rest of the string minus the first letter
    ( i[ 0 ] + "" ).ToUpper() + i.Substring( 1 ),
    // {1} - The input padded with a space, to save some bytes
    " " + i,
    // {2} - The name used as parameter, to save some bytes
    "Johnny" );

Full code

using System;

namespace Namespace {
    class Program {
        static void Main( string[] args ) {
            Func<string, string> func = i =>
                string.Format( "{0},{1}????\n{2} B. {0}ode"
                    .Replace( "?", "\n{0} {2}{1},{1}" ),

                    ( i[ 0 ] + "" ).ToUpper() + i.Substring( 1 ),
                    " " + i,
                    "Johnny" );

            int index = 1;

            // Cycle through the args, skipping the first ( it's the path to the .exe )

            while( index < args.Length ) {
                Console.WriteLine( func( args[index++] ) );
            }

            Console.ReadLine();
        }
    }
}

Releases

  • v1.2 - - 2 bytes - Swapped (i)=> for i=>, thanks to TheLetalCoder comment.
  • v1.1 - -29 bytes - Thanks to Sir Bitesalot last update, who remembered me I could edit the string before format.
  • v1.0 - 159 bytes - Initial solution.

Notes

The title has a link to a page with the code and the test cases. Just hit Go and the result will be printed below the code.

auhmaan

Posted 2017-03-19T02:00:25.113

Reputation: 906

No need for the () around the argument for the Func just do i=>. You can probably also make use of interpolated strings from C# 6 and lose the string.Format although I haven't looked at the code too much to see how (should be trivial). – TheLethalCoder – 2017-03-21T17:28:24.360

I can drop the (), but I doubt that I can make use of interpolated strings without boosting up the size of the code, due to the replace that happens to collapse the repetition. – auhmaan – 2017-03-22T11:03:29.827

Like I said I was being lazy and hand't actually tried anything myself! Was just a suggestion to look into. – TheLethalCoder – 2017-03-22T11:04:34.450

2

Javascript - 72 106 bytes

Edit: Oops!! I didn't pay attention to the capitalization rules! It'll be longer after a while

Edit 2: Should be following the rules now!

Could probably be golfed more

c=>(`G,g
`+`G Johnnyg,g
`.repeat(4)+`Johnny B.Gode`).replace(/g/g,' '+c.toLowerCase()).replace(/G/g,' '+c)

Used as:

c=>(`G,g
`+`G Johnnyg,g
`.repeat(4)+`Johnny B.Gode`).replace(/g/g,' '+c.toLowerCase()).replace(/G/g,' '+c)
alert(f("Code"));
alert(f("Go"));

Blue Okiris

Posted 2017-03-19T02:00:25.113

Reputation: 157

1

CJam, 50 bytes

r:L(eu\+:M',SLN[MS"Johnny ":OL',SLN]4*O"B. "M"ode"

Try it online!

Explanation:

r:L(eu\+:M',SLN[MS"Johnny ":OL',SLN]4*O"B. "M"ode" e# Accepts an input token.
r:L                                                e# Gets input token and stores it in L.
   (eu\+:M                                         e# Converts token to uppercase-first and stores it in M.
          ',S                                      e# Appears as ", ".
             L                                     e# Input token.
              N                                    e# Newline.
               [                                   e# Opens array.
                M                                  e# Modified token.
                 S                                 e# Space.
                  "Johnny ":O                      e# Pushes "Johnny " and stores it in O.
                             L                     e# Input token.
                              ',SLN                e# The same {',SLN} as before.
                                   ]4*             e# Closes array and repeats it 4 times.
                                      O            e# "Johnny ".
                                       "B. "       e# "B. ".
                                            M      e# Modified token.
                                             "ode" e# "ode".

Erik the Outgolfer

Posted 2017-03-19T02:00:25.113

Reputation: 38 134

1

Pyke, 43 bytes

l5j", "Qs3
Qld"Johnny "iQs3:D4i"B. ode"+Tj:

Try it online!

Constructs and prints the first line then inserts Johnny go before the comma and duplicates that 4 times. Finally constructs the last part.

Blue

Posted 2017-03-19T02:00:25.113

Reputation: 26 661

Doesn't seem to work for input johnny. https://tio.run/nexus/pyke#@59jmqWko6AUWGzMZZSi5JWfkZdXqaCUCeRbuZhkKjnpKeSnpCpph2RZ/f@fBZYFAA

– Dennis – 2017-03-19T15:46:38.663

I have no idea how I didn't spot that, fixed now – Blue – 2017-03-19T16:37:47.607

1

Java 6, 258 242 bytes

enum j{;public static void main(String[]a){char[]b=a[0].toCharArray();b[0]^=32;System.out.printf("%1$s, %2$s\n%1$s %3$s%2$s, %2$s\n%1$s %3$s%2$s, %2$s\n%1$s %3$s%2$s, %2$s\n%1$s %3$s%2$s, %2$s\n%3$sB. %1$sode",new String(b),a[0],"Johnny ");}}

Longest part of it is the format for printf. There are problems with input different than string from a to z(yes I know I don't need to support anything else).

Ungolfed with comments:

enum j {
    ;

    public static void main(String[] a) {
        char[] b = a[0].toCharArray();              // Copy of the input string
        b[0]^=32;                                   // First character of copy to uppercase
        System.out.printf(
                "%1$s, %2$s\n%1$s %3$s%2$s, %2$s\n%1$s %3$s%2$s, %2$s\n%1$s %3$s%2$s, %2$s\n%1$s %3$s%2$s, %2$s\n%3$sB. %1$sode", // Format string
                new String(b),  // Capitalized string
                a[0],           // Original input string
                "Johnny ");     // "Johnny "
    }
}

EDIT: Golfed 16 bytes thanks to weston

cookie

Posted 2017-03-19T02:00:25.113

Reputation: 271

1You can use a lambda to save lots of bytes. – corvus_192 – 2017-03-19T19:18:35.850

1"Johnny" is always followed by a space. – weston – 2017-03-19T19:57:59.577

b[0]^=32; will also uppercase without the need for (char) cast. – weston – 2017-03-19T20:03:30.220

1

Kotlin, 112, 99 Bytes

fun g(s:String)=s.capitalize().let{"$it, $s\n${"$it Johnny $s, $s\n".repeat(4)}Johnny B. ${it}ode"}

Ungolfed:

fun g(s: String) = s
        .capitalize()
        .let {
            "$it, $s\n${"$it Johnny $s, $s\n".repeat(4)}Johnny B. ${it}ode"
        }

weston

Posted 2017-03-19T02:00:25.113

Reputation: 371

1

Python, 258 bytes

from jinja2 import Template
def f(go):
    t = Template("""{{ Go }}, {{ go }}
{{ Go }} Johnny {{ go }}, {{ go }}
{{ Go }} Johnny {{ go }}, {{ go }}
{{ Go }} Johnny {{ go }}, {{ go }}
{{ Go }} Johnny {{ go }}, {{ go }}
Johnny B. {{ Go }}ode""")
    return t.render(Go=go.title(), go=go)

user7610

Posted 2017-03-19T02:00:25.113

Reputation: 111

Notice this is exactly on par with Java at this moment, and it is sooo much readable ;) – user7610 – 2017-03-19T23:03:59.243

1Welcome to the site! You could use string multiplication to shorten this answer. In addition it is not necessary to declare the variable t because it is only called once. – Post Rock Garf Hunter – 2017-03-19T23:05:11.587

Thanks, but I was aiming at exactly 258 bytes, to be on par with Java – user7610 – 2017-03-19T23:07:22.820

2Why are you trying to match another score? This is code golf, not a readability contest. – weston – 2017-03-20T01:59:51.683

Is it even possible to have a readability contest? I mean, there is not an objective measure of readability, as far as I know. – user7610 – 2017-03-20T08:33:30.370

2@user7610 I think you're missing the point. – miradulo – 2017-03-20T12:19:37.220

Guess what, Java's at 147 now. – Duncan X Simpson – 2017-03-20T20:50:46.757

This is not a language war. This is code golf. – Duncan X Simpson – 2017-03-20T20:51:21.583

Sure, I just placed my hole in a different way. – user7610 – 2017-03-22T09:28:23.983

1

Japt, 62 53 bytes

[1S]+U+R+`1 20, 0
`²²+`2B. 1o¸` d0U1Ug u +UÅ2"Johnny 

Try it online!


62-byte solution using a substantially different technique:

Saved 3 bytes thanks to ETHproductions

W=Ug u +UÅ)+", {U}
"+`{W} Johnny {U}, {U}
`²²+`Johnny B. {W}o¸

Try it online!

Oliver

Posted 2017-03-19T02:00:25.113

Reputation: 7 160

1

Bash, 72 bytes

(modified version of R. Kapp's answer):

M=hnny;echo -e ${1^}, $1 ${M//?/\\n${1^} Jo$M $1, $1}\\nJo$M B. ${1^}ode

Try it online!

agc

Posted 2017-03-19T02:00:25.113

Reputation: 141

1

Mathematica, 83 bytes

{a=ToTitleCase@#,b=", ",#,{"
",a," Johnny ",#,b,#}~Table~4,"
Johnny B. ",a}<>"ode"&

Anonymous function. Takes a string as input and returns a string as output. Could probably be golfed further.

LegionMammal978

Posted 2017-03-19T02:00:25.113

Reputation: 15 731

1

Swift, 151 144 bytes

import Foundation
func b(a:String){let u=a.capitalized;print(u+", "+a);for _ in 0..<4{print(u+" Johnny "+a+", "+a)};print("Johnny B. "+u+"ode")}


Previous Version:

import Foundation
func b(s:String){let u=s.capitalized;print("\(u), \(s)");for _ in 0..<4{print("\(u) Johnny \(s), \(s)")};print("Johnny B. \(u)ode");}


Ungolfed:

import Foundation
func beGoode(inputString: String) {
        let capitalizedString = inputString.capitalized
        print("\(capitalizedString), \(inputString)")
        for _ in 0..<4 {
            print("\(capitalizedString) Johnny \(inputString), \(inputString)")
        }
        print("Johnny B. \(capitalizedString)ode")
    }

Example:

b(s: "code")

Code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Code Johnny code, code
Johnny B. Codeode

Edit: Golfed down 6 bytes by using string concatenation instead of interpolation. Down 1 byte more by removing final semi-colon.

Stephen

Posted 2017-03-19T02:00:25.113

Reputation: 121

Welcome to the site! – James – 2017-03-23T14:48:23.440

Thanks! I thought I'd finally try my hand at a solution. – Stephen – 2017-03-23T14:56:42.300

1

PHP, 84 83 bytes

Good Bye, Chuck!

<?=strtr(str_pad("1, 0",41,"
1 2 0, 0")."2 B. 1ode",[$argn,ucfirst($argn),Johnny]);

or

<?=strtr(str_pad("1,0",32,"
1 20,0")."2 B. 1ode",[" $argn",ucfirst($argn),Johnny]);

Run with echo '<input>' | php -nF <filename> or try them online.

Titus

Posted 2017-03-19T02:00:25.113

Reputation: 13 814

@WeijunZhou fixed. thanks for the hint. – Titus – 2018-03-28T21:00:30.953

0

Q, 97 bytes

{u:(upper x[0]),1_x;u,", ",x,"\n",((,/)4#enlist u," Johnny ",x,", ",x,"\n"),"Johnny B. ",u,"ode"}

Assuming input "go".

  1. Defines uppercase input as variable u.
  2. Merges u with input x to generate first line, "Go, go".
  3. Copies body, "Go Johnny go, go", four times.
  4. Merges with final line, "Johnny B. Goode".

Daniel Plainview

Posted 2017-03-19T02:00:25.113

Reputation: 21

0

Common Lisp, 107 bytes

(apply'format t"~:(~A~), ~a~%~4@{~:(~A~) Johnny ~a, ~a ~%~}Johnny G. ~:(~A~)ode"(fill(make-list 15)(read)))

Try it online!

Renzo

Posted 2017-03-19T02:00:25.113

Reputation: 2 260

0

Japt, 52 bytes

[V=UhUg u)',SUR[VSW="Johnny "U',SUR]¬²²W`B. {V}o¸`]¬

Try it

Shaggy

Posted 2017-03-19T02:00:25.113

Reputation: 24 623

0

C (gcc), 110 bytes

n;f(char*s){for(n=6;n--;)printf(n?n^5?"%c%s Johnny %s, %s\n":"%c%s, %s\n":"Johnny B. %c%sode",*s-32,s+1,s,s);}

Try it online!

gastropner

Posted 2017-03-19T02:00:25.113

Reputation: 3 264

0

Excel, 103 bytes

-8 bytes saved by using # and ~ instead of Go and go

=SUBSTITUTE(SUBSTITUTE("#, ~
"&REPT("# Johnny ~, ~
",4)&"Johnny B. #ode","#",PROPER(A1)),"~",LOWER(A1))

Wernisch

Posted 2017-03-19T02:00:25.113

Reputation: 2 534

0

Pyth, 69 67 bytes

.F++"{0}, {1}\n"*"{0} {2} {1}, {1}\n"4"{2} B. {0}ode"[rz4rz0"Johnny

Try it here!

Python 3 translation:
z=input()
print(("{0}, {1}\n"+"{0} {2} {1}, {1}\n"*4+"{2} B. {0}ode").format(z.capitalize(),z.lower(),"Johnny"))

hakr14

Posted 2017-03-19T02:00:25.113

Reputation: 1 295

0

SOGL V0.12, 38 bytes

,1Ƨ, Ο,@+"9:█ŗ‘⁽:A+⁴+:³a,⁽"B. ŗode”+⁰⁽

Try it Here!

dzaima

Posted 2017-03-19T02:00:25.113

Reputation: 19 048

0

Stax, 34 bytes

₧:hç╟╫9↓Σl(Ü°╤`╨+B╥¢└ÇI╩4╢YP-├Γ■fl

Run and debug it

Unpacked, ungolfed, and commnted, it looks like this.

:.X         title case input and store in x register        e.g. "Code"
., +        concat ", "                                     e.g. "Code, "
y+Q         concat input and print without popping          e.g. "Code, code"
x`?*u)U"`+  concat " Johnny " to x                          e.g. "Code, code" "Code Johnny "
sv+         swap top of stack, lowercase, concat            e.g. "Code Johnny code, code"
QQQQ        print 4 times without popping                   e.g. "Code Johnny code, code"
j1@p        split, print the second word without newline    e.g. "Johnny"
" B. `xode  output string template with x register embedded e.g. " B. Codeode"

Run this one

recursive

Posted 2017-03-19T02:00:25.113

Reputation: 8 616

0

Jstx, 52 bytes

₧&Eu◄, :;4♦/u♂ Johnny♀:◄, :;► :;41u☼◘~ΘåÆëë╟┌°;♫☺åΘ:

Try it online!

Explanation

₧&          # Push literal 100
E           # Push the second stack value the absolute value of the first stack value times.
u           # Push the first stack value transformed to Title Case.
◄,          # Push literal , 
:           # Push the sum of the second and first stack values.
;           # Push the difference of the second and first stack values.
4           # Print the first stack value, then a newline.
♦           # Push literal 4
/           # Enter an iteration block over the first stack value.
u           # Push the first stack value transformed to Title Case.
♂ Johnny♀   # Push literal  Johnny
:           # Push the sum of the second and first stack values.
◄,          # Push literal , 
:           # Push the sum of the second and first stack values.
;           # Push the difference of the second and first stack values.
►           # Push literal  
:           # Push the sum of the second and first stack values.
;           # Push the difference of the second and first stack values.
4           # Print the first stack value, then a newline.
1           # End an iteration block.
u           # Push the first stack value transformed to Title Case.
☼◘~ΘåÆëë╟┌° # Push literal Johnny B. 
;           # Push the difference of the second and first stack values.
♫☺åΘ        # Push literal ode
:           # Push the sum of the second and first stack values.

Quantum64

Posted 2017-03-19T02:00:25.113

Reputation: 371

0

Python 2, 156 bytes

#coding:UTF-8
import sys;a="þ, ÿ"+("\nþ Johnny ÿ, ÿ"*4)+"\nJohnny B. þode";print a.replace("þ",sys.argv[1].title()).replace("ÿ",sys.argv[1].lower())

De-golfed:

#coding:UTF-8
import sys
a = "þ, ÿ" + ("\nþ Johnny ÿ, ÿ" * 4) + "\nJohnny B. þode"
b = a.replace("þ", sys.argv[1].title())
c = b.replace("ÿ", sys.argv[1].lower())
print c

Works as long as input doesn't contain the character ÿ, but passes all test cases.

Bytes counted by this byte counter.

NMorris

Posted 2017-03-19T02:00:25.113

Reputation: 1

1Welcome to the site! – caird coinheringaahing – 2018-04-05T10:08:43.110