# 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

PowerShell, 54 bytes

"$((('Programming','Puzzles'),('Code','Golf'))[!' '])" Try it online! Without spaces "$((('Programming','Puzzles'),('Code','Golf'))[!''])"

Try it online!

# T-SQL, 968281 67 bytes

print+iif(len(' x')=2,'Programming Puzzles','Code'+char(32)+'Golf')

Old version (96 bytes):

select(case'x'when(replace(' ',' ','x'))then'Programming Puzzles'else'Code'+nchar(32)+'Golf'end)
• Nice work, you can get to 67 by losing the outer parens and changing the condition to iif(len(' x')=2 Jun 21, 2017 at 21:31
• @BradC Close, but the outer paren prevents needing a space after print. I used a + instead and it worked. Jun 21, 2017 at 21:57
• Must've pasted and counted the wrong one. And I didn't know either, but the plus was the first character I tried and it worked. Jun 22, 2017 at 3:20

Simple comment abuse. (Hm, seems to confuse the syntax coloring too.)

{-- }"Code\SPGolf"--}"Programming Puzzles"
• With the spaces {-- }"Code\SPGolf"--} is a bracketed comment. Try it online!
• Without the spaces {--} is a bracketed comment and --}"ProgrammingPuzzles" an end of line comment. Try it online!

# Pushy, 47 bytes

Dpef!Hpmg KtkL9=?";
\
cProgramming Puzzles"

Try it online!

In its initial form, the top line adds the string "Code Golf" to stack, but with each character incremented, and a trailing space. It then decrements each character and checks if the length is 9 - however, as there is a trailing space, the length is 10, so the conditional is not entered. Therefore the top line is essential a no-op. The third line then prints "Programming Puzzles".

After modification, the program looks like this:

Dpef!HpmgKtkL9=?";\cProgramming Puzzles"

The \ starts a comment therefore the only relevant code is the first half, which gets the string "Code Golf", checks its length (which is now correctly 9 as the space has been removed), and then prints it accordingly.

# Kotlin, 62 bytes

Lambda returning a string.

{" ".ifEmpty{"Code\u0020Golf"}.ifBlank{"Programming Puzzles"}}

1.3 introduced ifEmpty and ifBlank which each take a lambda and return its result if the receiving string is empty or contains only whitespace. If the condition fails, the receiving string is passed on instead.

So, if the spaces are removed, then the ifEmpty check passes and the ifBlank check fails. The reverse is true if the spaces are left.

Try it online!

# MATL, 39 37 bytes

Thanks to @Sanchises for removing 2 bytes.

'Dpef!Hpmg'qc%
'Programming puzzles'&

Try it online!

## Explanation

### With spaces

'Dpef!Hpmg' pushes this string.

q substracts 1 from each code point, and c converts back to characters. This produces the string 'Code Golf'.

% is the comment symbol. The rest of the line (which is empty) is ignored.

'Programming puzzles' pushes this string.

& specifies that the display function, which is implicitly called at the end of the program, must only print the top of the stack (instead of the whole stack). So the second string is printed.

### Without spaces

'Dpef!Hpmg' pushes this string.

q substracts 1 from each code point, and c converts back to characters. This produces the string 'Code Golf'.

% is the comment symbol. The rest of the line (that is, the rest of the code) is ignored.

At the end of the program the implicit display function is called, which prints the string.

# SmileBASIC, 49 bytes

TABSTEP=5?"Code","Golf"'
CLS?"Programming Puzzles

TIO

"floG edoCselzzuP gnimmargorP"f3+40# $j$k$8k,@ Try it online! # Explanation: "floG edoCselzzuP gnimmargorP" ¤ Put "Code Golf" and "Programming Puzzles" into the stack, character by character, so that "Programming Puzzles" is on top of the stack. f3+40 ¤ Push 18, then 4, then 0 to the stack. #$                       ¤ With space: Jump over the whitespace and pop the 0.
j$k$8                    Jump over the 4 next instructions.
k,@                 Print the 18 top chars from the stack and exit.

