var QUESTION_ID=49876;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),e.has_more?getAnswers():process()}})}function shouldHaveHeading(e){var a=!1,r=e.body_markdown.split("\n");try{a|=/^#/.test(e.body_markdown),a|=["-","="].indexOf(r[1][0])>-1,a&=LANGUAGE_REG.test(e.body_markdown)}catch(n){}return a}function shouldHaveScore(e){var a=!1;try{a|=SIZE_REG.test(e.body_markdown.split("\n")[0])}catch(r){}return a}function getAuthorName(e){return e.owner.display_name}function process(){answers=answers.filter(shouldHaveScore).filter(shouldHaveHeading),answers.sort(function(e,a){var r=+(e.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0],n=+(a.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0];return r-n});var e={},a=1,r=null,n=1;answers.forEach(function(s){var t=s.body_markdown.split("\n")[0],o=jQuery("#answer-template").html(),l=(t.match(NUMBER_REG)[0],(t.match(SIZE_REG)||[0])[0]),c=t.match(LANGUAGE_REG)[1],i=getAuthorName(s);l!=r&&(n=a),r=l,++a,o=o.replace("{{PLACE}}",n+".").replace("{{NAME}}",i).replace("{{LANGUAGE}}",c).replace("{{SIZE}}",l).replace("{{LINK}}",s.share_link),o=jQuery(o),jQuery("#answers").append(o),e[c]=e[c]||{lang:c,user:i,size:l,link:s.share_link}});var s=[];for(var t in e)e.hasOwnProperty(t)&&s.push(e[t]);s.sort(function(e,a){return e.lang>a.lang?1:e.lang<a.lang?-1:0});for(var o=0;o<s.length;++o){var l=jQuery("#language-template").html(),t=s[o];l=l.replace("{{LANGUAGE}}",t.lang).replace("{{NAME}}",t.user).replace("{{SIZE}}",t.size).replace("{{LINK}}",t.link),l=jQuery(l),jQuery("#languages").append(l)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",answers=[],page=1;getAnswers();var SIZE_REG=/\d+(?=[^\d&]*(?:<(?:s>[^&]*<\/s>|[^&]+>)[^\d&]*)*$)/,NUMBER_REG=/\d+/,LANGUAGE_REG=/^#*\s*([^,]+)/;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;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>
A+
undA-
? Ich verstehe nicht, warum wir sie ignorieren.+
Antworten:
Python 2,
727062 BytesDies ist eine anonyme Funktion, die eine Ganzzahl annimmt und die Note als Zeichenfolge zurückgibt.
(thanks to @MartinBüttner, @grc and @TheNumberOne for tips)
quelle
"EDCB"[n/10-6]
->chr(75-n/10)
CJam,
34 3332 bytesOkay, I've tried multiple approaches now and am unable to get this below 33, so here goes the explanation:
UPDATE: 1 byte saved thanks to a pointer by Dennis
Try it online here
quelle
s
do?<
Character String -
pushes a string.Retina, 43 + 15 = 58 bytes
Retina is a regex language created by Martin Büttner, where the odd-numbered files are the regex to match with, and the even-numbered files are what to replace it with. Each line is a separate file, so I've added 15 bytes for each additional file.
Explanation
It starts by making anything with 3 or more digits an A. It adds a
-
if it is two-digit number ending with 0, 1, or 2, and+
if it ends with 7, 8, or 9. The numbers are then mapped to their grade (e.g. a number starting with 9 is given a B). Any number left over is automatically an F.Unfortunately,Update: version 0.5.0 has intermediate output off by default, allowing me to save a few bytes.;`
must be prepended to all but to the last regex to suppress intermediate output.quelle
\d.*
matches and replaces the whole string,+
included.C, 99 bytes
I'm new here, I hope I'm following the rules.
This function takes the mark as a parameter and returns the grade as a NULL-terminated string.
Explanation
Added whitespace:
Global variables are automatically initialized to zero, so b is filled with NULLs. Since only the first two characters are ever touched, we only have to worry about putting a NULL in b[1] if the grade has only one character. This NULL is inserted at the very beginning of the function. The n parameter is implicitly int. If the grade is less than 60, then it's set to 'F', if it's bigger than 99 it's set to 'A'. In the other cases, the base grade is given by
'E' - (n - 60) / 10
, which simplifies to75 - n / 10
.n % 10
gets the units digit of the mark. If it's less than 3 then a - is appended, if it's bigger than 6 a + is appended, otherwise b[1] is nulled (which it already was).Test cases
quelle
Pyth, 33 bytes
Try it online: Demonstration or Test Suite
Explanation:
quelle
><> (Fish),
7871 bytesMethod:
x,y,z
from the input. If a character is not present the value of its variable's will be-1
implicitly. (ord(c)
will mark the codepoint of the characterc
)z > 0
(3 digit input) printA
and exit.x < ord('6') or y < 0
(input < 60) printF
and exit.123 - x
.y < ord('4') print
-` and exit.y > ord('6') print
+` and exit.quelle
C,
6765Surprisingly, this is quite close to the python solution.
But in order for this program, to come to such great shortness, sacrifices had to be made:
If an
F
or anA
is printedprintf
doesn't even look at the other arguments passed. This is a quite nasty hack.If
(i%10+1)/4
evaluates to1
(no+
or-
should be appended to the grade), the%s
formatter receives a pointer to a\0
byte, so nothing is printed. Also quite funny, because I didn't know that you could take the address of an indexed string literal. (e.g.&"string"[i]
) (edit:"string"+i
is even shorter! Thanks @nutki)Here the output of the program for the numbers 57 to 102. I made it a hexdump, so we can be sure no weird
\0
bytes have been printed.The
main
method used:quelle
&"string"[i]
is unnecessary as it is equivalent to shorter"string"+i
with which you can save 2 bytes.CJam,
41393734 bytesThis is way too long, but I don't think I'll be golfing it further for now.
Test it here. Or run all test cases here.
Three bytes saved by Optimizer.
Explanation
(Slightly outdated)
quelle
GNU sed, 73 + 1 = 74 bytes
The + 1 is for the -r parameter.
quelle
Python 2,
94888469 bytesquelle
JavaScript (ES6), 66 bytes
Straight.
quelle
R,
10710599 bytesNot a very good effort I'm afraid, but I will try and golf it more later.
Edit Dropped a couple of
if
s. Fixed the case and an incorrect result for 100.Now to get rid of the. Got rid ofifelse
sifelse
s.quelle
LETTERS
rather thanletters
.Perl,
6662 bytesThis can probably be golfed more. Also a different way might be better.
+1 for
-p
Run with:
quelle
-p
and drop thesay
?Javascript (ES6),
7879 bytesThis really isn't the smartest option, but I did what I could.
Simply pass the grade as a string, and it will return it's grade letter.
The string part in very important.
You can check a testcase here:
If the aditional space after the letter isn't allowed, I will happily remove it.It wasn't! This increased my code by 1 byte, but nothing (too) serious.quelle
One newline after output is fine, but keep it consistent.
". I think that is consistent enough.C#,
14312711288 bytesI tried to be clever by doing ASCII number mods, but it seems I wasn't alone!
Thanks to Tim for advice on lists instead of ifs.
Thanks to DarcyThomas for pointing out I could use nested ternary operators.
quelle
[item1,item2][condition]
. If the condition is True it gives the 2nd item, if False the first.return <condition> ? <true result> : <false result>
Note you can also nest them soreturn <condition> ? <true result> : <condition> ? <2nd true result> : < 2nd false result>
Haskell, 78 bytes
The first line feels wasteful, costing 14 bytes, but I couldn't find a shorter version without it.
Explanation
The operator
#
is a shorthand for creating n copies of its second argument. Lista
is an infinite list of Strings "A". Functionf
indexes into a list of all grades for n=0,1,... The list comprehension builds the "middle part" of this list (grades E to B);g
is a single Char that is prepended to the Strings
(which may be empty).Usage
quelle
C, 102 bytes
}
Usage
quelle
dc, 52
Output
quelle
TI-Basic,
797476 Bytesquelle
1+
TI-BASIC,
696866 bytesTI-BASIC is not good for string manipulation.
Input on the calculator homescreen, in the form of [number]:[program name].
Formatted:
This can probably be golfed further.
quelle
C#, 82 bytes
Here's a fiddle with some test-cases.
quelle
JavaScript (ES6),
8683 bytesWhat's really eating up characters is the
String.fromCharCode
and the +/- condition... I strongly suspect there's a clever way to shorten at least one of them.quelle
~~(.1*-n+75.9)
-->75.9-n/10|0
PHP5.5, 73 bytes
Once again, not the shortest one.
But it works!
I've classified it under PHP5.5 instead of just PHP since this uses syntax that is only valid for PHP5.5 and PHP5.6.
You can read about string and array dereferencing in the manual:
http://php.net/manual/en/migration55.new-features.php
quelle
Perl, 52
quelle
Ruby, 58 Bytes
Couldn't believe that there are no Ruby ones here. On reflection it's fairly similar to some that are here already but anyway:
Try it here
quelle
Excel, 100 bytes
quelle