17
\$\begingroup\$

Write a program or a function that outputs the integer number of answers this question has. Your solution should still work as more answers are added.

Languages that run in a browser may be run from the js console while on this page. Otherwise, you'd probably have to download this page. Multi-language solutions, e.g. wgeting the page and parsing it using grep is ok. Just sum up the command line and all source used.

This is code golf; shortest answer in bytes wins.

EDIT:

Let's allow at most one occourance of the url of this page to not count, either as input or in the source. No url shorteners, no data stored as get parameters etc. Quotes around url, if needed, still count. Taking the url as input is also fine. If you want to remove some part of the url, e.g. /how-many-answers..., you can, but it probably doesn't help you.

\$\endgroup\$
14
  • \$\begingroup\$ It's probably too late to rule on this now, but should the count include deleted answers? \$\endgroup\$
    – Dennis
    Oct 15, 2016 at 15:28
  • 2
    \$\begingroup\$ @Dennis I cannot see deleted answers, so I would say nobody has to worry about them. Count them if you want to. I won't count them since I cannot see them. \$\endgroup\$ Oct 15, 2016 at 15:32
  • \$\begingroup\$ Can the function accept the url as an input? Would the input then count towards the bytecount? \$\endgroup\$ Oct 15, 2016 at 16:22
  • 2
    \$\begingroup\$ @FilipHaglund, I think that it would make most sense to have the URL byte count in the code be subtracted from the total count or have it be taken as input because that sort of makes it impossible for languages that don't run in the browser to have a chance at winning \$\endgroup\$
    – Daniel
    Oct 15, 2016 at 23:09
  • 1
    \$\begingroup\$ That (although in some languages taking input could require even more bytes) or only allow to subtract if the URL http://... appears verbatim in the source code. \$\endgroup\$
    – Dennis
    Oct 16, 2016 at 3:05

12 Answers 12

15
\$\begingroup\$

Javascript + jQuery, 23 bytes

_=>+$("h2>span").text()
\$\endgroup\$
8
  • \$\begingroup\$ Couldn't this fail if there's an h2>span in the body of the challenge? \$\endgroup\$ Oct 15, 2016 at 11:00
  • 3
    \$\begingroup\$ Might be good to add that to the answer then. :) \$\endgroup\$ Oct 15, 2016 at 11:11
  • 1
    \$\begingroup\$ Could this be made runnable ("run code snippet")? \$\endgroup\$
    – user60199
    Oct 15, 2016 at 20:11
  • 13
    \$\begingroup\$ Note that this uses jQuery which is not pure JavaScript (i.e. it should not be labeled as just "JavaScript"). Using a library which is included on top of the language should at least be explicitly stated. I'm not arguing against using jQuery, just that it should be stated. \$\endgroup\$
    – Makyen
    Oct 15, 2016 at 22:25
  • 1
    \$\begingroup\$ @Makyen StackOverflow's network uses jQuery. The question says "Languages that run in a browser may be run from the js console while on this page.". jQuery doesn't have to be loaded, since it already was. Maybe that's why jQuery isn't specified. \$\endgroup\$ Oct 16, 2016 at 14:16
4
\$\begingroup\$

Mathematica, 33 bytes

Length@Import[#,"Data"][[4,2]]-1&

The input is the url of this page.

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

Python 2, 120 bytes, 79 w/o URL

I can't say Python was made for this challenge.

import urllib
print[l for l in urllib.urlopen("http://codegolf.stackexchange.com/q/96298")if"answerCount"in l][0][83:-9]

Unfortunately, inline import is the same length :(

Any help with golfing this further would be greatly appreciated!

If the URL (a whopping 41 bytes—over 1/3 my byte count) can be taken as input, it is 82 bytes:

import urllib
lambda u:[l for l in urllib.urlopen(u)if"answerCount"in l][0][83:-9]
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Try using requests library \$\endgroup\$ Oct 15, 2016 at 15:18
  • \$\begingroup\$ I think the trailing slash can be omitted...? \$\endgroup\$ Oct 16, 2016 at 2:55
  • \$\begingroup\$ @TheInitializer, oh yes thanks! \$\endgroup\$
    – Daniel
    Oct 16, 2016 at 2:57
  • \$\begingroup\$ If you change the line import urllib to import urllib as u then you can save 3 bytes \$\endgroup\$
    – AvahW
    Oct 16, 2016 at 17:36
  • \$\begingroup\$ @sonrad10, how so? It ends up being the same length, no? \$\endgroup\$
    – Daniel
    Oct 16, 2016 at 17:53
3
\$\begingroup\$

Javascript, 67 bytes

alert($(".answers-subheader").children().first().children().html())

This look way too long

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

Javascript (ES5), 46 44 40 38 33 bytes

_=>parseInt($('#answers').text())

5 bytes saved thanks to Ismael Miguel

Note: This is pretty slow, and won't work if you have the PPCG-Design userscript.

\$\endgroup\$
3
  • \$\begingroup\$ This isn't much different than the other Javascript answer. In fact, this is simply an un-optimized version of it. \$\endgroup\$ Oct 16, 2016 at 14:43
  • \$\begingroup\$ @IsmaelMiguel 1. It was written before the other one, and 2. It uses the element before the hidden one the other answer uses. \$\endgroup\$
    – ASCII-only
    Oct 16, 2016 at 20:39
  • \$\begingroup\$ Fair enough. But you can use parseInt($('#answers').text()), which will work. That saves you a few bytes. \$\endgroup\$ Oct 16, 2016 at 21:39
3
\$\begingroup\$

CJam, 15 bytes

lg"2>"/1=A>S/0=

Expects this page's URL as input.

How it works

l                e# Read a line (the URL) from STDIN.
 g               e# Fetch the resource the URL points to.
  "2>"/          e# Split the source at occurrences of "2>".
       1=        e# Select the second chunk, i.e., everything between the first
                 e# <h2> and the first </h2>.
         A>      e# Discard the first 10 characters (a linefeed and 9 tabs).
           S/0=  e# Split at spaces and select the first chunk.
\$\endgroup\$
1
\$\begingroup\$

171 bytes bash + 3 keys lynx

lynx -cfg=<(echo PRINTER:Answercount:grep [0-9]*.Answers %s|less:FALSE:999') http://codegolf.stackexchange.com/questions/96298/how-many-answers-does-this-question-have
\$\endgroup\$
3
  • \$\begingroup\$ Count Count has just risen from 5 to 6 ;-) \$\endgroup\$ Oct 15, 2016 at 18:11
  • 1
    \$\begingroup\$ You could replace http:// ... with http://codegolf.stackexchange.com/q/96298 \$\endgroup\$ Oct 15, 2016 at 18:35
  • \$\begingroup\$ I preferred zero redirect as I would still be hopelessly behind even with 171-49=122. \$\endgroup\$ Oct 15, 2016 at 19:23
1
\$\begingroup\$

99 bytes sh + curl + jq + stackexchange API

curl -s --compressed api.stackexchange.com/questions/96298/answers?site=codegolf|jq .items\|length

Using the API, I was able to avoid issues related to page formatting and html. Unfortunately, 60 bytes of my answer are the maximally golfed url for this particular api query, and another 13 bytes for curl to unzip the resulting of the query, because stackexchange refuses to serve uncompressed data via the api.

The actual "logic" comes from curling the api to ask for a json reply with the answers to this post. That is unzipped and then piped into jq, a json parser, which extracts the "items" array and outputs its length.

You can get impressively close to having the api just return the number of answers, but from what I could come up with you could not get 100% of the way there, and getting closer would cost more bytes than just passing it through jq.

101 bytes to return {"total":}:

curl -s --compressed api.stackexchange.com/questions/96298/answers?site=codegolf&filter=!)V)MSZJUgX_