#$¤ Without space: Jump over the $ instruction,
j                         Jump over the next 0 instructions.
$Pop 4 from the stack. k$                      Pop the top 18 chars from the stack.
8k,@                  Print out the 8 top chars from the stack and exit.

# Wren, 62 bytes

System.write(" ".count>0?"Programming Puzzles":"Code\x20Golf")

Try it online!

## Explanation

System.write(                                                ) // Output
" ".count>0                                       // Whether the length of this string is larger than 0
?"Programming Puzzles"                 // If true, output "Programming Puzzles"
:"Code\x20Golf"  // Otherwise, output "Code Golf"

# Wren, 71 bytes

System.write(Num.fromString("0 0")?"Code\x20Golf":"Programming Puzzles")

Try it online!

## Explanation

System.write(                                                          ) // Output
Num.fromString("0 0")                                       // Whether converting to a single number is successful
// If this isn't successful, it outputs null, which is a false value.
?"Code\x20Golf"                        // If this is successful, output "Code Golf"
:"Programming Puzzles"  // Otherwise, output "Programming Puzzles"

# c, 73 bytes

main(){printf("%s\n",atoi("- 1")?"Code\x20Golf":"Programming Puzzles");}
main(){printf("%s\n",atoi("-1")?"Code\x20Golf":"Programming Puzzles");}

# GolfScript, 38 bytes

" ""Programming Puzzles""Code\sGolf"if

Try it online!

# Befunge, 76 bytes

" "0#v_"floG"84*"edoC",,,,,,,,,@
,,,,,@>"selzzuP gnimmargorP",,,,,,,,,,,,,,

Try it online!

Not the most compact solution, but then again writing Befunge code without whitespace is pretty difficult.

At the very beginning, it compares a space character with 0. If it's greater, it goes to the bottom row. If it isn't, which is what happens when you replace space with nothing, it stays on the first row.

• What do the ,s do?
– user58826
Apr 28, 2017 at 21:14
• @programmer5000 Each prints one character. Apr 28, 2017 at 21:51
• A tip for printing a large number of characters up until you reach a 0 is to do >:#,_ which could save you a lot of bytes May 23, 2017 at 0:09

# Pyth, 36 bytes

J
"Programming Puzzles" "Code\40Golf

Try it online: with or without whitespace characters

That's a nice challenge for Pyth.

In the above code the newline prints Programming Puzzles and the space suppresses the output of Code Golf. J only assigns the first string to the variable J, but doesn't print anything.

After removing the newline and the spaces, the first string doesn't get printed (only assigned), but the second string gets printed implicitly.

# Ruby, 46 bytes

puts' '[0]?'Programming Puzzles':"Code\40Golf"

# Braingolf, 53 bytes [non-competing]

Holy crap I did it

48*# 1+-?"Programming Puzzles"@19%"Code"1#!-"Golf"@9|

So the issue with Braingolf is that the only conditional it has is an "is the last number on the stack greater than 0?" check, which makes this kinda challenge a little tricky, but I persevered and here it is!

Non-competing because the language was created after the date of the challenge

Explanation: Whitespace version:

48*                                                    Push the int literals 4 and 8 to the stack, then multiply them together, consuming both and adding the result to the stack (32)
#<space>                                            Add the charcode of the literal char <space> to the stack, which also happens to be 32
1+                                                Push int literal 1 to the stack, then add together the last 2 elements of the stack (1 and 32)
-                                               Subtract the last 2 elements of the stack from each other (33 - 32)
?                                              Check if the last element on the stack is greater than 0, 33-32 is 1, so it is (this also consumes the checked number from the stack)
"Programming Puzzles"                         Add the charcodes for the string "Programming Puzzles" to the stack
@19                      Print the last 19 elements of the stack as chars
%                     Else, nothing after this will run, as the ? returned true
"Code"               Push the charcodes for the string "Code" to the stack
1#!-           Push the literal int 1 and the charcode for the character '!' (33) to the stack, then subtract one from the other (33 - 1 = 32, the charcode for a space)
"Golf"     Push the charcodes for the string "Golf" to the stack
@9   Print the last 9 elements of the stack as characters
|  End if-else, at this point Braingolf would normally output the last element of the stack, as there is no semicolon in the program, however the stack is empty, so it doesn't.

