19
\$\begingroup\$

In particular, use each of these symbols at least once in your source code:

! " # $ % & ' () * + , - .  / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

Symbols inside comments, string literals, regexps (or any other kinds of literals etc.) don't count (but their delimiters such as /**/ or "" do count).

The program should not perform any action. It just has to compile and do nothing when run.

If for some reason some of the symbols cannot be used in the language of your choice, explain that rigorously (what and why must be excluded).

Update: A few answers used symbols withing regular expressions. I'd consider this a bit problematic, it's the same thing as putting them into string literals or comments (that's why I put etc. in that requirement). Please try without this. I also updated the requirement above.

Update: Shortest code wins (tagged as code-golf). As suggested, we'll most likely need some tie-breaker criteria. I suggest that if there is a tie, the winning one is the one in which the ASCII symbols appear as much ordered as possible. Formally: Filter out the first occurrence of each of the listed symbols from a program. This will result in a permutation on the listed symbols. The program with less inversion number of its permutation wins.

Update: I'd be happy to see some/more solutions in regular/mainstream languages, such as C(++), Java, Scala, Haskell, etc.

\$\endgroup\$
12
  • 1
    \$\begingroup\$ Can we have a literals exception for characters that do not appear in any keyword, syntax or operator in a particular language? That is neither @% nor the backtick can appear in c except as part of a literal (in sufficiently new versions of c % can appear in digraphs). (and # only appears in preprocessor directives, but we'll just have to live with that)? That is, I'm asking for Matt's approach to be made explicitly legal. \$\endgroup\$ Nov 7, 2012 at 21:26
  • 2
    \$\begingroup\$ shortest code wins? i suspect there will need to be a tie-breaking criteria too \$\endgroup\$
    – ardnew
    Nov 7, 2012 at 22:52
  • 1
    \$\begingroup\$ In Javascript, the characters #, @, and ` are not legal outside of any sort of literal \$\endgroup\$ Nov 8, 2012 at 16:13
  • 6
    \$\begingroup\$ This is basically a competition of "find a language that can use all of these characters as syntax". Pretty crappy code gold in my opinion \$\endgroup\$
    – Earlz
    Nov 8, 2012 at 16:34
  • 1
    \$\begingroup\$ Yea I agree with @Earlz, I think it would be better if it had to do something useful, in the lease amount of code, that way people couldn't just fluff to get all the characters in but it wouldn't be ardnew's solution. Cause as it stands you can't get shorter then that, plus any language in which some of the characters are not valid outside of literals - as has been pointed out by several people - are instantly disqualified. Which means there are only a few not only acceptable answers but correct answers period. \$\endgroup\$
    – Ryan
    Nov 8, 2012 at 20:41

24 Answers 24

50
\$\begingroup\$

Perl, 32 chars

no hiding symbols in comments, string literals, or regular expressions. uses all symbols precisely once.

$`<@"^[(\{_},)]/+-~%'?&|:!*.=>;#

Description

i have tried to indicate operator precedence with indentation: the right-most expressions are evaluated before those to their left

        $`            # scalar variable $ named `
      <               # numeric less-than inequality <
        @"            # array variable @ named "
                      #   (array evaluated in scalar context)
    ^                 # bitwise XOR ^
        [(\{_},)]     # array reference [] 
                      #   containing array () 
                      #     containing reference \
                      #       to anonymous hash reference {}
                      #         with one key "_" 
                      #           and no value (nothing following comma ,)
                      #             (reference evaluated in scalar context)
      /               # numeric division /
        +-~%'         # unary addition +
                      #   and unary negation -
                      #     and bitwise complement ~
                      #       on hash variable % named '
                      #         (hash evaluated in scalar context)
  ?                   # ternary conditional operator ?:
    &|                # code reference variable & named |
  :                   # ternary conditional operator ?:
    ! *.              # logical negation !
                      #   on typeglob variable * named .
                      #     (typeglob evaluated in scalar context)
  =>                  # fat comma => for compound expression
                      #   (evaluates LHS as string, RHS is empty)
;                     # end of expression ;
#                     # start of comment #
\$\endgroup\$
5
  • \$\begingroup\$ Correct me if I'm wrong, but did you just make a scalar called `<@"^[(\{_},)]/+-~%'?&|:!*.=>? \$\endgroup\$
    – Mr. Llama
    Nov 8, 2012 at 17:25
  • 2
    \$\begingroup\$ @GigaWatt no, this is a sequence of several different expressions. i will add a description of how it is evaluated \$\endgroup\$
    – ardnew
    Nov 8, 2012 at 18:37
  • \$\begingroup\$ @GigaWatt: No; in Perl, the symbolic identifiers are all single characters. Of that sort of thing, I think this program only uses $`, @", %', and *. (a scalar, array, hash, and typeglob, respectively), though it's awfully hard to be sure . . . \$\endgroup\$
    – ruakh
    Nov 8, 2012 at 18:42
  • 24
    \$\begingroup\$ Either you're a genius, or any random byte sequence is a valid Perl program. Or both. \$\endgroup\$
    – ugoren
    Nov 8, 2012 at 20:37
  • 4
    \$\begingroup\$ Although according to my own rules I had to award the Brainfuck solution, this one is my favorite (same size, but inversion number 279). \$\endgroup\$
    – Petr
    Nov 10, 2012 at 10:22
18
\$\begingroup\$

Brainf*ck, 32 characters

!"#$%&'()*+.-/:;>=<?@[\]^_`{|}~,

Prints a smiley face (if your console can output ASCII symbol 1)

Yay, a Branf*ck solution that beats Golfscript! :P

If you insist on no output, just exchange the + and . :

!"#$%&'()*.+-/:;>=<?@[\]^_`{|}~,
\$\endgroup\$
8
  • 6
    \$\begingroup\$ Hm... I'd rather say it does hide characters in comments, but oh well. \$\endgroup\$ Nov 8, 2012 at 14:17
  • 6
    \$\begingroup\$ @leftaroundabout : They are not technically comments because you can't use them to comment out valid instructions. They are handled as whitespace. By the way, I agree with you that this is a shameless stretching of the rules. \$\endgroup\$
    – vsz
    Nov 8, 2012 at 15:54
  • 3
    \$\begingroup\$ @vsz There's nothing wrong with bending the rules. \$\endgroup\$
    – Gareth
    Nov 8, 2012 at 17:18
  • 1
    \$\begingroup\$ Inversion number of this solution is 24, so it's the winner according to the rules. I have some doubts about that, as ignoring characters is very close to comments, but I'd say it's my bad, I didn't think of this possibility when writing the question. \$\endgroup\$
    – Petr
    Nov 10, 2012 at 10:18
  • 1
    \$\begingroup\$ @BodoThiesen Indeed that's quite old, I guess that might also qualify for a winning solution, although this one is better, even though it stretches the rules a little. \$\endgroup\$
    – Petr
    Dec 5, 2016 at 19:20
10
\$\begingroup\$

Whitespace, 32

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

all non whitespace characters are ignored, so this is actually an empty program

\$\endgroup\$
1
  • 3
    \$\begingroup\$ one could argue therefor that all non-whitespace characters are comments and this is invalid :) \$\endgroup\$
    – NRGdallas
    Jan 11, 2013 at 21:34
8
\$\begingroup\$

Python 65

a=lambda x:0.
@a#
def _():{[`'"$?'`,]};a<a!=a+a%a&a^a-a*a|~a>a/a\

I used the characters ? and $ inside a string literal. These characters are not legal outside string literals or comments.

\$\endgroup\$
3
  • \$\begingroup\$ This contains invalid syntax. Specifically, {[`'"$?'`,]} isn't valid, as it creates a dictionary with a key with no associated value. You can make it valid with two more characters like so: {[`'"$?'`,]:0} \$\endgroup\$
    – Strigoides
    Nov 10, 2012 at 14:47
  • 1
    \$\begingroup\$ @Strigoides nope, it makes a set not a dict. but even if that was the case (dict key with no value) it runs, which was the requirement, not valid syntax. (you need python >= 2.7 for it to work) \$\endgroup\$
    – Matt
    Nov 10, 2012 at 18:33
  • \$\begingroup\$ I see, I was using python 2.6 when I tested it. \$\endgroup\$
    – Strigoides
    Nov 11, 2012 at 10:11
8
\$\begingroup\$

PHP, 32 chars

Probably a bit of a troll, this PHP program is also a QUINE, and uses all symbols in the exact order they are listed.

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

Executed from the command line, the output will simply be the contents of the file, as there is no <?php to invoke the interpretor.

$ cat > a.php
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

$ php a.php
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
\$\endgroup\$
4
  • 4
    \$\begingroup\$ IMHO this violates Symbols inside comments, string literals, regexps (or any other kinds of literals etc.) don't count, as the whole program is a single literal. \$\endgroup\$
    – Petr
    Nov 8, 2012 at 17:10
  • \$\begingroup\$ Also, the program required by the task should output nothing, and there is only one quine which outputs nothing. \$\endgroup\$ Dec 8, 2016 at 21:15
  • \$\begingroup\$ It doesn't even get parsed by PHP so this is pretty much invalid \$\endgroup\$
    – Rob
    May 28, 2018 at 12:25
  • \$\begingroup\$ @RobQuist of course it gets parsed, it's busy hunting that elusive T_OPEN_TAG token right til the bitter end \$\endgroup\$
    – Leigh
    May 29, 2018 at 10:11
7
\$\begingroup\$

Perl 6 (43 chars)

Let's define a infix operator with a rather unusual name:

sub infix:<!"#$%&'()*+,-./:;=?@[]^_´\|~>{}

It's not a literal nor a literal in a RE or anything hidden in a comment. You don't believe me? Running the following program will output "2":

sub infix:<!"#$%&'()*+,-./:;=?@[]^_´\|~>($a, $b){ $a + $b }
say 1 !"#$%&'()*+,-./:;=?@[]^_´\|~ 1;

But I'm quite sure you nitpickers will extend the rules further to exclude redefinition of the language itself to make us perl lubbers unhappy. :(

\$\endgroup\$
1
  • \$\begingroup\$ I'd say this is ok, using symbols in an identifier seems reasonable. \$\endgroup\$
    – Petr
    Nov 8, 2012 at 5:28
7
\$\begingroup\$

Haskell, 53

a@""#%&*+/?^b|a'<-a!!0=(\_->a`b`[]);main=do{print$1}

We can make this a bit shorter

45

a'@""#%&*+/?!<|^b=(\_->1`b`[]);main=do{main}

however this program doesn't terminate, and uses more characters ('!','<','|') in the newly-defined infix.

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

k (33 chars)

Very easy in k. Unfortunately " must be paired.

(@:'!""#$%&*+-\./;<=>?^_{}[`]|,~)
\$\endgroup\$
5
\$\begingroup\$

GolfScript (33 chars)

{!""$%&()*+,-./<=>?@[\]^_`|~};:'#

" has to be paired. It took a bit of trial and error to get it to output nothing - most other positions of the :' result in some output.

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

J, 33 characters

''&][?!"$>{}:<;,|^~*/\%+`-@.#(=_)

' needs to be doubled to avoid syntax errors.

My first answer was missing some symbols.

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

C, 48 chars

Kind of cheating.

#define _ ~''*+,-./:<=>?[]`|\
@%^$&!
main(){"";}
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Definitely pushing the bounds of "cheating" as most of them appear in an uncalled macro. How about #define Q(_) "##_##" ... char*c=Q(~''*+,-./:<=>?[]|\);`. Nothing in a string, it just gets converted to one... It would be longer, of course. \$\endgroup\$ Nov 8, 2012 at 15:49
  • \$\begingroup\$ @dmckee, I don't think your suggestion is more honest than mine. In both cases, the problematic characters don't end up in code. I could move most of them to code (as I did with "";), but $@ and backtick seem hopeless. \$\endgroup\$
    – ugoren
    Nov 8, 2012 at 16:06
  • \$\begingroup\$ No doubt about it, mine is also foul, underhanded and sneaky. But it's fun, too. \$\endgroup\$ Nov 8, 2012 at 16:20
  • \$\begingroup\$ @dmckee, fun is what it's about. BTW, you need #define Q(_) #_. The preprocessor would treat "##_##" as a string literal. \$\endgroup\$
    – ugoren
    Nov 8, 2012 at 21:32
5
\$\begingroup\$

Postscript - 32 chars

{!"#$&'()*+,-./:;=<>?@[\]^_`|~}%

Results in an object on the stack, but otherwise has no effects. Postscript allows almost any char in an identifier except for <>()[]{}/ which are special syntax forms and % which introduces a comment. The program is tokenized as

{                  % start a procedure object
!"#$&'             % an identifier
()                 % an empty string
*+,-.              % an identifier
/:;=               % a symbol
<>                 % an empty hex string
?@                 % an identifier
[\]                % an array
^_`~|              % an identifier
}                  % close the procedure object
%                  % a comment
\$\endgroup\$
3
\$\begingroup\$

C# (91 characters)

#define a 
class a{static int @Main(){return(char)1.1*""[0]/1%1-1+1!=1?1:~1^1>>1<<1&1+'\0';}}

As far as I know, The $ character is not a valid character outside string literals, so I didn't use it in my answer.

EDIT : Thanks to Peter Taylor, I shortened the code.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ #if A...#endif is shorter than #region...#endregion. And "x"[0]+ is shorter than [System.STAThread]. \$\endgroup\$ Nov 8, 2012 at 18:13
3
\$\begingroup\$

PowerShell (52 characters)

$_+=(@(1.1,1)[0]-1)*1/1%'1'::ToInt|&{!"">~\1}<#^#>`;

I had to put the ^ character as a string literal because as far as I know, the ^ character is not a legal PowerShell character outside string literals. Correct me if I am wrong.

The only way I have found to fit in the : character is to use the :: operator to make a call to a static method.

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

Scheme, 41 characters

(define !$%&*+,-./:<=>?@[\]^_`{|}~ "#'");

This takes advantage of Scheme's incredible tolerance for characters in variable names. In this program I create a variable with the name !$%&*+,-./:<=>?@[]^_`{|}~ which Scheme happily accepts.

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

Tcl 37 chars

set {!#$%&'()*+,-./:<=>?@[]^\_`|~} ""

Defines a variable !#$%&'()*+,-./:<=>?@[]^\_`|~ with an empty string

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

C: 83 56 characters

#undef a
_;main($){$/=-+~!0|0^0%1,1<.0>0?'`':*&"@"[0];}\

At-signs and backticks are not a part of the allowed C-syntax, so I have put them between quotation marks.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Why the parenthesis in (b)? It's strange syntax and a waster of characters. \$\endgroup\$
    – ugoren
    Apr 21, 2013 at 12:45
  • 1
    \$\begingroup\$ If you use the struct for the dot, 0.0 is much shorter. \$\endgroup\$
    – ugoren
    Apr 21, 2013 at 12:46
  • \$\begingroup\$ How could I not think of that (0.0)? The parenthesis is a leftover from when they actually saved one character, but now they are indeed unnecessary. Thanks @ugoren! \$\endgroup\$
    – Fors
    Apr 21, 2013 at 16:09
  • \$\begingroup\$ I think main(_,$) will save a character. \$\endgroup\$
    – ugoren
    Apr 22, 2013 at 11:18
  • \$\begingroup\$ And with gcc, you can write x?:y, but it isn't standard C. \$\endgroup\$
    – ugoren
    Apr 22, 2013 at 11:22
2
\$\begingroup\$

Common Lisp, 33 characters

#+()'|!"$%&*,-./:;<=>?@[\]^_`{}~|

The main part of the above evaluates (or rather, would evaluate if not for the #+() in front of it) to the symbol named:

!#$%&()*+,-./:;<=>?@[\]^_`{}~

The #+() is to avoid output. I'm not sure if the |foo| syntax for symbols counts as a literal, but symbols are used as variable/function names in Common Lisp, and there are a few answers already using the necessary characters in those.

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

Smalltalk (Squeak 4.x dialect) 38 chars

Implement this method in Object, the syntax is valid if Preferences Allow underscore assignment (which is the default setting)

%&*+/<=>?@\!~y""^Z_([:z|{#'',$`;-y}]).

declare Z as global var in pop-up menu if interactive

Explanations:

  • %&*+/<=>?@!~ is a binary selector (an operator)
  • y is the parameter (the operand)
  • "" is an empty comment
  • ^ means return
  • Z is a global variable
  • _ is an assignment
  • () delimits a sub-expression
  • [:z|] delimits a block of code with one parameter z
  • {} delimits an Array
  • #'' is a Symbol literal made of an empty String (first element of the Array)
  • , is a binary message sent to above Symbol
  • $` is a Character literal used as argument of above message
  • ; is for chaining a second message sent to the same Symbol receiver #''
  • the second message is -
  • y is used as the second message argument
  • . is for separating next instruction (here it's a final period)

Damn, who said Smalltalk was readable?

Note that I didn't cheat, the only non valid character ` is put into a literal Character. EDIT see below, this was a false assertion (at least in Squeak)


But we can make it down to 34 chars with this trick

%&*+,-/<=>?@\!~|y""^[{#(:_;`$')}].

This time:

  • #() delimits a literal Array
  • which contains 4 literal Symbols : _ ; ` and 1 literal Character $'

Strictly speaking, we can consider this as cheating... It's not a literal String, but it is still a non empty literal...

I still have to double comment quote "" (unless I make a literal Character with it $", but then I need to double the String quote '')
I still have to use a letter for the parameter (_ is not accepted even if we set Preferences allow underscore in selector to true).

What is nice is that we can even run the method, for example send the message to 1 with argument 2:

1%&*+,-/<=>?@\!~|2

EDIT Finally in 35 chars without cheating

!%&*+,-/<=>?@`y""^[:x_|#()~$';\{}].

Notes:

  • Preferences allow underscore in selector enable using a block parameter named x_
  • ` is a valid character for binary selector contrarily to what I said
  • I also use $' to avoid doubling a quote
\$\endgroup\$
2
\$\begingroup\$

R: 108 characters

a=new(setClass("a",representation(b="list")));a@b=list(c_d=1:2);if(1!='\t'&.1%*%1^1>a@b$c[1]/1|F){`~`<-`?`}#

Clearly the longest submission, but to be able to use symbols @ and $ in R, we need to create an object that has slots (indexed using @) and named elements (indexed using $).
The shortest solution I could think of was to create a new class of object (a=new(setClass("a",representation(b="list")))), which takes its toll. Otherwise, appart from the classics that don't need explanations (such as !=, # or 1:2),

`~`<-`?` 

creates a new operator that does the same as operator ? (i. e. it calls the help page of the element it precedes).

%*% computes the inner product of two matrices (here of 1^1 and .1).
Symbol _ does nothing (AFAIK) in R but is routinely used in variable or function names, as it is the case here.

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

TI-Basic, 36

:End:#$%&'()*+.-/;>=<?@[\]^_`{|}~,!"
\$\endgroup\$
3
  • \$\begingroup\$ This is not valid because the begin quote is often used for comments. \$\endgroup\$
    – Tyzoid
    Dec 29, 2013 at 22:19
  • \$\begingroup\$ @Tyzoid I moved it to the end. \$\endgroup\$
    – Timtech
    Dec 30, 2013 at 11:15
  • \$\begingroup\$ End ends a loop statement, not the program. I think you meant Return. \$\endgroup\$
    – lirtosiast
    Jun 8, 2015 at 1:34
2
\$\begingroup\$

Rust, 45 bytes

macro_rules!a{[r"`\"#%&'_*+-./:<?@^|~]=>{()}}

Try it online!

Creates a macro that matches when you call it with the tokens r"`\"#%&'_*+-./:<?@^|~ and returns the unit type. Do note that the backtick and backslash are inside of a string literal because that is the only valid place they can go. Also, apparently whatever stack exchange uses for syntax highlighting can't handle rust's raw strings.

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

Jelly, 32 bytes

!"#$%&')*+,-./:;<=>?@[\]^_`{|}~(

Try it online!

This technically outputs 0, but given that the empty program does that as well, I didn't see an issue with that (especially as there is no way to "output nothing" in Jelly, aside from outputting the empty string. In this case, 34 33 bytes (-1 byte thanks to xigoi)).

How it works

Jelly works by splitting its program into "links", which are compositions of "chains", which are collections of "atoms" and "quicks". 31 of the 32 characters used above are either atoms or quicks (or syntax characters), ( is the only non-implemented character. When Jelly encounters an unimplemented character outside of a string literal, it breaks the chain, essentially ignoring everything before it. So the ( breaks the program's chain, ignoring all the previous (erroring) characters. At this point, the new chain is made up of the characters after the breaking character, which in this case, is the "empty" program.

For the 33 byte version, the last byte - - is the new chain, and is an empty list, which is then implicitly output at the end of the program as nothing

\$\endgroup\$
4
  • \$\begingroup\$ -1 byte for the “true” version by using instead of “”. \$\endgroup\$
    – xigoi
    Oct 29, 2020 at 18:42
  • \$\begingroup\$ @xigoi Oh yeah, I always forget about it’s niladic return, nice! \$\endgroup\$ Oct 29, 2020 at 18:48
  • \$\begingroup\$ I'm xigoi, not xigol :) \$\endgroup\$
    – xigoi
    Oct 29, 2020 at 18:53
  • \$\begingroup\$ @xigoi My mistake, corrected \$\endgroup\$ Oct 29, 2020 at 18:56
1
\$\begingroup\$

CJam, 34 bytes

""`'5~*:+{i[}%-/#~|<=!$\?@,^>()_];

Try it online!

Explanation:

""`                 Push an empty string and get the string representation of that ("\"\"")
'5~*                Push a 5 character, convert it to a number, and multiply the previous string 5 times. We spend a alphanumeric char here because there's no way to use ' without wasting a char, as ' will simply accept the next character as a character and cannot be otherwise used (except in a string/char literal)
:+                  Over our string, try to do some concatenation. This doesn't really do much.
{i[}%               Convert all the characters in out string to their codepoints (34s for "). We now have a bunch of numbers on the stack. This is really nice, because CJam is strict with types but just about everything accepts numbers in some way. It's so nice that we spend a character on it.
-/#~|<=!$\?@,^>()_  Lots of meaningless operations. I just threw them in randomly and fiddled around with them so that everything would have enough operands, and we would be left with a small stack at the end
];                  Discard what we have left
\$\endgroup\$

Your Answer

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

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