48
\$\begingroup\$

Write a program or function that prints or returns a string of the alphanumeric characters plus underscore, in any order. To be precise, the following characters need to be output, and no more:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_

When printing to stdout, an optional trailing newline after your output is permitted.

Built-in constants that contain 9 or more of the above characters are disallowed.


Shortest code in bytes wins.

This is a very simple challenge, which I believe will generate some interesting answers nevertheless.


Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=85666,OVERRIDE_USER=4162;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – Dennis
    Commented Jul 19, 2016 at 0:45
  • \$\begingroup\$ "Built-in constants that contain 9 or more of the above characters are disallowed" sighs in 05AB1E... \$\endgroup\$
    – Makonede
    Commented Feb 24, 2021 at 3:43
  • \$\begingroup\$ Is duplication of one char allowed? \$\endgroup\$
    – l4m2
    Commented Feb 24, 2022 at 17:42
  • \$\begingroup\$ @l4m2 No. "To be precise, the following characters need to be output, and no more." \$\endgroup\$
    – orlp
    Commented Feb 25, 2022 at 15:49
  • \$\begingroup\$ and no more can be parsed as "No more kind of chars" \$\endgroup\$
    – l4m2
    Commented Feb 25, 2022 at 15:51

106 Answers 106

16
\$\begingroup\$

Ruby, 26 bytes

Characters can be printed in any order? Don't mind if I do!

Try it online!

$><<(?0..?z).grep(/\w/)*''
\$\endgroup\$
12
\$\begingroup\$

Convex, 9 bytes

New method! Also, I realized that is it pretty much exactly the same as Luis' answer but in Convex, but I came up with this independently.

'{,®\W"Oò

Try it online!

Explanation:

'{,           Array of chars from NUL to 'z
   ®\W"       Regex to match non-word characters
       Oò     Replace all matches with emtpy string

Old solution, 10 bytes:

A,'[,_¬^'_

Try it online!

Explanation:

A,          0-9
'[,_¬^      A-Za-z
'_          _
\$\endgroup\$
3
  • 1
    \$\begingroup\$ @mbomb007 Yes it is equivalent to the CJam code A,'[,_el^'_ \$\endgroup\$
    – GamrCorps
    Commented Jul 18, 2016 at 19:07
  • \$\begingroup\$ Which encoding is this? \$\endgroup\$ Commented Jul 18, 2016 at 19:11
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ CP-1252 or Windows-1252 \$\endgroup\$
    – GamrCorps
    Commented Jul 18, 2016 at 19:12
11
\$\begingroup\$

Perl, 20 bytes

Requires -E at no extra cost.

say+a.._,A.._,_..9,_

So, my original answer (below) was a bit too boring. The only thing I've managed to come up with is the above, that's exactly the same, but looks a bit more confusing... It's pretty much exactly equivalent to the below:

say a..z,A..Z,0..9,_

