75
\$\begingroup\$

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
\$\endgroup\$
  • 46
    \$\begingroup\$ Missed opportunities for test cases: an, c, cath \$\endgroup\$ – Neil Mar 19 '17 at 10:08
  • 54
    \$\begingroup\$ Somebody please do a Go version. \$\endgroup\$ – jl6 Mar 19 '17 at 10:59
  • 2
    \$\begingroup\$ How should the program handle multi-word strings? \$\endgroup\$ – Comrade SparklePony Mar 19 '17 at 17:54
  • 6
    \$\begingroup\$ Let's just observe a minute or two of silence, and wish Rest In Peace to Chuck. \$\endgroup\$ – Erik the Outgolfer Mar 19 '17 at 19:51
  • 1
    \$\begingroup\$ The code text sounds best when spoken by Google Translator in Spanish: translate.google.com/#es/en/… \$\endgroup\$ – palsch Mar 22 '17 at 18:33

45 Answers 45

86
\$\begingroup\$

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"}
\$\endgroup\$
  • 12
    \$\begingroup\$ The only thing missing is 90 bytes. \$\endgroup\$ – Uriel Mar 19 '17 at 14:53
14
\$\begingroup\$

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!)

\$\endgroup\$
  • \$\begingroup\$ I think you can do Y instead of yy and maybe even G instead of 2j \$\endgroup\$ – Kritixi Lithos Mar 19 '17 at 13:51
  • \$\begingroup\$ And you can do <CR> instead of <esc>o \$\endgroup\$ – Kritixi Lithos Mar 19 '17 at 13:54
  • \$\begingroup\$ Also hD works instead of diw \$\endgroup\$ – Kritixi Lithos Mar 19 '17 at 13:57
  • \$\begingroup\$ 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. \$\endgroup\$ – Dominic A. Mar 19 '17 at 16:21
  • \$\begingroup\$ Try it online! \$\endgroup\$ – DJMcMayhem Mar 19 '17 at 20:57
11
\$\begingroup\$

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Tom Carpenter Mar 19 '17 at 9:38
  • 2
    \$\begingroup\$ @TomCarpenter Fixed! :) \$\endgroup\$ – R. Kap Mar 19 '17 at 9:47
11
\$\begingroup\$

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
\$\endgroup\$
6
\$\begingroup\$

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
\$\endgroup\$
6
\$\begingroup\$

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"
\$\endgroup\$
6
\$\begingroup\$

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>

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Pedro A Mar 19 '17 at 16:17
  • \$\begingroup\$ Yep, they each use a byte. \$\endgroup\$ – Jonathan Allan Mar 19 '17 at 20:37
  • \$\begingroup\$ I only count 102 bytes here (using mothereff.in/byte-counter), and also there's no need to use a final semicolon, so it's really 101 bytes. \$\endgroup\$ – apsillers Mar 21 '17 at 13:52
  • \$\begingroup\$ @apsillers You're right, I miscounted! And worse, haha I forgot to golf out the semicolon. Thanks. \$\endgroup\$ – Pedro A Mar 21 '17 at 23:58
6
\$\begingroup\$

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
\$\endgroup\$
  • 5
    \$\begingroup\$ Explanation please? \$\endgroup\$ – ckjbgames Mar 19 '17 at 21:00
  • \$\begingroup\$ @ckjbgames Done! \$\endgroup\$ – DJMcMayhem Mar 20 '17 at 16:42
5
\$\begingroup\$

Pyth - 52 bytes

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

Test Suite.

