# From Programming Puzzles to Code Golf

Your task is to write a program that outputs the exact string Programming Puzzles (trailing newline optional), but when all spaces, tabs, and newlines are removed it outputs Code Golf (trailing newline optional.)

Your byte count is the count of the first program, with spaces still there.

# Notes

• The spaces in Code Golf and Programming Puzzles will be removed as part of the removal, so plan accordingly.

• In encodings where 0x09, 0x0A and 0x20 aren't tabs, newlines or spaces respectively, those chars will be removed.

42  $@ rw$
@42


then that must output Programming Puzzles. Also, in the same language,

42$@rw$@42


must output Code Golf.

This is , so the shortest code in bytes wins! Good luck!

• will the space in Code Golf also be removed, what about the one in Programming Puzzles. Apr 28, 2017 at 13:20
• This will be impossible in at least Whirl and Whitespace. Apr 28, 2017 at 17:14
• What are the close votes for? Apr 28, 2017 at 21:16
• @OldBunny2800 good question. I keep asking that but the comment gets removed...
– user58826
Apr 28, 2017 at 21:19
• The close votes are for the lack of inputs and outputs and restriction on the formatting of inputs and outputs. 100% if you had a section labeled "rules" with a bullet-pointed list of things you think were obvious about the challenge you'd have 0 close votes. It's all about presentation with CompSci folks, if we can think of a seemingly idiotic question to ask that may save us a byte or to, we will, try to counter that idiocy and you will be a great question writer. Apr 28, 2017 at 22:00

# Python 2, 50 bytes

print["Code\40Golf","Programming Puzzles"][" ">""]


Try it online!

With all spaces removed:

print["Code\40Golf","ProgrammingPuzzles"]["">""]


Try that online!

Thanks to Stephen S for 3 bytes, and Erik the Outgolfer for 1

• Darn, ninja'd! I was just about to click Post Answer! +1 Apr 28, 2017 at 13:22
• I think you just broke my friend's brain. How does this even work? Apr 28, 2017 at 15:09
• @StevenVascellaro It's really simple. In the first case, " ">"" returns True, since, lexicographically, a space is greater than the empty string. In the second case, "">"" returns False, since nothing can be greater than itself. True and False are actually just 1 and 0 respectively, just in the bool datatype instead of int or long. In the first case, the spaces are preserved, so, the item at index 1, "Programming Puzzles", is returned verbatim. In the second case, the spaces are gone, hence the \x20 in the item at index 0 "Code\x20Golf" to preserve a space. Apr 28, 2017 at 15:26
• @StephenS Nope, because unlike JavaScript, Python doesn't have an obsession with implicit casting.
– user45941
Apr 28, 2017 at 15:57
• If find Python's lack of obsession disturbing. Apr 28, 2017 at 19:56

# Python 2, 48 47 bytes

-1 byte thanks to Erik the Outgolfer

print' 'and'Programming Puzzles'or'Code\40Golf'


Try it online!

print''and'ProgrammingPuzzles'or'Code\40Golf'


Try it online!

• Aww, I was just about to improve my answer to this...
– user45941
Apr 28, 2017 at 13:29

# Jelly, 17 bytes

“Ñ1ɦ+£)G“½ċṭ6 Ỵ»Ṃ


Try it online!

### How it works

As in the other Jelly answer, Ñ1ɦ+£)G and ½ċṭ6Ỵ encode the strings Programming Puzzles and Code Golf. “ begins a string literal and separates one string form another, while » selects the kind of literal (dictionary-based compression), so this yields

["Programming Puzzles", "Code Golf"]


Ṃ then takes the minimum, yielding Code Golf.

However, by adding a space between ½ċṭ6 and Ỵ, we get a completely different second string, and the literal evaluates to

["Programming Puzzles", "caird coinheringaahing"]


Since caird coinheringaahing is lexicographically larger than Programming Puzzles, Ṃ selects the first string instead.