I like @msh210's suggestions in the comments, but they're just a bit too long!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ +1. Slightly more interesting but longer is any of these, all 27 bytes: say grep/\w/,map chr,1..122 || say map{chr=~/\w/;$&}1..122 || say map{chr=~s/\W//r}1..122 \$\endgroup\$
    – msh210
    Commented Jul 18, 2016 at 20:05
  • 1
    \$\begingroup\$ @msh210 Still can't shrink this more... Managed a 25 though: say chr=~/\w/g for 1..255... \$\endgroup\$ Commented Jul 23, 2016 at 1:07
10
\$\begingroup\$

JavaScript (ES6), 62 bytes

_=>String.fromCharCode(...Array(123).keys()).replace(/\W/g,'')

Returns 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz, so only 6 bytes shorter than a function that returns the string literal. Yes, it sucks.

\$\endgroup\$
7
  • \$\begingroup\$ You could try finding a string for which btoa returns the required output. \$\endgroup\$
    – gcampbell
    Commented Jul 19, 2016 at 9:10
  • \$\begingroup\$ @gcampbell 60! (60 factorial) strings might take some time to search... \$\endgroup\$
    – Neil
    Commented Jul 19, 2016 at 13:53
  • \$\begingroup\$ You could just use atob. \$\endgroup\$
    – gcampbell
    Commented Jul 19, 2016 at 13:53
  • \$\begingroup\$ @gcampbell I have to exclude unrepresentable strings somehow. \$\endgroup\$
    – Neil
    Commented Jul 19, 2016 at 13:54
  • \$\begingroup\$ @gcampbell It turns out that the btoa version takes 62 bytes anyway: 45 to encode 60 alphanumerics, 3 unencoded chars (including _) and 14 for _=>atob("")+"". \$\endgroup\$
    – Neil
    Commented Jul 19, 2016 at 18:34
10
\$\begingroup\$

Haskell, 38 bytes

'_':['a'..'z']++['A'..'Z']++['0'..'9']

Nothing to explain here.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ question: what's the difference between : and ++? \$\endgroup\$
    – Downgoat
    Commented Jul 18, 2016 at 17:56
  • 3
    \$\begingroup\$ @Downgoat: ++ takes two strings and concatenates them. : takes a char and a string and puts the char in front of the string. "_"++['a'..'z']... also works, but is one byte longer. \$\endgroup\$
    – nimi
    Commented Jul 18, 2016 at 17:58
9
\$\begingroup\$

Cheddar, 31 27 bytes

->97@"123+65@"91+48@"58+"_"

This showcases the @" operator well

Non-completing because I finally got aroudn to fixing the @" operator. The bug was that it was generating a Cheddar range not a JS range so it couldn't properly work


Explanation

The @" operator was designed by @CᴏɴᴏʀO'Bʀɪᴇɴ, and what it does is generate a string range from LHS to RHS. When used as an unary operator, it returns the char at the given code point (like python's chr)

Ungolfed

->
  97 @" 123 +
  65 @" 91  +
  48 @" 58  +
  "_"
\$\endgroup\$
0
9
\$\begingroup\$

brainfuck, 58 bytes

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

Try it online.

Initializes the tape to 3·2n, and works from there.

+++[[<+>->++<]>]    initialize the tape
                    |   0 |   3 |   6 |  12 |  24 |  48 |  96 | 192 | 128 |   0 |   0 |
                                                                                    ^
<<[-<->]            subract 128 from 192
                    |   0 |   3 |   6 |  12 |  24 |  48 |  96 |  64 |   0 |   0 |   0 |
                                                                        ^
<<-.+<<++           ouput '_'; increment 24 twice
                    |   0 |   3 |   6 |  12 |  26 |  48 |  96 |  64 |   0 |   0 |   0 |
                                                ^
[->>+.>+.<<<]       output aAbBcC ~ zZ
                    |   0 |   3 |   6 |  12 |   0 |  48 | 122 |  90 |   0 |   0 |   0 |
                                                ^
<--[->>.+<<]        decrement 12 twice; output 0 ~ 9
                    |   0 |   3 |   6 |   0 |   0 |  58 | 122 |  90 |   0 |   0 |   0 |
                                          ^
\$\endgroup\$
8
\$\begingroup\$

V, 27 bytes

i1122ñYpñvHgJ|éidd@"Í×

Try it online!

This answer is horribly convoluted. I'll post an explanation later.

Hexdump:

00000000: 6916 1631 1b31 3232 f159 7001 f176 4867  i..1.122.Yp..vHg
00000010: 4a7c e969 6464 4022 1bcd d7              J|.idd@"...

Explanation:

Readable:

i<C-v><C-v>1<esc>                                   "Insert the text "<C-v>1"
                                                    "<C-v> means "literal"
                 122ñ       ñ                       "122 times,
                     Yp                             "Duplicate this line
                       <C-a>                        "And increment the first number on this line
                             vHgJ                   "Join every line together
                                 |éi                "Insert an 'i' at the beginning of this line
                                    dd              "Delete this line
                                      @"<esc>       "And execute it as V code.
                                                    "That will generate every ascii value from 1-123
                                             Í×     "Now remove every non-word character.
\$\endgroup\$
1
  • 5
    \$\begingroup\$ :D :D :D Cheddar is tied with a golfing lang! \$\endgroup\$
    – Downgoat
    Commented Jul 18, 2016 at 22:57
8
\$\begingroup\$

J, 30 29 28 bytes

Saved a byte thanks to randomra!

~.u:95,;48 65 97+i."*10,,~26

Output:

   ~.u:95,;48 65 97+i."*10,,~26
_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Explanation

I wont provide an explanation per se, but will provide intermediate results.

   10,,~26
10 26 26
   i. b. 0
1 _ _
   *  b. 0
0 0 0
   i."* b. 0
   i."*2 3 4
0 1 0 0
0 1 2 0
0 1 2 3
   i. 2
0 1
   i. 3
0 1 2
   i. 4
0 1 2 3
   i."*10,,~26
0 1 2 3 4 5 6 7 8 9  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   0 1 2 + i."*10,,~26
0 1 2 3 4 5 6 7  8  9  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
1 2 3 4 5 6 7 8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   48 65 97+i."*10,,~26
48 49 50  51  52  53  54  55  56  57  48  48  48  48  48  48  48  48  48  48  48  48  48  48  48  48
65 66 67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
   ;48 65 97+i."*10,,~26
48 49 50 51 52 53 54 55 56 57 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
   95,;48 65 97+i."*10,,~26
95 48 49 50 51 52 53 54 55 56 57 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 ...
   u:95,;48 65 97+i."*10,,~26
_01234567890000000000000000ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
   ~.u:95,;48 65 97+i."*10,,~26
_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
\$\endgroup\$
0
7
\$\begingroup\$

PowerShell v3+, 35 33 bytes

-join([char[]](1..127)-match'\w')

Constructs a dynamic array 1..127, casts it as a char array. That's fed to the -match operator working on the regex \w, which will return all elements that match (i.e., exactly alphanumeric and underscore). We encapsulate those array elements in a -join to bundle it up as one string. That's left on the pipeline and output is implicit.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hey, that's not fair. My own solution is identical except I started at 0 ... \$\endgroup\$
    – Joey
    Commented Jul 18, 2016 at 19:16
  • \$\begingroup\$ @Joey Gotta go fast. :D \$\endgroup\$ Commented Jul 18, 2016 at 19:36
  • \$\begingroup\$ Especially for the trivial and obvious solutions, I guess ;-) \$\endgroup\$
    – Joey
    Commented Jul 18, 2016 at 19:38
