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>
106 Answers 106
Ruby, 26 bytes
Characters can be printed in any order? Don't mind if I do!
$><<(?0..?z).grep(/\w/)*''
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ò
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,'[,_¬^'_
Explanation:
A, 0-9
'[,_¬^ A-Za-z
'_ _
-
1\$\begingroup\$ @mbomb007 Yes it is equivalent to the CJam code
A,'[,_el^'_
\$\endgroup\$GamrCorps– GamrCorps2016年07月18日 19:07:08 +00:00Commented Jul 18, 2016 at 19:07 -
\$\begingroup\$ Which encoding is this? \$\endgroup\$Conor O'Brien– Conor O'Brien2016年07月18日 19:11:20 +00:00Commented Jul 18, 2016 at 19:11
-
\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ CP-1252 or Windows-1252 \$\endgroup\$GamrCorps– GamrCorps2016年07月18日 19:12:07 +00:00Commented Jul 18, 2016 at 19:12
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!
-
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– msh2102016年07月18日 20:05:58 +00:00Commented 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\$Dom Hastings– Dom Hastings2016年07月23日 01:07:52 +00:00Commented Jul 23, 2016 at 1:07
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.
-
-
\$\begingroup\$ @gcampbell 60! (60 factorial) strings might take some time to search... \$\endgroup\$Neil– Neil2016年07月19日 13:53:01 +00:00Commented Jul 19, 2016 at 13:53
-
\$\begingroup\$ You could just use
atob
. \$\endgroup\$gcampbell– gcampbell2016年07月19日 13:53:39 +00:00Commented Jul 19, 2016 at 13:53 -
\$\begingroup\$ @gcampbell I have to exclude unrepresentable strings somehow. \$\endgroup\$Neil– Neil2016年07月19日 13:54:42 +00:00Commented 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– Neil2016年07月19日 18:34:34 +00:00Commented Jul 19, 2016 at 18:34
Haskell, 38 bytes
'_':['a'..'z']++['A'..'Z']++['0'..'9']
Nothing to explain here.
-
2\$\begingroup\$ question: what's the difference between
:
and++
? \$\endgroup\$Downgoat– Downgoat2016年07月18日 17:56:48 +00:00Commented 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– nimi2016年07月18日 17:58:52 +00:00Commented Jul 18, 2016 at 17:58
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 +
"_"
brainfuck, 58 bytes
+++[[<+>->++<]>]<<[-<->]<<-.+<<++[->>+.>+.<<<]<--[->>.+<<]
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 |
^
V, 27 bytes
i1122ñYpñvHgJ|éidd×ばつ
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.
-
5\$\begingroup\$ :D :D :D Cheddar is tied with a golfing lang! \$\endgroup\$Downgoat– Downgoat2016年07月18日 22:57:51 +00:00Commented Jul 18, 2016 at 22:57
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
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.
-
1\$\begingroup\$ Hey, that's not fair. My own solution is identical except I started at
0
... \$\endgroup\$Joey– Joey2016年07月18日 19:16:37 +00:00Commented Jul 18, 2016 at 19:16 -
\$\begingroup\$ @Joey Gotta go fast. :D \$\endgroup\$AdmBorkBork– AdmBorkBork2016年07月18日 19:36:37 +00:00Commented Jul 18, 2016 at 19:36
-
\$\begingroup\$ Especially for the trivial and obvious solutions, I guess ;-) \$\endgroup\$Joey– Joey2016年07月18日 19:38:13 +00:00Commented Jul 18, 2016 at 19:38
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 __"
-
2\$\begingroup\$
do
notation saves two bytes:do(x,y)<-zip"aA0_""zZ9_";[x..y]
\$\endgroup\$Anders Kaseorg– Anders Kaseorg2016年07月19日 08:27:03 +00:00Commented Jul 19, 2016 at 8:27
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.
-
\$\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– manatwork2016年07月19日 08:33:44 +00:00Commented Jul 19, 2016 at 8:33 -
\$\begingroup\$ @manatwork I didn't know that. Oddly enough it now makes pascal competitive here... \$\endgroup\$hdrz– hdrz2016年07月19日 10:13:02 +00:00Commented Jul 19, 2016 at 10:13
-
\$\begingroup\$ The language I grew up with. I know that it is not yet dead... \$\endgroup\$rexkogitans– rexkogitans2016年07月19日 12:06:46 +00:00Commented 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– hdrz2016年07月19日 17:46:43 +00:00Commented Jul 19, 2016 at 17:46
C, 50 bytes
Call f()
without any arguments.
f(n){for(n=128;--n;)isalnum(n)|n==95&&putchar(n);}
Prints
zyxwvutsrqponmlkjihgfedcba_ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210
-
1\$\begingroup\$
isalnum(n)|n==95&&putchar(n)
\$\endgroup\$orlp– orlp2016年07月18日 17:41:12 +00:00Commented Jul 18, 2016 at 17:41 -
\$\begingroup\$ There's UB there. You didn't pass the required arguments. \$\endgroup\$Ven– Ven2016年07月18日 18:33:55 +00:00Commented Jul 18, 2016 at 18:33
-
\$\begingroup\$ @orlp - You used
putc
instead ofputchar
.putc
expects a stream to write to as well, which you didn't pass. The function itself works fine (try removing yourputc
altogether, and it works). \$\endgroup\$owacoder– owacoder2016年07月18日 19:43:01 +00:00Commented Jul 18, 2016 at 19:43 -
\$\begingroup\$ @owacoder Oops! \$\endgroup\$orlp– orlp2016年07月18日 19:45:11 +00:00Commented Jul 18, 2016 at 19:45
-
1
///, 63 bytes
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
-
\$\begingroup\$ How is it the valid answer? Looks like it's violating the rules. \$\endgroup\$nicael– nicael2016年07月18日 19:20:24 +00:00Commented Jul 18, 2016 at 19:20
-
\$\begingroup\$ @nicael which rule? \$\endgroup\$Leaky Nun– Leaky Nun2016年07月18日 19:29:35 +00:00Commented 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– nicael2016年07月18日 19:32:30 +00:00Commented Jul 18, 2016 at 19:32
-
4\$\begingroup\$ Yes, that this didn't use any built-in constants. \$\endgroup\$Leaky Nun– Leaky Nun2016年07月18日 19:33:54 +00:00Commented 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– nicael2016年07月18日 19:35:17 +00:00Commented Jul 18, 2016 at 19:35
Pyth, (削除) 13 (削除ここまで) 12 bytes
s:#"\w"0rk\|
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{_`
basically generates the half-inclusive ranges required: 0-:, A-[, a-{, _-`
.
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
If string constants were allowed, the following would have been 45 bytes:
from string import*
print('_'+printable[:62])
-
1\$\begingroup\$
print('_'+*filter(str.isalnum,map(chr,range(123))))
\$\endgroup\$Leaky Nun– Leaky Nun2016年07月18日 19:22:46 +00:00Commented 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\$TheBikingViking– TheBikingViking2016年07月18日 19:39:47 +00:00Commented 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\$atlasologist– atlasologist2016年07月18日 19:52:07 +00:00Commented Jul 18, 2016 at 19:52
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.
-
2\$\begingroup\$ Welcome to PPCG! Feel free to remove all the space! Also check Tips for golfing in C# for some tips. \$\endgroup\$aloisdg– aloisdg2016年07月18日 19:11:31 +00:00Commented Jul 18, 2016 at 19:11
-
1\$\begingroup\$ Great - saved me 2 bytes more. I'm really starting to like this codegolf :) \$\endgroup\$Daniel– Daniel2016年07月18日 19:37:20 +00:00Commented 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– Value Ink2016年07月18日 20:57:46 +00:00Commented 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– aloisdg2016年07月18日 23:55:08 +00:00Commented 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– aloisdg2016年07月19日 13:19:11 +00:00Commented Jul 19, 2016 at 13:19
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.
-
1\$\begingroup\$
man sh|egrep -io _\|\\w|sort -u|tr -d \\n
\$\endgroup\$Digital Trauma– Digital Trauma2016年07月18日 22:26:30 +00:00Commented 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– user152592016年07月19日 10:29:10 +00:00Commented Jul 19, 2016 at 10:29 -
\$\begingroup\$
env
instead ofman sh
should work on most environments. It does on mine.$ env|egrep -o \\w|sort -u|tr -d \\n
->0123456789ABCDEFGHIKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz
. \$\endgroup\$YSC– YSC2016年07月19日 15:31:26 +00:00Commented Jul 19, 2016 at 15:31 -
\$\begingroup\$ There's no
J
. My bad. \$\endgroup\$YSC– YSC2016年07月19日 15:39:39 +00:00Commented Jul 19, 2016 at 15:39
-
1\$\begingroup\$ +1 for excellence. Side note: PHP 7.2 yields warnings mentioning that future versions will throw errors for undefined constants. :-( \$\endgroup\$Titus– Titus2018年04月30日 18:35:33 +00:00Commented Apr 30, 2018 at 18:35
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_
Output:
_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Thanks to Leaky Nun for golfing 4 bytes off my latest attempt.
-
\$\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 thew
on the left witho
. Although you can save a byte by usingEO
instead ofd
since they only contain 5 characters each. \$\endgroup\$Martin Ender– Martin Ender2016年07月18日 21:26:02 +00:00Commented 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\$Martin Ender– Martin Ender2016年07月18日 21:31:37 +00:00Commented Jul 18, 2016 at 21:31
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.
-
\$\begingroup\$ Wow, I was about to post a solution very similar to yours. \$\endgroup\$Leaky Nun– Leaky Nun2016年07月22日 08:30:53 +00:00Commented Jul 22, 2016 at 8:30
-
\$\begingroup\$ Alike minds think great! \$\endgroup\$Dennis– Dennis2016年07月22日 19:39:40 +00:00Commented Jul 22, 2016 at 19:39
-
\$\begingroup\$ @LeakyNun Hm, kinda late, but it looks like yours is 3 bytes shorter... \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年04月30日 11:36:02 +00:00Commented 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– Dennis2018年04月30日 13:13:24 +00:00Commented Apr 30, 2018 at 13:13
-
\$\begingroup\$ @Dennis Ah, well, the TIO cache can be confusing at times. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年04月30日 13:31:34 +00:00Commented Apr 30, 2018 at 13:31
MATL, 11 bytes
7W:'\W'[]YX
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
-
\$\begingroup\$ 2 bytes shorter:
8W:'\w'XX
\$\endgroup\$DJMcMayhem– DJMcMayhem2016年07月18日 18:22:30 +00:00Commented 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– Luis Mendo2016年07月18日 18:24:24 +00:00Commented 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– DJMcMayhem2016年07月18日 19:34:57 +00:00Commented 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– Luis Mendo2016年07月18日 21:24:52 +00:00Commented Jul 18, 2016 at 21:24
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
Dyalog APL, 18 bytes
∊'\w'⎕S'&'⎕UCS⍳255
prints:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz
-
\$\begingroup\$ @user Built-in constants that contain 9 or more of the above characters are disallowed. \$\endgroup\$Adám– Adám2021年01月11日 19:55:52 +00:00Commented Jan 11, 2021 at 19:55
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
Edit: Saved 4 bytes thank to Adnan
-
\$\begingroup\$ Does this also work:
žyLçJá9ÝJ'_J
? \$\endgroup\$Adnan– Adnan2016年07月18日 21:11:14 +00:00Commented Jul 18, 2016 at 21:11 -
\$\begingroup\$ @Adnan: Yes of course :) Much better! \$\endgroup\$Emigna– Emigna2016年07月18日 21:58:18 +00:00Commented Jul 18, 2016 at 21:58
-
1\$\begingroup\$ Alternatively
žyLçá9Ý'_) ̃J
also works for the same byte count. \$\endgroup\$Emigna– Emigna2016年07月18日 22:04:03 +00:00Commented Jul 18, 2016 at 22:04 -
\$\begingroup\$ Alternatively,
žj
non-competing. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年06月16日 22:05:37 +00:00Commented Jun 16, 2017 at 22:05
CJam, (削除) 15 (削除ここまで) (削除) 14 (削除ここまで) 11 bytes
4 bytes off thanks to @FryAmTheEggman and @Dennis!
A,'[,_el^'_
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
Brainfuck, 89 bytes
+++++++++[>+++++>+<<-]>+++.>[<+.>-]<+++++++>>+++++++++++++[<+<+.+.>>-]<<+++++.+>[<+.+.>-]
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.
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_
Hexagony, 33
"A}_8_47<='>({a/[email protected]!356円);');
Expanded:
" A }
_ 8 _ 4 7
< = ' > ( {
a / _ x . @ .
9 ! \ 3 5 6
) ; ' ) ;
. . . .
Output:
aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1203568479_
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.
and no more
can be parsed as "No more kind of chars" \$\endgroup\$