The filter parameter in api queries is very powerful, but it falls just short of providing a "just curl a url" solution. There may be a middle ground here, where you can get a shorter response and then just count the lines or extract the number, but unfortunately filter strings are a set length, and the required jq command was already more efficient.

\$\endgroup\$
3
  • \$\begingroup\$ Cool. Note that you can save many bytes ussing an URL shortener and adding the -L option to make curl follow redirects; for example curl -sL --compressed bit.ly/2ebw404|jq .total \$\endgroup\$
    – sergioFC
    Oct 16, 2016 at 8:19
  • 1
    \$\begingroup\$ @sergioFC URL shorteners are forbidden by default. \$\endgroup\$
    – Dennis
    Oct 16, 2016 at 14:33
  • \$\begingroup\$ @Dennis Didn't knew that, thank you. \$\endgroup\$
    – sergioFC
    Oct 16, 2016 at 14:41
1
\$\begingroup\$

PHP, 76 (Code) + 41 (URL) = 117 bytes

preg_match_all('<h2>(.*) answers<span',file_get_contents("http://codegolf.stackexchange.com/q/96298"),$o);
echo $o[0];
\$\endgroup\$
1
\$\begingroup\$

Java, 230 269-41=228 bytes

interface A{static void main(String[]a)throws Exception{System.out.print(new java.util.Scanner(new java.net.URL("http://codegolf.stackexchange.com/q/96298").openStream()).useDelimiter("\\Z").next().replaceAll("\n|\r","").replaceAll("^.+?\\s+(\\d+) Answers.+$","$1"));}}

(Only counts non-deleted answers)

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

JavaScript + jQuery (already included in page), 20 bytes

+$('h2>span').text()

This is a program intended to be executed in the console for the current page (opened with F12). It outputs the number of answers, without quotation marks. It works in Chrome, Firefox, IE11, and Edge. It should work in other browsers, but I have only tested it in those listed.

Unlike other JavaScript solutions here, it is a program by itself rather than a function expression using ES6 arrow function notation. Also unlike those solutions, it actually produces output (without quotes) in the console with the number of answers (rather than just being a function expression which produces no output). Producing output relies on the feature in each browser's console that the value of an expression is output after that expression has been evaluated.

This relies on the only <span> on the page with a parent that is an <h2> element is the one containing the number of answers as its text content. After experimentation, this appears to be the case, and it does not appear possible to intentionally create an <h2> with a <span> child using the editor: all explicitly inserted <span> elements are stripped and no Markdown, as used on Stack Exchange, creates an actual <span> within an <h2>. If someone can demonstrate a case where the page can be manipulated such that 'h2>span' selector is no longer unique, then this will need to be revised.

If evaluating to a string instead of a number is acceptable, then

JavaScript + jQuery (already included in page), 19 bytes

$('h2>span').text()

However, while this does not evaluate to include "", in all tested consoles it produces output which is enclosed within "". I read Filip Haglund's comments on the question as precluding this output.

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

R, 80 bytes

Answer is based on searching the vector returned by readLines using regular expressions. I'm guessing that this could be broken by text in comments/answers (possibly even my own). Will delete if so. Also the pattern could also be gofled but not sure if that would increase the likelihood of getting a false value.

x=readLines(scan(,""));regmatches(x,regexpr("(?<=answerCount\">).*?(?=<)",x,,T))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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