7
\$\begingroup\$

Haskell, 31 bytes

do(x,y)<-zip"aA0_""zZ9_";[x..y]

The expression zip "aA0_" "zZ9_" gives the list of endpoints [('a','z'),('A','Z'),('0','9'),('_','_')]. The do notation takes each (x,y) to the inclusive \(x,y)->[x..y] and concatenates the results. Thanks to Anders Kaseorg for two bytes with do instead of >>=.

Compare to alternatives:

do(x,y)<-zip"aA0_""zZ9_";[x..y]

zip"aA0_""zZ9_">>= \(x,y)->[x..y]
f(x,y)=[x..y];f=<<zip"aA0_""zZ9_"
id=<<zipWith enumFromTo"aA0_""zZ9_"
[c|(a,b)<-zip"aA0_""zZ9_",c<-[a..b]]
f[x,y]=[x..y];f=<<words"az AZ 09 __"
\$\endgroup\$
1
  • 2
    \$\begingroup\$ do notation saves two bytes: do(x,y)<-zip"aA0_""zZ9_";[x..y] \$\endgroup\$ Commented Jul 19, 2016 at 8:27
5
\$\begingroup\$

Pascal (FPC), 85 83 73 bytes

Just plain object pascal using a set of chars. Writing a full program instead of a procedure shaves off 2 bytes. Removing the program keyword shaves 10 more bytes.

var c:char;begin for c in['a'..'z','A'..'Z','0'..'9','_']do write(c);end.

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ I always was and probably will always be confused by the exact classification of Object Pascal. Turbo/Borland Pascal and FreePascal are certainly happy without the useless program keyword. \$\endgroup\$
    – manatwork
    Commented Jul 19, 2016 at 8:33
  • \$\begingroup\$ @manatwork I didn't know that. Oddly enough it now makes pascal competitive here... \$\endgroup\$
    – hdrz
    Commented Jul 19, 2016 at 10:13
  • \$\begingroup\$ The language I grew up with. I know that it is not yet dead... \$\endgroup\$ Commented Jul 19, 2016 at 12:06
  • \$\begingroup\$ @rexkogitans Yeah I learned it at school, but can't remember anything from back then... I'm playing with some programming again now \$\endgroup\$
    – hdrz
    Commented Jul 19, 2016 at 17:46
4
\$\begingroup\$

C, 50 bytes

Call f() without any arguments.

f(n){for(n=128;--n;)isalnum(n)|n==95&&putchar(n);}

Prints

