4
\$\begingroup\$

You are to make a program that given a number, such as 22009, will go for example to Pi Calculation Code Golf. Then it will, for each answer, try to figure out what the code is, and count its characters. It will then output, for each answer, its number of characters, and a link to it. It should be sorted with fewest characters at the top. For example :

$ answers 22009
5 https://codegolf.stackexchange.com/a/22138/16842
5 https://codegolf.stackexchange.com/a/22188/16842
6 https://codegolf.stackexchange.com/a/22012/16842
6 https://codegolf.stackexchange.com/a/22043/16842

and so on

This is code-golf, so shortest code wins!

Note: If there are multiple things that look like code blocks, it doesn't matter which you choose. It is better though if you look which block actually looks like a program, or you could post a different one for each block.

Note: This specifically says characters. It does not matter what the question actually says, just count characters.

\$\endgroup\$
  • \$\begingroup\$ Since 100% correct is a tough goal, what % correctness are we looking for to count as a "valid" answer? If one answer is shorter and the other is a bit more correct, which wins? \$\endgroup\$ – Geobits Mar 15 '14 at 1:04
  • 1
    \$\begingroup\$ How can we tell which code block is the code? Just find the first one? \$\endgroup\$ – Doorknob Mar 15 '14 at 1:04
  • \$\begingroup\$ We could just count all code blocks and post the score for each block separately. \$\endgroup\$ – 23fc9a62-56de-47fb-97b4-737890 Mar 15 '14 at 1:06
  • \$\begingroup\$ You might try the one with the least whitespace ratio (unless there's the term "whitespace" in the first line of course). That will filter out the common "non-golfed" blocks at least. \$\endgroup\$ – Geobits Mar 15 '14 at 1:10
  • 2
    \$\begingroup\$ Count characters? How about count both characters and bytes? Please run your programs through the sandbox before posting. \$\endgroup\$ – Justin Mar 15 '14 at 4:29
1
\$\begingroup\$

Perl - 233

use LWP::Simple;for($s="http://codegolf.stackexchange.com",$h=get"$s/questions/@ARGV";$h=~/-answerid="(\d+).+?(<div id="answer-|$)/sg;){$a=$&;$i=$1;$_=$1,$_=~s/&.+?;/@/sgm,print length."$u\t$s/a/$i/\n"while$a=~/<pre><code>(.+?)</sg;}

More aerated version:

use LWP::Simple;
$s="http://codegolf.stackexchange.com";
$h=get"$s/questions/@ARGV";
for(;$h=~/-answerid="(\d+).+?(<div id="answer-|$)/gs;){
    $a=$&;
    $i=$1;
    while($a=~/<pre><code>(.+?)</gs){
        $_=$1;
        $_=~s/&.+?;/@/sgm;
        print length."$u\t$s/a/$i/\n"
    }
}

This is my first script in this language... so there's still probably a lot of golfing to do.

\$\endgroup\$
0
\$\begingroup\$

Javascript? (jQuery) 321

This is my first golf in javascript.

var l={};$(".answercell").each(function(){var w=0,c=0,t=$(this),s;t.find("pre code").each(function(){w+=$(this).text().length;c+=1});s=Math.floor(w/c);if(!isNaN(s)){l[t.find(".short-link").prop("href")]=s}});var s=Object.keys(l).sort(function(a,b){return l[a]-l[b]});for(var i=0;i<s.length;i++){console.log(l[s[i]],s[i])}

Ungolfed:

var list = {};
$(".answercell").each(function () {
    var w = 0, c = 0, s;
    t.find("pre code").each(function () {
       w += $(this).text().length;
       c += 1;
    });
    s = Math.floor(w/c);
    if (!isNaN(s)) {
       list[$(this).find(".short-link").prop("href")] = s;
    }
});
var sorted = Object.keys(list).sort(function(a,b){return list[a]-list[b]});
for (var i=0; i<sorted.length; i++) {
    console.log(list[sorted[i]], sorted[i]);
}

Works in the browser console, so maybe it's against the rules, idk. The way it computes for the score is adding all lenghts of code blocks and then dividing by the number of blocks, for each question.

This version, at 257, just counts the first code block:

var l={};$(".answercell").each(function(){var t=$(this),s;s=t.find("pre code").text().length;if(s>0){l[t.find(".short-link").prop("href")]=s}});var s=Object.keys(l).sort(function(a,b){return l[a]-l[b]});for(var i=0;i<s.length;i++){console.log(l[s[i]],s[i])}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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