No whitespace version (only the important part):

48*        Same as last time, push 4 and 8 to the stack, then multiply them to make 32
#1      This time add the charcode for the character '1' to the stack (which is 49)
+     Add the last 2 elements of the stack together (32 + 49 = 81)
-    Subtract the last 2 elements of the stack from each other, however there is only 1 element in the stack (81) so subtract it from itself, 0
?   Is the last (only) element in the stack greater than 0? (no, it is 0)
At this point the code skips every character up until the % else, and then runs from there to the end.

# Befunge-98, 51 bytes

]@,k9"Code"*84"Golf";
["selzzuP gnimmargorP">:#,_@;

Try it online!

The main idea here is to use the "turn right" operator (]) to check if there is a line below the first one. If there is, we print selzzuP gnimmargorP backwards. If the newline is removed, we turn right twice to go backwards on the top row. we use the ;s to skip the "Programming Puzzles" code and print Code Golf.

I have other ideas about how to golf this whole thing (having to do with the fact that ProgrammingPuzzles is exactly twice as long as Code Golf) but I thought I would post a simple version first, considering my other one is in progress.

Even if it's longer than the one by @nimi, here's another Haskell solution, that works by abusing the [] Monad:

λ putStr$[s|s<-[" ">>"Programming Puzzles","Code\32Golf"],s/=""]!!0 Programming Puzzles Try it online! λ putStr$[s|s<-["">>"Programming Puzzles","Code\32Golf"],s/=""]!!0

Code Golf

Try it online!

Thanks @Laikoni for golfing off 2 bytes!

INy,x:" ",Ix,,O"Programming Puzzles",INx,,O"Code\40Golf"

Try it online!

Uses a very similar technique to a lot of answers, and has the pseudocode of

x = ' '
if x:
print('Programming Puzzles')
else:
print('Code Golf')

By removing all whitespace, we make x equal to the empty string, which is falsey in Add++. Therefore, we output Code Golf instead of Programming Puzzles. The space in Code Golf is simply the octal value $$\40_8 = 32_{10}\$$. The only unusual part is the preceding INy,. With this, we are able to put all our code on one line, as this is a compound statement. Without this, the programming, with whitespace, would look something like this:

x:" "
Ix,O"Programming Puzzles"
INx,O"Code\40Golf"

But, when you remove the whitespace from this, we cause syntax errors, as outside of compound statements, Add++ has no other line terminator than a newline.

j ~"floG"32"edoC"~"selzzuP gnimmargorP"j~WW@

The first space is what's actually important. j will "jump over" the next cell in the head's direction; if the space is there it will be jumped over and the head will land on the first ~, causing it to skip the section that pushes Code Golf. When the spaces are removed, the ~ will be jumped over and the first section will be entered instead.

Try it online!

# Lua, 56 bytes