zyxwvutsrqponmlkjihgfedcba_ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210
\$\endgroup\$
10
  • 1
    \$\begingroup\$ isalnum(n)|n==95&&putchar(n) \$\endgroup\$
    – orlp
    Commented Jul 18, 2016 at 17:41
  • \$\begingroup\$ There's UB there. You didn't pass the required arguments. \$\endgroup\$
    – Ven
    Commented Jul 18, 2016 at 18:33
  • \$\begingroup\$ @orlp - You used putc instead of putchar. putc expects a stream to write to as well, which you didn't pass. The function itself works fine (try removing your putc altogether, and it works). \$\endgroup\$
    – owacoder
    Commented Jul 18, 2016 at 19:43
  • \$\begingroup\$ @owacoder Oops! \$\endgroup\$
    – orlp
    Commented Jul 18, 2016 at 19:45
  • 1
    \$\begingroup\$ @QPaysTaxes that's wrong. If the number of arguments does not equal the number of parameters, the behavior is undefined. 6.5.2.2/6, see N1570. \$\endgroup\$
    – Ven
    Commented Jul 20, 2016 at 6:18
4
\$\begingroup\$

///, 63 bytes

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
\$\endgroup\$
6
  • \$\begingroup\$ How is it the valid answer? Looks like it's violating the rules. \$\endgroup\$
    – nicael
    Commented Jul 18, 2016 at 19:20
  • \$\begingroup\$ @nicael which rule? \$\endgroup\$
    – Leaky Nun
    Commented Jul 18, 2016 at 19:29
  • 1
    \$\begingroup\$ "Built-in constants that contain 9 or more of the above characters are disallowed."... Am I missing something obvious, @Leaky? \$\endgroup\$
    – nicael
    Commented Jul 18, 2016 at 19:32
  • 4
    \$\begingroup\$ Yes, that this didn't use any built-in constants. \$\endgroup\$
    – Leaky Nun
    Commented Jul 18, 2016 at 19:33
  • \$\begingroup\$ Ok, but this is weird at least. Definitely OP doesn't ask you to copy-paste the sequence, that's not interesting. \$\endgroup\$
    – nicael
    Commented Jul 18, 2016 at 19:35
4
\$\begingroup\$

Pyth, 13 12 bytes

s:#"\w"0rk\|

Try it online!

Finds all characters in U+0000 to U+007B that matches the regex /\w/.

Outputs 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz.

alternative approach: 15 bytes

ssrMc4"0:A[a{_`

Try it online!