• Just did the same with “½ċṭ6Ỵ“Ñ1ɦ +£)G»Ṁ Apr 29, 2017 at 8:00
• To my knowledge neither of those characters can be saved using one byte, assuming well known encodings. in UTF-8 I get 34 bytes... Am I wrong? May 4, 2017 at 19:55
• @steffen Jelly uses a custom code page that encodes each of the 256 characters it understands as a single byte. May 4, 2017 at 21:48
• @Dennis thank you for clarifying. May 5, 2017 at 9:30
• Where it all started. Oct 10, 2020 at 4:34

# C, 646253 52 bytes

f(){puts(*" "?"Programming Puzzles":"Code\40Golf");}


Try it Online!

Uses the fact that C strings end with a null character

# 05AB1E, 15 bytes

”ƒËŠˆ”" "v”–±ÇÀ


Try it online!

Explanation

”ƒËŠˆ”           # push "Code Golf"
" "v       # for each character in the string " " do
”–±ÇÀ  # push "Programming Puzzles"
# implicitly output top of stack

• ...I just don't get it. Does this use dictionary compression or something? Apr 30, 2017 at 2:15
• @LegionMammal978 I'm pretty sure it does. Apr 30, 2017 at 3:15
• @LegionMammal978: It does indeed use dictionary compression. Apr 30, 2017 at 8:59
• @Emigna Okay, because last I checked, neither of those strings could be fit into 4 bytes :p Apr 30, 2017 at 10:27
• @Draco18s: Kevin has written a nice tutorial over at the tips page you can use to get a better idea of how to use it. Jan 9, 2019 at 17:54

# CJam, 38 bytes