print((#' '>#'')and'Programming Puzzles'or'Code\32Golf')

Try it online!

# Keg, 35 30 bytes

‘∂⬭⑥⑷‘¶C≬;Puzz0ɧ;¶\ #ø¶[*;Golf

Without spaces:

‘∂⬭⑥⑷‘¶C≬;Puzz0ɧ;¶\#ø¶[*;Golf

## Explained

‘∂⬭⑥⑷‘¶C≬;Puzz0ɧ;¶\ #ø¶[*;Golf
‘∂⬭⑥⑷‘              #Push the string "Program"
¶C≬;Puzz0ɧ;¶     #Push the string "ing Puzzles"
\        #Escape the space and implicitly print

------------------------------------------------------------------------------

‘∂⬭⑥⑷‘¶C≬;Puzz0ɧ;¶\#ø¶[*;Golf
‘∂⬭⑥⑷‘¶C≬;Puzz0ɧ;¶\#        #Everything as before, but this time:
ø      #Empty the stack
¶[*;Golf  #Push the string "Code Golf" and print

Try it online!

### 35 bytes

∂⬭;⑥⑷;¶C≬;Puzz0ɧ;¶+\ #__¶[*;Golf¶

Real shifty string compression going on here! Try it online!

# Zsh, 45 bytes

\ ;<<<Programming\ Puzzles||<<<Code$'\40'Golf Try it online! With spaces: • \ : execute the command , (which doesn't exist so does nothing) • ;: then • <<<Programming\ Puzzles: print Programming Puzzles • || if that fails (which is never does, because printing is always successful): • <<<Code$'\40'Golf: print Code Golf (this doesn't get executed)

Without spaces:

• \;: execute the command ; (which doesn't exist so does nothing with its input)
• <<<Programming\Puzzles: and send ; the input ProgrammingPuzzles (which it discards, since it doesn't do anything)
• || if that fails (which it always does, since ; is not a defined command):
• <<<Code$'\40'Golf: print Code Golf ($'\40' is a space, written with an octal escape)

# Vyxal, 20 bytes

[↔ḃ ₄∩|¬꘍»₆½Ṅ

## How it works

•   is a string with one space, a truthy value. When spaces are removed, it becomes the empty string , which is falsy.
• [...|...] is an conditional statement. If the top of the stack is truthy, the first part is executed, otherwise the second part.
• If true (spaces are not removed): ↔ḃ ₄∩, the compressed string Programming Puzzles, is pushed and output
• Otherwise: ¬꘍»₆, the compressed string CodeGolf, is pushed. ½ splits it into the list ⟨Code|Golf⟩, which is then joined by spaces with and implicitly printed.

### Vyxal, 21 bytes, for the new site name

Additionally, here is a 21-byte solution for the new site name - Code Golf and Coding Challenges:

[¬꘍ »₆|⟩₆,→ṗ`⌐Ṅ

It works similarly, using ⌐Ṅ to replace the comma in compressed Coding,Challenges with a space.

# Microscript, 48 bytes

' d32{"floG"z32s"edoC"ah}"selzzuP gnimmargorP"ah

Or, which is the same length:

" "#{o"selzzuP gnimmargorP"ah}"floG"z32s"edoC"ah

Or:

' s#{o"selzzuP gnimmargorP"ah}"floG"z32s"edoC"ah

# Microscript II, 45 bytes

" "("Programming Puzzles"ph)"Code"p32Kp"Golf"

Or, which is one byte longer:

' s#("Programming Puzzles"ph)"Code"p32Kp"Golf"

# Pip, 37 bytes

"Programming Puzzles"  "Code0Golf"R0s

Try it online!

### Explanation

Comment abuse.

Anything following two spaces in Pip is a comment.* So with the spaces, this is just the string expression "Programming Puzzles", which is autoprinted. Without the spaces, the first expression doesn't do anything, and we instead autoprint "Code0Golf"R0s, which replaces the 0 in Code0Golf with a space.

My original idea, ' ?"Programming Puzzles""Code0Golf"R0s (fun with character literals), was 1 byte longer.

* Spaces at the beginning of a line don't count.

## Ruby, 56 bytes

puts"Code\x20Golf"if(" "=="")#
puts"Programming Puzzles"

The second string literal should contain a tab.

• You can save a fair number of bytes if you change !(" ">" ") to " "=="". That should bring it to 56 bytes, by my count. May 4, 2017 at 5:16

# Petit Computer BASIC, 40 bytes

?"Code","Golf"'
CLS?"Programming Puzzles

The original program prints Code Golf and then clears the screen and prints Programming Puzzles. When whitespace is removed it becomes ?"Code","Golf"'CLS?"Programming Puzzles (' starts a comment), so only Code Golf is printed.