basically generates the half-inclusive ranges required: 0-:, A-[, a-{, _-`.

\$\endgroup\$
4
\$\begingroup\$

Python 3, 58 bytes

print('_',*filter(str.isalnum,map(chr,range(123))),sep='')

A full program that prints to STDOUT.

The output is: _0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

How it works

map(chr,range(123))       Yield an iterator containing all ascii characters with
                          code-points in [0,122]...
*filter(str.isalnum,...)  ...keep characters if alphanumeric and unpack into tuple...
print('_',...,sep='')     ...add underscore and print all characters with no separating
                          space

Try it on Ideone

If string constants were allowed, the following would have been 45 bytes:

from string import*
print('_'+printable[:62])
\$\endgroup\$
3
  • 1
    \$\begingroup\$ print('_'+*filter(str.isalnum,map(chr,range(123)))) \$\endgroup\$
    – Leaky Nun
    Commented Jul 18, 2016 at 19:22
  • \$\begingroup\$ @LeakyNun That throws a syntax error. I thought about doing something similar with print('_',*filter(str.isalnum,map(chr,range(123)))), but that prints separating spaces; the OP said in the comments that '...no spaces are permitted in the output at all'. \$\endgroup\$ Commented Jul 18, 2016 at 19:39
  • \$\begingroup\$ Python 2 can save one byte with this approach: print''.join(filter(str.isalnum,map(chr,range(123)))),'_' \$\endgroup\$ Commented Jul 18, 2016 at 19:52
4
\$\begingroup\$

My first attempt at codegolf!

C#, 168 152 150 147 130 127 117 116 115 109 106 bytes

for(var a='0';a<'~';a++){Console.Write(System.Text.RegularExpressions.Regex.IsMatch(a+"","\\w")?a+"":"");}

Thanks a lot to aloisdg, AstroDan, Leaky Nun and Kevin Lau - not Kenny for all the help in comments.

\$\endgroup\$
24
  • 2
    \$\begingroup\$ Welcome to PPCG! Feel free to remove all the space! Also check Tips for golfing in C# for some tips. \$\endgroup\$
    – aloisdg
    Commented Jul 18, 2016 at 19:11
  • 1
    \$\begingroup\$ Great - saved me 2 bytes more. I'm really starting to like this codegolf :) \$\endgroup\$
    – Daniel
    Commented Jul 18, 2016 at 19:37
  • 1
    \$\begingroup\$ AFAIK, the \w regex class covers the alphanumerics and also _, which should make "\\w" valid enough for your regex match function. \$\endgroup\$
    – Value Ink
    Commented Jul 18, 2016 at 20:57
  • 1
    \$\begingroup\$ Your code should a be function or a full program not a statement. Also use .NetFiddle to share a demonstration of your code :) \$\endgroup\$
    – aloisdg
    Commented Jul 18, 2016 at 23:55
  • 2
    \$\begingroup\$ "C# isn't the best to codegolf" I find C# fun enough to code golf. I think it is a great mainstream language to play with. If you are here to win, you will loose. Define your objective as learning and having a good time and you will win all the times. \$\endgroup\$
    – aloisdg
    Commented Jul 19, 2016 at 13:19
4
\$\begingroup\$

Pure bash, 32

printf %s {a..z} {A..Z} {0..9} _

Ideone.

\$\endgroup\$
4
\$\begingroup\$

bash – 47 37 bytes

man sh|egrep -o \\w|sort -u|tr -d \\n

Output on my system is:

_0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ

Thanks to Digital Trauma for helpful suggestions.

On some systems you might be able to use ascii instead of man sh to save a byte.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ man sh|egrep -io _\|\\w|sort -u|tr -d \\n \$\endgroup\$ Commented Jul 18, 2016 at 22:26
  • 1
    \$\begingroup\$ @DigitalTrauma Thank you! Experimented and found out \w matches _ and is already case insensitive, so could shorten even more. \$\endgroup\$
    – user15259
    Commented Jul 19, 2016 at 10:29
  • \$\begingroup\$ env instead of man sh should work on most environments. It does on mine. $ env|egrep -o \\w|sort -u|tr -d \\n -> 0123456789ABCDEFGHIKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz. \$\endgroup\$
    – YSC
    Commented Jul 19, 2016 at 15:31
  • \$\begingroup\$ There's no J. My bad. \$\endgroup\$
    – YSC
    Commented Jul 19, 2016 at 15:39
4
\$\begingroup\$

PHP, 40 bytes

_0<?for(;$a++^9?$a^q:$a=A;)echo" $a"|$a;

Online Demo.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 for excellence. Side note: PHP 7.2 yields warnings mentioning that future versions will throw errors for undefined constants. :-( \$\endgroup\$
    – Titus
    Commented Apr 30, 2018 at 18:35
4
\$\begingroup\$

Retina, 30 19 16 15 12 bytes

I modified my original alphabet attempt for this latest version. Each character is printed in a loop.

The first line is empty.


;
+T\`;w`w_

Try it online

Output:

_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Thanks to Leaky Nun for golfing 4 bytes off my latest attempt.

\$\endgroup\$
2
  • \$\begingroup\$ I'd say that w counts as a constant containing 9 or more of the required characters. You'll probably have to expand the right hand side and replace the w on the left with o. Although you can save a byte by using EO instead of d since they only contain 5 characters each. \$\endgroup\$ Commented Jul 18, 2016 at 21:26
  • 1
    \$\begingroup\$ @mbomb007 w inside the transliteration stage has nothing to do regex. It's a shorthand that expands to a list of the required 63 characters. At least I'd ask orlp about this case specifically, since it's quite different from using \w in a regex. \$\endgroup\$ Commented Jul 18, 2016 at 21:31
4
\$\begingroup\$

Sesos, 17 bytes

00000000: a854be 2cbc9e 71d597 14bc56 1ad99e 713b           .T.,..q....V...q;

Output

0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz_

Try it online! Check Debug to see the generated binary code.

How it works

The binary file above has been generated by assembling the following SASM code.

add 48     ; Set cell 0 to 48 ('0').
fwd 1      ; Advance to cell 1.
add 10     ; Set cell 1 to 10.
jmp        ; Set an entry marker and jump to the jnz instruction.
    rwd 1  ;   Retrocede to cell 0.
    put    ;   Print its content (initially '0').
    add 1  ;   Increment cell 0 ('0' -> '1', etc.).
    fwd 1  ;   Advance to cell 1.
    sub 1  ;   Decrement cell 1.
jnz        ; While cell 1 in non-zero, jump to 'rwd 1'.
           ; This loop will print "0123456789".
rwd 1      ; Retrocede to cell 0, which holds 48 + 10 = 58.
add 7      ; Set cell 0 to 65 ('A').
fwd 1      ; Advance to cell 1.
add 26     ; Set cell 1 to 26.
jmp        ; Set an entry marker and jump to the jnz instruction.
    rwd 1  ; Retrocede to cell 0.
    put    ; Print its content (initially 'A').
    add 32 ; Add 32 to convert to lowercase ('A' -> 'a', etc.).
    put    ; Print the cell's content.
    sub 31 ; Subtract 31 to switch to the next uppercase letter ('a' -> 'B', etc.).
    fwd 1  ; Advance to cell 1.
    sub 1  ;   Decrement cell 1.
jnz        ; While cell 1 in non-zero, jump to 'rwd 1'.
           ; This loop will print "AaBb...YyZz".
rwd 1      ; Retrocede th cell 0, which holds 65 + 26 = 91.
add 4      ; Set cell 0 to 95 ('_').
put        ; Print its content.
\$\endgroup\$
6
  • \$\begingroup\$ Wow, I was about to post a solution very similar to yours. \$\endgroup\$
    – Leaky Nun
    Commented Jul 22, 2016 at 8:30
  • \$\begingroup\$ Alike minds think great! \$\endgroup\$
    – Dennis
    Commented Jul 22, 2016 at 19:39
  • \$\begingroup\$ @LeakyNun Hm, kinda late, but it looks like yours is 3 bytes shorter... \$\endgroup\$ Commented Apr 30, 2018 at 11:36
  • \$\begingroup\$ @EriktheOutgolfer Sesos used to be encoded in a less efficient manner. Both programs are 17 bytes long now. \$\endgroup\$
    – Dennis
    Commented Apr 30, 2018 at 13:13
  • \$\begingroup\$ @Dennis Ah, well, the TIO cache can be confusing at times. \$\endgroup\$ Commented Apr 30, 2018 at 13:31
3
\$\begingroup\$

MATL, 11 bytes

7W:'\W'[]YX

Try it online!

7W     % Push  2 raised to 7, i.e. 128
:      % Range [1 2 ... 128]
'\W'   % Push string to be used as regex pattern
[]     % Push empty array
YX     % Regex replace. Uses (and consumes) three inputs: source text, regex pattern, 
       % target text. The first input (source text) is implicitly converted to char.
       % So this replaces non-word characters by nothing.
       % Implicitly display
\$\endgroup\$
4
  • \$\begingroup\$ 2 bytes shorter: 8W:'\w'XX \$\endgroup\$
    – DJMcMayhem
    Commented Jul 18, 2016 at 18:22
  • 2
    \$\begingroup\$ @DrGreenEggsandIronMan Thanks! But I think newlines are not allowed as separators. The challenge says " a string of the alphanumeric characters plus underscore... and no more" \$\endgroup\$
    – Luis Mendo
    Commented Jul 18, 2016 at 18:24
  • 1
    \$\begingroup\$ So why couldn't you do 8W:'\w'XX! which I tried but doesn't work \$\endgroup\$
    – DJMcMayhem
    Commented Jul 18, 2016 at 19:34
  • \$\begingroup\$ @DrGreenEggsandIronMan Very good question! Let me answer you in chat so I can use a little more characters \$\endgroup\$
    – Luis Mendo
    Commented Jul 18, 2016 at 21:24
3
\$\begingroup\$

Brachylog, 25 bytes

"_"w9yrcw"A":"Z"ycL@l:Lcw

This prints the following to STDOUT:

_9876543210abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Explanation

"_"w               Write "_"
9y                 Get the list [0:1:2:3:4:5:6:7:8:9]
  rcw              Reverse it, concatenate into one number, write
"A":"Z"y           Get the list of all uppercase letters
        cL         Concatenate into a single string L
          @l:Lcw   Concatenate L to itself lowercased and write
\$\endgroup\$
0
3
\$\begingroup\$

Dyalog APL, 18 bytes

∊'\w'⎕S'&'⎕UCS⍳255

prints:

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz
\$\endgroup\$
1
  • \$\begingroup\$ @user Built-in constants that contain 9 or more of the above characters are disallowed. \$\endgroup\$
    – Adám
    Commented Jan 11, 2021 at 19:55
3
\$\begingroup\$

05AB1E, 18 16 12 bytes

žyLçá9Ý'_)˜J

Explanation

žyL               # push [1..128]
   ç              # convert to char
    á             # keep only members of the alphabet
     9Ý           # push [0..9]
       '_         # push underscore
         )˜J      # add to lists of lists, flatten and join
                  # implicit output

Try it online

Edit: Saved 4 bytes thank to Adnan

\$\endgroup\$
4
  • \$\begingroup\$ Does this also work: žyLçJá9ÝJ'_J? \$\endgroup\$
    – Adnan
    Commented Jul 18, 2016 at 21:11
  • \$\begingroup\$ @Adnan: Yes of course :) Much better! \$\endgroup\$
    – Emigna
    Commented Jul 18, 2016 at 21:58
  • 1
    \$\begingroup\$ Alternatively žyLçá9Ý'_)˜J also works for the same byte count. \$\endgroup\$
    – Emigna
    Commented Jul 18, 2016 at 22:04
  • \$\begingroup\$ Alternatively, žj non-competing. \$\endgroup\$ Commented Jun 16, 2017 at 22:05
