Write a program that uses all printable non-alphanumeric ASCII symbols

17

3

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.

Petr Pudlák

Posted 2012-11-07T19:13:25.473

Reputation: 4 272

Is it required to do nothing, or is it just not required to do anything? – Matt – 2012-11-07T19:48:35.123

@Matt I'd prefer programs that do nothing, but don't consider it important, solutions that do something are also acceptable. – Petr Pudlák – 2012-11-07T20:01:06.463

1Can 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. – dmckee --- ex-moderator kitten – 2012-11-07T21:26:12.033

For that matter what about % in printf format specifiers which are formally strings, but are in some sense part of the language? – dmckee --- ex-moderator kitten – 2012-11-07T21:44:21.493

2shortest code wins? i suspect there will need to be a tie-breaking criteria too – ardnew – 2012-11-07T22:52:26.447

@dmckee I'd say % is inside a string literal so it should not be considered. – Petr Pudlák – 2012-11-08T06:06:07.127

@dmckee, % is a C operator. But @$ and backtick are indeed problematic. – ugoren – 2012-11-08T07:25:50.597

@ugoren Er...how could I forget modulus? ::sigh:: – dmckee --- ex-moderator kitten – 2012-11-08T15:45:38.217

1In Javascript, the characters #, @, and ` are not legal outside of any sort of literal – Peter Olson – 2012-11-08T16:13:33.783

6This is basically a competition of "find a language that can use all of these characters as syntax". Pretty crappy code gold in my opinion – Earlz – 2012-11-08T16:34:30.590

1Yea 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. – Ryan – 2012-11-08T20:41:57.277

@ryan I was thinking about requiring some goal. But I fear that the results will be programs that do the goal in one part and use all characters in another. If you think it's better, feel free to start your own similar code-golf question requiring some goal. Competition benefits customers. | And to your other objection, that's why I said 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). In other words, you can omit those characters that aren't available in your language, if you explain it. – Petr Pudlák – 2012-11-09T06:25:21.560

Answers

18

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 . :

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

vsz

Posted 2012-11-07T19:13:25.473

Reputation: 7 963

As there are no explicit comments (as in, so symbol that indicates that specific strings are part of a comment) I hope it qualifies. Brainf*ck just ignores the characters which are not part of the language, so you don't need (and can't use) comments and string literals. – vsz – 2012-11-08T04:23:24.580

Edited the order of < and > to be more interpreter-friendly – vsz – 2012-11-08T09:03:54.220

6Hm... I'd rather say it does hide characters in comments, but oh well. – ceased to turn counterclockwis – 2012-11-08T14:17:08.570

5@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. – vsz – 2012-11-08T15:54:59.140

3@vsz There's nothing wrong with bending the rules. – Gareth – 2012-11-08T17:18:05.570

1Inversion 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. – Petr Pudlák – 2012-11-10T10:18:51.483

@PetrPudlák: Just out of curiosity: How would you have dealt with this answer, if vsz just explained, that most of the requested characters are not part of the language and hence came up with a solution, that only contained a few characters? (yes, I know this thing is 4 years old) – Bodo Thiesen – 2016-12-05T00:23:12.503

1@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. – Petr Pudlák – 2016-12-05T19:20:32.603

47

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 #

ardnew

Posted 2012-11-07T19:13:25.473

Reputation: 2 177

Correct me if I'm wrong, but did you just make a scalar called \<@"^[({_},)]/+-~%'?&|:!*.=>`? – Mr. Llama – 2012-11-08T17:25:29.790

2@GigaWatt no, this is a sequence of several different expressions. i will add a description of how it is evaluated – ardnew – 2012-11-08T18:37:55.023

@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 . . . – ruakh – 2012-11-08T18:42:14.837

23Either you're a genius, or any random byte sequence is a valid Perl program. Or both. – ugoren – 2012-11-08T20:37:03.013

4Although according to my own rules I had to award the Brainfuck solution, this one is my favorite (same size, but inversion number 279). – Petr Pudlák – 2012-11-10T10:22:32.907

9

Whitespace, 32

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

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

SeanC

Posted 2012-11-07T19:13:25.473

Reputation: 1 117

3one could argue therefor that all non-whitespace characters are comments and this is invalid :) – NRGdallas – 2013-01-11T21:34:27.053

8

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.

Matt

Posted 2012-11-07T19:13:25.473

Reputation: 1 395

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}` – Strigoides – 2012-11-10T14:47:56.453

1@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) – Matt – 2012-11-10T18:33:17.407

I see, I was using python 2.6 when I tested it. – Strigoides – 2012-11-11T10:11:06.763

8

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

