Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
12  
Missed opportunities for test cases: an, c, cath – Neil 15 hours ago
17  
Somebody please do a Go version. – jl6 14 hours ago
    
How should the program handle multi-word strings? – SparklePony 7 hours ago
    
Let's just observe a minute or two of silence, and wish Rest In Peace to Chuck. – Erik the Outgolfer 5 hours ago
    
@SparklePony The input only contains letters so it will only be one "word" as far as the program knows. – Helka Homba 4 hours ago

23 Answers 23

Go, 153 128 bytes

Go Johnny, go.

Try it online!

import u"strings"
func(s string)string{t,e:=u.Title(s),", "+s+"\n";return t+e+u.Repeat(t+" Johnny "+s+e,4)+"Johnny B. "+t+"ode"}
share|improve this answer
4  
The only thing missing is 90 bytes. – Uriel 10 hours ago
2  
+1 for using Go – Challenger5 5 hours ago

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
share|improve this answer

V, 41 bytes

ä$a, Ùäwa Johnny 5ÄGdwwcWB.W~Aode.ÎvU

Try it online!

The perfect challenge for V!

share|improve this answer
1  
Explanation please? – ckjbgames 4 hours ago

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!

share|improve this answer
    
In 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 16 hours ago
1  
@TomCarpenter Fixed! :) – R. Kap 15 hours ago

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
share|improve this answer

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
share|improve this answer
    
I think you can do Y instead of yy and maybe even G instead of 2j – Kritixi Lithos 11 hours ago
    
And you can do <CR> instead of <esc>o – Kritixi Lithos 11 hours ago
    
Also hD works instead of diw – Kritixi Lithos 11 hours ago
    
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. 9 hours ago
    
Try it online! – DJMcMayhem 4 hours ago

Pyth - 52 bytes

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

Test Suite.

share|improve this answer

Python, 102 bytes

lambda s:("|, #\n"+"| Johnny #, #\n"*4+"Johnny B. |ode").replace("|",s.title()).replace("#",s.lower())
share|improve this answer
    
.lower() isn't needed ("Given a nonempty, lowercase string of letters a-z") – Eric Duminil 5 hours ago

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);}
share|improve this answer

Stacked, 64 bytes

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

Try it online!

share|improve this answer

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!

share|improve this answer

Retina, 65 bytes

Byte count assumes ISO 8859-1 encoding.

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

Try it online!

share|improve this answer

Jelly, 41 bytes

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

Try it online!

share|improve this answer

JavaScript (ES6), 104 bytes

(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.

Test snippet

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

share|improve this answer
    
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? – Hamsteriffic 9 hours ago
    
Yep, they each use a byte. – Jonathan Allan 5 hours ago

Python 3, 86 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.

share|improve this answer
    
@EricDuminil Thanks. Fixed. – wizzwizz4 3 hours ago
1  
@EricDuminil I knew it was at the end of the line, but there was line wrap in the editor window... :-/ – wizzwizz4 3 hours ago

PHP, 88 Bytes

<?=$u=ucfirst($l=$argv[1]),", $l\n",$r="$u Johnny $l, $l\n","$r$r{$r}Johnny B. {$u}ode";
share|improve this answer

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".
share|improve this answer

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.

share|improve this answer
    
Doesn't seem to work for input johnny. tio.run/nexus/… – Dennis 9 hours ago
    
I have no idea how I didn't spot that, fixed now – muddyfish 9 hours ago

Java, 258 bytes

enum j{;public static void main(String[]a){char[]b=a[0].toCharArray();b[0]=(char)(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$s B. %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] = (char) (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$s B. %1$sode", // Format string
                new String(b),  // Capitalized string
                a[0],           // Original input string
                "Johnny");      // "Johnny"
    }
}
share|improve this answer
    
You can use a lambda to save lots of bytes. – corvus_192 6 hours ago
1  
"Johnny" is always followed by a space. – weston 5 hours ago
    
b[0]^=32; will also uppercase without the need for (char) cast. – weston 5 hours ago

Ruby, 89 88 86 bytes

My first golf submission :

->(x){"^, *\n#{"^ Johnny *, *\n"*4}Johnny B. ^ode".gsub('^',x.capitalize).gsub('*',x)}
share|improve this answer

Kotlin, 112 Bytes

fun g(s:String):String{val u=s.capitalize();return "$u, $s\n${"$u Johnny $s, $s\n".repeat(4)}Johnny B. ${u}ode"}

Ungolfed:

fun g(s: String): String {
    val u = s.capitalize()
    return "$u, $s\n${"$u Johnny $s, $s\n".repeat(4)}Johnny B. ${u}ode"
}
share|improve this answer

VBA, 138 Bytes

Subroutine that takes variant input b of assumed type string, and outputs by printing the the VBE immediates window

Sub a(b)
n=vbCr
c=b
Mid(c,1,1)=UCase(b)
s=c &", " &b
For i=1To 4
s=s &n &c &" Johnny " &b &", " &b
Next
Debug.?s &n &"Johnny B. " &c &"ode"
End Sub

Example Case

a "an"          ''  <- subroutine call
An, an          ''  <- output
An Johnny an, an
An Johnny an, an
An Johnny an, an
An Johnny an, an
Johnny B. Anode
share|improve this answer

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)
share|improve this answer
    
Notice this is exactly on par with Java at this moment, and it is sooo much readable ;) – user7610 2 hours ago
    
Welcome 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. – Wheat Wizard 2 hours ago
    
Thanks, but I was aiming at exactly 258 bytes, to be on par with Java – user7610 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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