-13
\$\begingroup\$

Your friend has been caught sleeping in class again! As punishment, his computer science teacher has assigned him homework, knowing that he will not be able to solve it due to not having paid any attention.

The assignment seems deceiving simple at first; all that is required is to output the string:

John Doe

Unfortunately, the teacher is intent on making the simple assignment much more difficult. Since your friend was not listening when the teacher explained the concept of strings and numbers, the teacher forbids him from using any number character (0-9) or the quotes (", ', `).

Can you help your friend solve this problem with the shortest code possible, and save his computer science grade in the process?

Try to find the shortest code in each language!

Note: it is encouraged to try to solve this problem in languages where string/character literals exist but cannot be used due to the restrictions (most mainstream languages are like this, e.g. C++).

\$\endgroup\$
8
  • 6
    \$\begingroup\$ This is awfully close to a previous poorly received challenge. \$\endgroup\$
    – Wheat Wizard
    Mar 2, 2021 at 20:55
  • \$\begingroup\$ And as we still not know why some serial downvoters had objections on many solutions there, personally I feel no urge to participate in another so similar challenge. \$\endgroup\$
    – manatwork
    Mar 2, 2021 at 20:59
  • \$\begingroup\$ @manatwork You may find this helpful. \$\endgroup\$
    – Wheat Wizard
    Mar 2, 2021 at 21:05
  • \$\begingroup\$ @WheatWizard, read it last year. Read it again. Still in dark. But never mind. \$\endgroup\$
    – manatwork
    Mar 2, 2021 at 21:17
  • 1
    \$\begingroup\$ Why did you repost your previous question? \$\endgroup\$
    – user100690
    Mar 3, 2021 at 6:20

24 Answers 24

5
\$\begingroup\$

C, 41 bytes

#define p(x)puts(#x)
main(){p(John Doe);}

Try it online!

# converts a token passed to a macro to a string literal.

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

PowerShell, 51 bytes

#John Doe
(gc $PsCommandPath)[+$x]-replace(echo \#)

Try it online!

PowerShell, 59 54 bytes

$x++;$x++
-join((echo John),(echo Doe).PadLeft($x+$x))

Try it online!

PowerShell, 56 bytes

-join((echo John),[char](++$x-shl$x+++$x+$x),(echo Doe))

Try it online!

PowerShell, 57 bytes

-join((echo John),[char]([char](echo !)-++$x),(echo Doe))

Try it online!

PowerShell, The Most Trivial Solution™, 10 bytes

(I'm sorry, I said I wanted to avoid trivial solutions, but I did not know this was possible, and felt that this method might benefit PS solutions to other challenges)

{John Doe}

Try it online!

\$\endgroup\$
15
  • \$\begingroup\$ 19 bytes: Write-Host John Doe \$\endgroup\$
    – Makonede
    Mar 2, 2021 at 21:18
  • \$\begingroup\$ @Makonede Feel free to post yourself; It's a good example for PowerShell, but I want my answer to stick with non-trivial/built-in solutions to the problem :) Thanks for pointing it out, though! \$\endgroup\$ Mar 2, 2021 at 21:22
  • 1
    \$\begingroup\$ Alright, sounds good! \$\endgroup\$
    – Makonede
    Mar 2, 2021 at 21:25
  • \$\begingroup\$ one more to the collection :) Try it online! \$\endgroup\$
    – mazzy
    Mar 3, 2021 at 11:13
  • \$\begingroup\$ +1 Try it online! \$\endgroup\$
    – mazzy
    Mar 3, 2021 at 11:17
5
\$\begingroup\$

JavaScript (V8), 80 77 76 65 40 34 20 bytes

_=>/John Doe/.source

Turns out the _=> might be required.

History:

/John Doe/.source

Oh there's a trivial way to do it. I just wasted an hour :/

31:

(/John Doe/+[]).slice(-~[],~[])

Finally found a way to get rid of that pesky toString :p

37:

/John Doe/.toString().slice(-~[],~[])

Why use the function thing at all?

62:

(function John_Doe(){}).name.replace(/_/,/ /.toString()[-~[]])

I probably should have thought about using / / sooner...

73:

(function John_Doe(){}).name.replace(/_/,{}.toString(x=true)[x-~x-~x-~x])

I wasted twenty minutes trying to do this character by character then remembered this.

Golfed three bytes by using -~x, which is the same as +2.

\$\endgroup\$
2
  • \$\begingroup\$ Currently, your code is a snippet. You can make it a function by just prepending _=>: _=>/John Doe/.source \$\endgroup\$
    – Makonede
    Apr 14, 2021 at 19:17
  • \$\begingroup\$ @Makonede I was under the impression snippets were allowed but I'll mention that in the answer and ask for clarification in a comment. Thanks! \$\endgroup\$ Apr 14, 2021 at 19:29
4
\$\begingroup\$

Vyxal, s, 16 bytes

\J\o\h\nð\D\o\eW

Try it Online!

Explained

\J\o\h\n    # Push the characters J, o, h and n
ð           # Push a space
\D\o\e      # Push the characters D, o and e
W           # Wrap everything into a list. The s flag joins on newlines
\$\endgroup\$
2
  • \$\begingroup\$ @Downvoters why are you serial downvoting everything? Just because it's trivial doesn't mean you need to act harshly. \$\endgroup\$
    – lyxal
    Mar 3, 2021 at 7:34
  • \$\begingroup\$ I also faced the same problem in answering the trivial questions, in most of the cases the downvoter doesn't reply. Anyhow cut your downvote with an upvote. \$\endgroup\$
    – Wasif
    Mar 3, 2021 at 9:42
3
\$\begingroup\$

Python 3, 55 43 28 bytes

Saved 12 bytes thanks to Makonede!!!
Saved 15 bytes thanks to ovs!!!

print(*dict(John=[],Doe=[]))

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Nice, that seems to be the shortest fix here. Also, here's a 43 byte version: John=Doe=[];print(dir()[True],dir()[False]) \$\endgroup\$
    – Makonede
    Mar 2, 2021 at 22:00
  • \$\begingroup\$ @Makonede Was just fixing that and nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Mar 2, 2021 at 22:00
  • \$\begingroup\$ 28 bytes with print(*dict(John=[],Doe=[])) \$\endgroup\$
    – ovs
    Mar 3, 2021 at 6:29
  • \$\begingroup\$ @ovs Nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Mar 3, 2021 at 7:44
  • \$\begingroup\$ @tsh Both unfortunately for this challenge and fortunately as capital letters come first, Python sorts namespaces - so your suggestion prints out Doe John. \$\endgroup\$
    – Noodle9
    Mar 3, 2021 at 12:15
3
\$\begingroup\$

CW for languages where the program John Doe is valid

Feel free to add to this list if you find a solution in another language.

NOTE: Text editors have been excluded as there are way too many of them.

///

Charcoal

GS2

HTML

Keg

PHP

Quine

Text

\$\endgroup\$
1
  • \$\begingroup\$ I'm not sure, so I won't add it yet, but what about vim -y? \$\endgroup\$
    – PkmnQ
    Apr 19, 2021 at 5:26
2
\$\begingroup\$

05AB1E, 5 bytes

”„ÐÒ«

Try it online!

”„ÐÒ«  # full program
”„ÐÒ«  # push "John Doe"
       # implicit output
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -M5.10.0, 15 bytes

say q(John Doe)

Try it online! I don't know whether -M5.10.0 is optimal, I just cargo culted it from a random Perl answer.

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

Java, 166 bytes

$->{class John_Doe{}final char o=Byte.MAX_VALUE/Byte.MAX_VALUE,t=o+o,f=t+t+o,T=f+f,u=T*T-f,s=f*(f+o)+t;System.out.print(John_Doe.class.getSimpleName().replace(u,s));}

Try it online!

Java, 192 bytes

a->{final char O=Byte.MAX_VALUE/Byte.MAX_VALUE,T=O+O,t=T+O,F=t+T,S=F+T,Z=F+F,Y=Z*Z,o=Y+Z+O,J=S*Z+T+T,h=Y+T+T,n=Y+Z,$=Z*t+T,D=F*Z+Z+F+t,e=Y+O;System.out.print(String.valueOf(J)+o+h+n+$+D+o+e);}

This uses Byte.MAX_VALUE/Byte.MAX_VALUE to obtain the number 1 and then uses it to construct the ASCII codes of all the characters in the string. final is needed so that casts from int to char are not necessary.

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ The 41 bytes version is not valid as digits are not allowed either. \$\endgroup\$
    – Laikoni
    Mar 2, 2021 at 21:43
  • 1
    \$\begingroup\$ @Laikoni Good catch. \$\endgroup\$ Mar 2, 2021 at 21:44
  • \$\begingroup\$ You can use pre-existing public variables to get the space char value (or use a field, which auto initializes itself to 0, and shift its bits around like ++x>>++x>>++x, 1<<2<<3=32). I got it to 134 bytes \$\endgroup\$
    – branboyer
    Mar 8, 2021 at 8:30
2
\$\begingroup\$

JavaScript, 32 bytes

_=>(/John Doe/+[]).slice(-~_,~_)

When the function is called without arguments, the first argument will be undefined and ~undefined produces -1.

Try it online!

Thanks to tsh for the idea

JavaScript (V8), 39 bytes

print(...Object.keys({John:[],Doe:[]}))

print uses a space to separate arguments when printing.

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ x=>(/Jone Doe/+[]).slice(-~x,~x) 32 bytes \$\endgroup\$
    – tsh
    Mar 3, 2021 at 2:21
  • \$\begingroup\$ @tsh Nice one.​ \$\endgroup\$ Mar 3, 2021 at 2:23
  • \$\begingroup\$ Interesting approach with the print and Object.keys, probably the most creative answer so far! I've also found a 17 byte JS solution a few days ago, although it's a snippet (can be made into a function for 20 bytes). \$\endgroup\$ Mar 5, 2021 at 0:39
1
\$\begingroup\$

Windows Batch, 13 bytes

echo John Doe
\$\endgroup\$
1
\$\begingroup\$

Jelly, 8 bytes

“®ƊʠỌḂŒ»

Try it online!

The at the beginning isn't defined as a quote in the challenge, so Id say this is valid.

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

PowerShell Core, 19 bytes

Write-Host John Doe

Try it online!

Write-Host John Doe  # full program
Write-Host           # output...
           John Doe  # literal
\$\endgroup\$
1
\$\begingroup\$

Bubblegum, 10 7 bytes

-3 bytes by @tsh.

´H&ÒF

Try it online! This program gets converted to base 256, then to base 96, and each digit \$n\$ gets replaced with the \$n\$th printable ASCII character. Hexdump:

00000000: 0bb4 4826 02d2 46  ..H&..F
\$\endgroup\$
2
  • \$\begingroup\$ 7 bytes: 0bb4482602d246 \$\endgroup\$
    – tsh
    Mar 3, 2021 at 2:13
  • \$\begingroup\$ The 7 bytes version is not a zip stream, but a base 96 to base 256 encoding. \$\endgroup\$
    – tsh
    Mar 3, 2021 at 2:29
1
\$\begingroup\$

Notepad / Gedit, 15 keystrokes

SHIFT+J
o
SHIFT+LEFT ARROW
CTRL+C
RIGHT ARROW
h
n

D
CTRL+V
e

Notepad / Gedit (Boring solution), 8 keystrokes

J
o
h
n

D
o
e
\$\endgroup\$
1
  • 2
    \$\begingroup\$ If capitals count as one keystroke, shouldn't SHIFT+J be J? \$\endgroup\$
    – Makonede
    Mar 3, 2021 at 18:54
1
\$\begingroup\$

brainfuck, 91 bytes

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

Try it online!

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

Julia, 38 bytes

print(:John,Char(ceil(π*π*π)),:Doe)

Try it online!

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

Java (JDK), 134 bytes

class John{public static void main(String[]z){System.out.print(John.class.getName()+(char)Float.SIZE+Doe.class.getName());}}enum Doe{}

Java (OpenJDK 8), 131 bytes

interface John{static void main(String[]z){System.out.print(John.class.getName()+(char)Float.SIZE+Doe.class.getName());}}enum Doe{}

This is an everything java solution - count in the whole file, header and footer. No tio.run link because it depends on the class name, which must be John in this case. In Java 8+ you can make it an interface and omit public in the main method for -3.

Float.SIZE and Integer.SIZE return 32, the char code for a space. In this case, getName(), getTypeName(), getSimpleName(), and getCanonicalName() will all return just the desired name part of the class/enum. You can make John an enum for -1, but it would require an extra ; at the start to declare the enum objects, +1, making it equal length.

You can run this to verify whether it prints the correct String (and that the space character is an actual space, because doing (char)0, for example, will leave a blank spot without it technically being a space):

interface John { // or class
    public static void main(String[]z) {
        System.out.print((John.class.getName() + (char) Float.SIZE + Doe.class.getName())
            .equals("John Doe"));
        }
    }
enum Doe{}
\$\endgroup\$
1
\$\begingroup\$

Pxem, 10 bytes of file name + 0 bytes of content = 10 bytes

  • Filename: John Doe.p
  • Content is empty

What a low effort.

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

Deadfish~, 70 bytes

{i}dds{i}c{iiii}dddc{d}iiiciiiiiic{{d}ii}iic{iii}iiiiiic{iiii}iiic{d}c

Try it online!

It somehow beats Java!

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

Vyxal, 8 bytes

«:L∞I\«ǐ

Try it Online!

Explanation:

«:L∞I\«   # Base-256 compressed "john doe"
       ǐ  # Convert To Title Case
          # Implicit output

Vyxal doesn't support capital letters in compressed strings, which is why the ǐ command is needed.

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

PHP, 8 bytes

John Doe

Try it online!

John Doe  # full program
John Doe  # no leading <?, so outputs source
\$\endgroup\$
0
\$\begingroup\$

Python 2, 56 14+8=22 bytes

print __file__

Must be saved in a file called John Doe in the root directory. However, the path may not even contain a single / for this to work. Therefore, this file's full path should be only John Doe, and not /John Doe or C:/John Doe. Theoretically possible but not sure how in practice.

NB: Adding the lengths of the file name and the code was somehow the shortest approach I could find to this challenge (in Python).

\$\endgroup\$
1
-2
\$\begingroup\$

Bash, 13 bytes

echo John Doe

Try it online!

echo John Doe  # full program
echo           # output...
     John Doe  # literal
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Combine your Batch and this answer, they are polygots \$\endgroup\$
    – Wasif
    Mar 3, 2021 at 9:16
  • \$\begingroup\$ I know, but they're different languages. \$\endgroup\$
    – Makonede
    Mar 3, 2021 at 18:52

Your Answer

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

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