\$\endgroup\$
5
\$\begingroup\$

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);}
\$\endgroup\$
  • \$\begingroup\$ This doesn't compile in gcc 4.4.7 \$\endgroup\$ – villapx Mar 22 '17 at 19:37
  • \$\begingroup\$ @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? \$\endgroup\$ – Steadybox Mar 22 '17 at 20:33
  • \$\begingroup\$ It generates some warnings, but when you add a main() that actually calls the function it works... \$\endgroup\$ – moooeeeep Mar 23 '17 at 7:47
  • \$\begingroup\$ @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. \$\endgroup\$ – villapx Mar 24 '17 at 17:56
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ @EricDuminil Thanks. Fixed. \$\endgroup\$ – wizzwizz4 Mar 19 '17 at 22:06
  • 1
    \$\begingroup\$ @EricDuminil I knew it was at the end of the line, but there was line wrap in the editor window... :-/ \$\endgroup\$ – wizzwizz4 Mar 19 '17 at 22:17
  • \$\begingroup\$ I'm counting 88 bytes \$\endgroup\$ – Felipe Nardi Batista Mar 20 '17 at 10:41
  • 1
    \$\begingroup\$ @EricDuminil len("\n".__repr__()[1:-2]) is 2. I forgot the __repr__() when measuring the program's length. \$\endgroup\$ – wizzwizz4 Mar 20 '17 at 18:52
  • 1
    \$\begingroup\$ @EricDuminil Only if you wrap it with """ marks instead of " marks. \$\endgroup\$ – wizzwizz4 Mar 20 '17 at 20:18
4
\$\begingroup\$

Retina, 65 bytes

Byte count assumes ISO 8859-1 encoding.

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 41 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python, 94 bytes

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

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

\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need to provide a whole program, a function or better yet, lambda, will suffice. \$\endgroup\$ – weston Mar 21 '17 at 2:00
  • 1
    \$\begingroup\$ +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. \$\endgroup\$ – Kevin Cruijssen Mar 21 '17 at 14:19
  • 1
    \$\begingroup\$ Hope 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 ;) \$\endgroup\$ – auhmaan Mar 21 '17 at 14:45
  • 1
    \$\begingroup\$ @auhmaan If you're compiling to a Func<string, string> you can just do a=> no need for the (). \$\endgroup\$ – TheLethalCoder Mar 21 '17 at 17:26
3
\$\begingroup\$

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!

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

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. pastebin.com/6C6np5Df \$\endgroup\$ – manatwork Mar 20 '17 at 9:59
  • \$\begingroup\$ @manatwork: Wow, very impressive and nice of you. Thanks! \$\endgroup\$ – Eric Duminil Mar 20 '17 at 10:08
3
\$\begingroup\$

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!!)

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

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.

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

PHP, 86 Bytes

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

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
\$\endgroup\$
2
\$\begingroup\$

Stacked, 64 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – TheLethalCoder Mar 21 '17 at 17:28
  • \$\begingroup\$ 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. \$\endgroup\$ – auhmaan Mar 22 '17 at 11:03
  • \$\begingroup\$ Like I said I was being lazy and hand't actually tried anything myself! Was just a suggestion to look into. \$\endgroup\$ – TheLethalCoder Mar 22 '17 at 11:04
2
\$\begingroup\$

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"));
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Is Debug.?s effectively Debug.Print? How does that work? \$\endgroup\$ – BruceWayne Mar 21 '17 at 5:05
  • 1
    \$\begingroup\$ @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 codegolf.meta.stackexchange.com/questions/10258/… ) \$\endgroup\$ – Taylor Scott Mar 21 '17 at 21:38
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Taylor Scott Mar 21 '17 at 21:48
  • 1
    \$\begingroup\$ Good 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! \$\endgroup\$ – BruceWayne Mar 21 '17 at 21:58
1
\$\begingroup\$

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".
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to work for input johnny. tio.run/nexus/… \$\endgroup\$ – Dennis Mar 19 '17 at 15:46
  • \$\begingroup\$ I have no idea how I didn't spot that, fixed now \$\endgroup\$ – Blue Mar 19 '17 at 16:37
1
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Notice this is exactly on par with Java at this moment, and it is sooo much readable ;) \$\endgroup\$ – user7610 Mar 19 '17 at 23:03
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Mar 19 '17 at 23:05
  • \$\begingroup\$ Thanks, but I was aiming at exactly 258 bytes, to be on par with Java \$\endgroup\$ – user7610 Mar 19 '17 at 23:07
  • 2
    \$\begingroup\$ Why are you trying to match another score? This is code golf, not a readability contest. \$\endgroup\$ – weston Mar 20 '17 at 1:59
  • 2
    \$\begingroup\$ @user7610 I think you're missing the point. \$\endgroup\$ – miradulo Mar 20 '17 at 12:19
1
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ You can use a lambda to save lots of bytes. \$\endgroup\$ – corvus_192 Mar 19 '17 at 19:18
  • 1
    \$\begingroup\$ "Johnny" is always followed by a space. \$\endgroup\$ – weston Mar 19 '17 at 19:57
  • \$\begingroup\$ b[0]^=32; will also uppercase without the need for (char) cast. \$\endgroup\$ – weston Mar 19 '17 at 20:03
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

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.