" ""Programming Puzzles""Dpef!Hpmg":(?


Explanation

" "                    e# Push this string.
"Programming Puzzles"  e# Push "Programming Puzzles".
"Dpef!Hpmg":(          e# Push "Dpef!Hpmg" and decrement each char, giving "Code Golf".
?                      e# If the first string is true (non-empty) return the second string,
e#   else return the third.


Whether spaces are in the program or not determines if the first string is truthy or falsy.

• Your code is sad :( Apr 28, 2017 at 13:51
• If you're willing to use unprintables, "Bncd\x19Fnke":) is happy code instead (replace \x19). Apr 28, 2017 at 15:58

# Jelly, 18 bytes

“½ċṭ6Ỵ»ḷ
“Ñ1ɦ+£)G»


Try it online!

## Explanation

In the program as written, the first line is a helper function that's never run. The second line (the last in the program) is the main program, and is the compressed representation of the string "Programming Puzzles" (which is then printed implicitly).

If you remove the newline, the whole thing becomes one large program. “½ċṭ6Ỵ» is the compressed representation of the string "Code Golf". ḷ evaluates but ignores its right hand argument, leaving the same value as before it ran; it can be used to evaluate functions for their side effects (something I've done before now), but it can also be used, as here, to effectively comment out code.

Interestingly, the actual logic here is shorter than the 05AB1E entry, but the code as a whole comes out longer because the string compressor is less good at compressing these particular strings.

• This turns out to be valid. Apr 28, 2017 at 15:34
• To my knowledge some of those characters can be saved using one byte, assuming well known encodings. in UTF-8 I get 36 bytes... Am I wrong? May 4, 2017 at 19:59
• @steffen: Jelly uses its own character encoding, in which all of the 256 different characters it uses can be stored in a single byte. (The only reason it does this rather than using a pre-existing encoding is for readability (!); you could trivially write the program encoded in, say, codepage 437, and it would run in the Jelly interpreter, but it would typically be much harder to read.)
– user62131
May 4, 2017 at 21:12

# Javascript, 4643 42 Bytes

x=>" "?"Programming Puzzles":"Code\40Golf"


console.log((x=>" "?"Programming Puzzles":"Code\40Golf")())
console.log((x=>""?"ProgrammingPuzzles":"Code\40Golf")())

• You can replace the \x20 in the first string with a space. Apr 28, 2017 at 13:30
• Beat me to it, nicely done. Does this need a trailing ;? Apr 28, 2017 at 15:22
• @ricdesi no, it dosen't.
– user68614
Apr 28, 2017 at 15:31
• @ricdesi since this is codegolf, if the program runs, it works. ;s are sometimes not required in JavaScript. Apr 28, 2017 at 15:52
• You can replace \x20 with \40 to save a byte :-) Apr 28, 2017 at 18:32

a _="Programming Puzzles";a4="Code\32Golf";f=a 4
a_="ProgrammingPuzzles";a4="Code\32Golf";f=a4


Defines function f which returns the corresponding string.

For reference, the old version is:

f""="Code\32Golf";f(_)="Programming Puzzles";f" "


with spaces removed

f""="Code\32Golf";f(_)="ProgrammingPuzzles";f""


Two simple pattern matches. (_) matches all patterns. You can improve the without-spaces version by one byte by defining the second pattern as f" "=/f""=, but this gives a "Pattern match is redundant" warning.

Alternative solution with the same byte count:

last$"Code\32Golf":["Programming Puzzles"|" ">""] last$"Code\32Golf":["ProgrammingPuzzles"|"">""]


## Wolfram language, 62 bytes

"Programming Puzzles"[ToExpression@"\"Code\\.20Golf\""][[0 1]]


The space in [[0 1]] is implicit multiplication, so this is equivalent to [[0]]. Without a space, 01 is just 1. The 0th and 1st parts of this expression are the strings we want.

Another solution of questionable validity (44 bytes, 2 saved by Kelly Lowder):

"Programming Puzzles"["Code\.20Golf"][[0 1]]


The \.20 gets replaced by a space immediately when typed into a Mathematica environment, so it's not clear if it gets removed along with the other spaces…

• As soon as you paste (or type) this into Mathematica, (cloud based or not) the escape sequence, \:0020 turns into a space, and thus would be removed, so I'm not sure this qualifies. Also \.20 is shorter.by two characters. "Programming Puzzles"["Code"<>FromCharacterCode@32<>"Golf"][[01]] will work. Apr 28, 2017 at 20:12
• @KellyLowder, hmm, that's a good point. I've added another solution that should avoid that issue. (Thanks for the \.20 suggestion — how did you find that? I thought I'd scoured the whole documentation…) Apr 28, 2017 at 22:56
• I found the \.20 by making a typo. Seems to only work for two-digit character codes. I don't think it's in the documentation. May 1, 2017 at 13:48

## Excel - 56 Bytes

=IF(""=" ","Code"&CHAR(32)&"Golf","Programming Puzzles")


Very similar to most of the other answers... nothing fancy here.

# Ohm, 33 32 bytes

Uses CP437 encoding.

▀Bn¬i≈╣Ü╝Rb╡°╧S½uÇ▀
▀4>~H├MS░l╬δ


### Explanation

With whitespace:

▀Bn¬i≈╣Ü╝Rb╡°╧S½uÇ▀    Main wire

▀Bn¬i≈╣Ü╝Rb╡°╧S½uÇ▀    Push "Programming Puzzles" (compressed string)
Implicitly output the top stack element

▀4>~H├MS░l╬δ           Helper wire (never called)


Without whitespace:

▀Bn¬i≈╣Ü╝Rb╡°╧S½uÇ▀▀4>~H├MS░l╬δ    Main wire

▀Bn¬i≈╣Ü╝Rb╡°╧S½uÇ▀                Push "Programming Puzzles" (compressed string)
▀4>~H├MS░l╬δ    Push "Code Golf" (compressed string)
Implicitly output top stack element

• I'm so glad I finally finished string compression! May 2, 2017 at 20:01
• @NickClifford The curious thing I noticed was that Code Golf got longer when compressed. I guess that's because of the capitals? Either way it's still shorter than writing it normally because I can't use a literal space here. May 2, 2017 at 20:05
• Yeah, Smaz is kind of weird like that. May 2, 2017 at 20:07
• FYI, feel free to ping me in chat if you have any questions or feature requests for Ohm. May 2, 2017 at 20:18

# Japt, 29 bytes

With spaces:

Co¸{S}Golfr PžgŸmÚÁ Puzz¤s


Try it online!

Without spaces:

Co¸{S}GolfrPžgŸmÚÁPuzz¤s


Try it online!

This takes advantage of the fact that in Japt, a space closes a method call. With spaces, the code is roughly equivalent to this JavaScript code:

("Code"+(S)+"Golf").r(),"Programming Puzzles"


This is evaluated as JavaScript, and the result is sent to STDOUT. Since the last expression is "Programming Puzzles", that string is printed.

Without spaces, the code is roughly equivalent to:

("Code"+(S)+"Golf").r("ProgrammingPuzzles")


(If you haven't figured it out by now, the S variable is a single space.) The .r() method on a string, if given one argument, removes all instances of that argument from the string. Since "Code Golf" does not contain "ProgrammingPuzzles", it returns "Code Golf" unchanged, which is then sent to STDOUT.

• I didn't even think of using replace. Nice one!
– Tom
Apr 28, 2017 at 14:39
• To my knowledge some of those characters can be saved using one byte, assuming well known encodings. in UTF-8 I get 36 bytes... Am I wrong? May 4, 2017 at 19:57
• @steffen Japt uses the ISO-8859-1 encoding, in which each char represents one byte. But some of the chars would be unprintable in this program, so I used the Windows-1252 encoding here (actually, it was autogenerated by TIO) May 4, 2017 at 20:00

# Brachylog, 44 bytes

" "Ṣ∧"Programming Puzzles"w|"Code"wṢw"Golf"w


Try it online!

### Explanation

" "Ṣ                                           If " " = Ṣ (which is itself " ")
∧"Programming Puzzles"w                    Write "Programming Puzzles"
|                   Else
"Code"w            Write "Code"
Ṣw          Write " "
"Golf"w   Write "Golf"

• Crossed out 44 is still 44 :( Edit in &nbsp; on either side to fix :) Apr 28, 2017 at 13:41
• @HyperNeutrino It's not crossed out, 44 is the length with spaces, and 42 without. Apr 28, 2017 at 13:41
• Oh. Whoops. Well, it's too similar so I couldn't tell without looking at the markdown by going into edit. Never mind :P Apr 28, 2017 at 13:42

## Alice, 44 bytes

/"floG!"t"edoC"#
/"selzzuP gnimmargorP"d&o@


Try it online!

Without whitespace:

/"floG!"t"edoC"#/"selzzuPgnimmargorP"d&o@


Try it online!

### Explanation

In the first program, the two mirrors / redirect the instruction pointer onto the second line. "selzzuP gnimmargorP" pushes the required code points in revere order, d pushes the stack depth and &o prints that many bytes. @ terminates the program.

Without the whitespace, the program is all on a single line. In that case, the instruction pointer can't move in Ordinal mode, so the / effectively become no-ops (technically, the IP simply doesn't move for one step, the same / gets executed again, and the IP reflects back to Cardinal mode). So if we drop those from the program, it looks like this:

"floG!"t"edoC"#"selzzuPgnimmargorP"d&o@


To include the space in Code Golf, we use an exclamation mark instead and decrement it with t. After we've got all the code points on the stack, # skips the next command, which is the entire second string. d&o then prints the stack again, and @ terminates the program.

• You must golf this (crossed out 44 is 44) Jan 11, 2019 at 1:55

# PHP, 44 Bytes

ternary operator

<?=" "?"Programming Puzzles":"Code\x20Golf";


## PHP, 51 Bytes

comment

<?=/** /"Code\x20Golf"/*/"Programming Puzzles"/**/;


## PHP, 57 Bytes

array switch

<?=["Code\x20Golf","Programming Puzzles"][(ord("
")/10)];


# Common Lisp (SBCL), 52 bytes

(format,t"~[Programming Puzzles~;Code~@TGolf~]"0 1)


Prints Programming Puzzles

(format,t"~[ProgrammingPuzzles~;Code~@TGolf~]"01)


Prints Code Golf

Ungolfed:

(format t "~[Programming Puzzles~;Code Golf~]" 0 1)


Explaination:

The trick basically comes from how #'format works in Common Lisp.

In CL, most whitespace can be omitted provided that there is no ambiguity about where tokens start or end. The first trick was separating the format and t symbols. I had to unambiguously end the format symbol without changing how t was interpreted. Luckily,  in CL ends the preceding token before it gets processed, and , cancels the effect of  ( is used to implement templating, where the next expression following it gets "quoted", but any sub-expression prefixed with a , is evaluated and the result included in the template, so , is nearly a no-op).

The third argument to format is the template string. format is similar to printf in C, but has much more powerful formatting directives and use ~ to indicate them instead of %. ~[ and ~] allow you to select between multiple options for printing, with ~; separating them. An additional argument is supplied to format- the numeric index of which one you want printed. In order to ensure that the " " in Code Golf survives, I used the tabulation directive ~T, which is used to insert whitespace, generally to align text into columns. ~@T is a variation which just inserts a given number of spaces, defaulting to 1.

Finally, there are two arguments to format- 0 and 1. Before whitespace is removed, the 0 is used by ~[~;~] to select "Programming Puzzles" and the extra format argument (the 1) is dropped (I'm not sure how standard dropping extra format arguments is, but this works on Steel Bank Common Lisp). After whitespace is removed, there is only one argument (01) which selects "Code Golf" instead.

• +1, just one thing: "but any sub-expression prefixed with a , is evaluated and spliced in" Isn't ,@ necessary for splicing?
– user65167
May 3, 2017 at 15:46
• "spliced" is a poor term for that, admittedly. Technically, , evals the next expression and includes the result in the template, while ,@ assumes the expression will eval to a list and includes that list's contents in the template directly. Traditionally in the lisp community, ,@ is called "splicing", but I'm not sure that's the most obvious choice. I'll try to reword it a bit. May 3, 2017 at 17:04

# Perl 5, 43 41 bytes

say" "?"Programming Puzzles":Code.$".Golf  Try it online! Uses the fact that ' ' is true in perl and '' is false. The $" variable is set to a space by default.

Thanks to @NahuelFouilleul for removing two bytes.

• maybe late but "Code$\"Golf" is shorter and Code.$".Golf too Jan 9, 2019 at 13:09
• @NahuelFouilleul Yeah, I was pretty new when I wrote this answer. Thanks though! Jan 9, 2019 at 17:19

# R, 50 bytes

I think this is the same as this Javascript answer, and basically the same idea as all the others.

if(' '=='','Code\x20Golf','Programming Puzzles')

• 49 bytes Nov 8, 2019 at 11:48

# Pyth, 37 bytes

?" ""Programming Puzzles""Code\40Golf


Try it here.

?"""ProgrammingPuzzles""Code\40Golf


Try it here.

# ><>, 42 bytes

With whitespace

\"floG"48*"edoC"
/>o<"Programming Puzzles"


Try it online!

Without whitespace

\"floG"48*"edoC"/>o<"ProgrammingPuzzles"


Try it online!

# dc, 50

[pq]sm[Programming Puzzles]dZ19=m[Code]n32P[Golf]p


[ ] defines a string - Z measures its length. If the length is 19 then it contains a space and the macro stored in the m register is called, which prints Programming Puzzles and quits. Otherwise Code Golf is printed.

• Could you link to the dc interpreter / docs / github?
– user58826
Apr 29, 2017 at 1:14
• @programmer5000 Just choose dc on TIO, then click the language name. Apr 29, 2017 at 1:15
• @Dennis thanks, I didn't even know that TIO does that!
– user58826
Apr 29, 2017 at 1:18

# C# 888170 63 bytes

Func<string>@a=()=>" "==""?"Code\x20Golf":"Programming Puzzles";


With whitespace stripped:

Func<string>@a=()=>""==""?"Code\x20Golf":"ProgrammingPuzzles";


Thanks to BrianJ for showing me how to have no space between a method return type and the method name, PeterTaylor for saving 7 18 bytes, and Patrick Huizinga for saving 7 bytes.

Same method as everyone else really, technically this could be considered invalid because the method doesn't specify a return type for the method, but there has to be whitespace between the return type and the method name.

• you can prefix the function name with an @, so then you have void@a... and avoid the "no return type" compilation error (also adds bytes, though :( ) Apr 28, 2017 at 15:36
• .Length<1 saves one byte; \u0020 saves six if I've counted correctly; and you can make a valid answer (and save a few bytes at the same time) by submitting a lambda instead of a top level function. Func<string>a=()=>... Apr 28, 2017 at 15:37
• @BrianJ Hmm, didn't know that, wonder why that works from a compiler view-point. Anyway, it may lose bytes but it also technically makes this answer not non-competing, so it's worth it. Thanks! Apr 28, 2017 at 15:38
• @Mayube the @ is primarily used if you need to use a reserved word as a variable name (the equivalent is surrounding with [] in VB.NET (and MS SQL Server)). Apr 28, 2017 at 15:41
• Yes, it requires returning the string; but if you use => instead of return you don't need any spaces. (And even if you use return, you can return(...)). Apr 28, 2017 at 15:47

# Java 8, 7450 48 bytes

()=>" "==""?"Code\040Golf":"Programming Puzzles"

• @NonlinearFruit you're right, I've changed that one to be non-competent May 1, 2017 at 18:50
• I don't think printing is a requirement, so you could just return the string. I haven't tested it but the == operator should also work, ()=>""==""?"Code\u00A0Golf":"Programming Puzzles" May 1, 2017 at 19:59
• \u00A0 -> \040 for a 2 byte savings.
– Poke
May 3, 2017 at 18:47

# ><>, 47 45 bytes

! v"floG!"1-"edoC"!
o<>"selzzuP gnimmargorP">


Try it online!

Thanks to randomra for -2 (clever two !s so that I can use only one >o<.)

The code shouts "Flog! Flog! Flog!" and resembes a fish.

# Befunge, 47 bytes

# <"floG"84*"edoC">:#,_@#" Programming Puzzles"


Try it Online

Without spaces it skip the first < and prints Code Golf instead

# APL (Dyalog Unicode), ₄₅₄₁ 39 bytesSBCS

Anonymous lambda. Takes dummy argument.

{'Programming Puzzles'
⊢4⌽9↑'GolfCode'}


Try it online!

This function is a so called "dfn" which terminates with and returns the first non-assignment, i.e. Programming Puzzles.

Removing all whitespace gives:

{'ProgrammingPuzzles'⊢4⌽9↑'GolfCode'}


Try it online!

This takes the last nine characters of GolfCode thus padding with a trailing space, then cyclically rotates that 4 steps to the left. ⊢ ignores the text on its left and returns the text on its right.

# Python 3, 7268 bytes

Had output backwards at first, thanks Draco18s, then saved 4 bytes, thanks Jo King

a=0;ab="Programming";a


Try it online!

• I think you have the outputs backwards. Nov 11, 2019 at 20:56
• Whoops lol, thanks. Fixed it Nov 13, 2019 at 16:45
• You can reuse the a to get 68 bytes
– Jo King
Nov 16, 2019 at 2:56

# Japt, 32 bytes

" "?PžgŸmÚÁ Puzz¤s:Co¸{S}Golf


Try it online!

• Heh, I was just about to post the same thing :-) Apr 28, 2017 at 14:30
• Actually, there is a shorter way... Apr 28, 2017 at 14:35

# PowerShell, 56 bytes

('Programming Puzzles',('Code'+[char]32+'Golf'))[!(' ')]
`

Try it online!

Pretty basic I would say, but it gets the job done