Leigh

Posted 2012-11-07T19:13:25.473

Reputation: 261

4IMHO 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. – Petr Pudlák – 2012-11-08T17:10:32.440

Also, the program required by the task should output nothing, and there is only one quine which outputs nothing. – Paŭlo Ebermann – 2016-12-08T21:15:59.773

It doesn't even get parsed by PHP so this is pretty much invalid – Rob – 2018-05-28T12:25:19.073

@RobQuist of course it gets parsed, it's busy hunting that elusive T_OPEN_TAG token right til the bitter end – Leigh – 2018-05-29T10:11:35.700

7

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. :(

gfldex

Posted 2012-11-07T19:13:25.473

Reputation: 173

I'd say this is ok, using symbols in an identifier seems reasonable. – Petr Pudlák – 2012-11-08T05:28:11.647

7

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.

ceased to turn counterclockwis

Posted 2012-11-07T19:13:25.473

Reputation: 5 200

5

k (33 chars)

Very easy in k. Unfortunately " must be paired.

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

skeevey

Posted 2012-11-07T19:13:25.473

Reputation: 4 139

5

J, 33 characters

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

' needs to be doubled to avoid syntax errors.

My first answer was missing some symbols.

Gareth

Posted 2012-11-07T19:13:25.473

Reputation: 11 678

5

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.

Peter Taylor

Posted 2012-11-07T19:13:25.473

Reputation: 41 901

5

C, 48 chars

Kind of cheating.

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

ugoren

Posted 2012-11-07T19:13:25.473

Reputation: 16 527

2Definitely 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. – dmckee --- ex-moderator kitten – 2012-11-08T15:49:53.000

@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. – ugoren – 2012-11-08T16:06:54.557

No doubt about it, mine is also foul, underhanded and sneaky. But it's fun, too. – dmckee --- ex-moderator kitten – 2012-11-08T16:20:27.830

@dmckee, fun is what it's about. BTW, you need #define Q(_) #_. The preprocessor would treat "##_##" as a string literal. – ugoren – 2012-11-08T21:32:55.737

5

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

Geoff Reedy

Posted 2012-11-07T19:13:25.473

Reputation: 2 828

3

Tcl 37 chars

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

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

Johannes Kuhn

Posted 2012-11-07T19:13:25.473

Reputation: 7 122

3

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.

Fors

Posted 2012-11-07T19:13:25.473

Reputation: 3 020

1Why the parenthesis in (b)? It's strange syntax and a waster of characters. – ugoren – 2013-04-21T12:45:00.387

1If you use the struct for the dot, 0.0 is much shorter. – ugoren – 2013-04-21T12:46:23.313

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! – Fors – 2013-04-21T16:09:41.937

I think main(_,$) will save a character. – ugoren – 2013-04-22T11:18:54.977

And with gcc, you can write x?:y, but it isn't standard C. – ugoren – 2013-04-22T11:22:17.900

main(,$) wouldn't compile under Clang, and it wouldn't save any characters anyway. `;main($)vsmain(_,$)`, both are 9 characters long. – Fors – 2013-04-22T13:31:32.150

Why would Clang care if main has two parameters? But the point is that , is used, allowing 2 characters to be deleted from the body (not just 1 as I wrote). – ugoren – 2013-04-22T18:15:28.167

Ah, I see what you mean. What Clang doesn't allow is to have an unspecified type as a second argument to main, specifically it enforces it to be char**. – Fors – 2013-04-22T18:43:40.580

3

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.

user6786

Posted 2012-11-07T19:13:25.473

Reputation:

1#if A...#endif is shorter than #region...#endregion. And "x"[0]+ is shorter than [System.STAThread]. – Peter Taylor – 2012-11-08T18:13:01.313

3

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.

user6786

Posted 2012-11-07T19:13:25.473

Reputation:

3

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.

Strigoides

Posted 2012-11-07T19:13:25.473

Reputation: 1 025

2

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

aka.nice

Posted 2012-11-07T19:13:25.473

Reputation: 411

2

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.

Strigoides

Posted 2012-11-07T19:13:25.473

Reputation: 1 025

1

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.

plannapus

Posted 2012-11-07T19:13:25.473

Reputation: 8 610

1

TI-Basic, 36

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

Timtech

Posted 2012-11-07T19:13:25.473

Reputation: 12 038

End ends a loop statement, not the program. I think you meant Return. – lirtosiast – 2015-06-08T01:34:30.377

This is not valid because the begin quote is often used for comments. – Tyzoid – 2013-12-29T22:19:29.083

@Tyzoid I moved it to the end. – Timtech – 2013-12-30T11:15:25.750