3
\$\begingroup\$

CJam, 15 14 11 bytes

4 bytes off thanks to @FryAmTheEggman and @Dennis!

A,'[,_el^'_

Try it online!

A,    e# Push range [0 1 ... 9]
'[,   e# Push range of chars from 0 to "Z" ("[" minus 1)
_el   e# Duplicate and convert to lowercase
^     e# Symmetric difference. This keeps letters only, both upper- and lower-case
'_    e# Push "_".
      e# Implicitly display stack contents, without separators
\$\endgroup\$
0
3
\$\begingroup\$

Brainfuck, 89 bytes

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

Try it here

Details:

+++++++++[>+++++>+<<-]>+++.  Goes to '0' while remembering a 9 for the 9 other numbers
[<+.>-]                      Simply prints for the next 9 characters
<+++++++>                    Moves it 7 space to the letters
>+++++++++++++               Saves a 13
[<+<+.+.>>-]                 Prints 2 char at a time while making a second '13' space
<<+++++.+>                   Moves 5, prints '_' and moves to the lowercases
[<+.+.>-]                    And again the double print

If I could have commented, I would have to improve others answers. But since I can't, I might as well post my own. As I started writing this the lowest BF one was 96 long.

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

F#, 50 59 bytes

Seq.iter(printf"%c"<<char)(95::[48..57]@[65..90]@[97..122])

Output:

_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Edit: missed the digits the first time

Edit2, inspired by this Haskell solution this F# snippet is 67 bytes.

Seq.zip"aA0_""zZ9_"|>Seq.iter(fun(x,y)->Seq.iter(printf"%c")[x..y])

Output:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
\$\endgroup\$
0
3
\$\begingroup\$

Hexagony, 33

"A}_8_47<='>({a/[email protected]!\356);');

Expanded:

    " A }
  _ 8 _ 4 7
 < = ' > ( {
a / _ x . @ .
 9 ! \ 3 5 6
  ) ; ' ) ;
   . . . .

Output:

aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1203568479_

Try it online!

Note that there is an unprintable character 0x1A as the first byte of the program. This also makes the first row of the expanded Hexagon look sort of off. Many thanks to Martin for showing me this trick, as well as for suggesting the algorithm for printing the alphabet!

This prints the alphabet by storing a and A on two edges of a hexagon and the number 26 on the edge of the hexagon that touches the joint between the letters. This looks something like this:

A \ / a
   |
   26

Then it enters a loops that prints the letters and then increments them, and then decrements the number. After one iteration we would have:

B \ / b
   |
   25

And so on. The linear code for the initialisation is: 0x1A " A } a. The linear code for the loops outside of control flow changes is: ; ) ' ; ) { ( ' =.

Once the counter reaches zero, we follow a different path to print the numbers and an underscore. Written out linearly this is: x 3 5 6 8 4 7 9 ! ; { @. This replaces the current memory edge's value with the number 1203568479 (note that x's ASCII code is 120), which contains all of the decimal digits. We print out this number and then we use a neat feature of Hexagony: we print out the number mod 256 as an ASCII character. This just happens to be 95, or underscore.

\$\endgroup\$

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