How many answers does this question have?

16

1

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.

Filip Haglund

Posted 2016-10-15T10:15:41.943

Reputation: 1 789

It's probably too late to rule on this now, but should the count include deleted answers? – Dennis – 2016-10-15T15:28:48.247

2@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. – Filip Haglund – 2016-10-15T15:32:14.277

Can the function accept the url as an input? Would the input then count towards the bytecount? – JungHwan Min – 2016-10-15T16:22:59.777

@JHM yes and yes – Filip Haglund – 2016-10-15T20:46:41.370

@JHM or maybe no, as Dennis points out below one of the answers, it's unfair against browser code to count the url's. If that's the case, then I'd say url constants maybe shouldn't count either. What do you people think? – Filip Haglund – 2016-10-15T20:51:37.723

Please define "output". Do you mean that it returns that integer, or that it outputs the integer in a human readable format? – Makyen – 2016-10-15T21:59:46.413

2@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 – Daniel – 2016-10-15T23:09:27.870

@Dopapp Subtract from byte count is slippery. Some languages have ways to compress the URL. – Dennis – 2016-10-16T03:03:31.580

@Dennis, then maybe it could optionally be taken as input? – Daniel – 2016-10-16T03:04:17.880

1That (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. – Dennis – 2016-10-16T03:05:38.717

@Mayken when the code is executed, I want to see "9" on the screen. If you use a repl, return values are fine, since it prints them to the screen. Also ints vs strings; both are fine (but please don't output quotation marks). – Filip Haglund – 2016-10-16T07:56:08.080

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-questions..., you can, but it probably doesn't help you. – Filip Haglund – 2016-10-16T08:03:38.353

You should add that to the question. This comment is hidden by default, so not everybody will see it before answering. – Dennis – 2016-10-16T14:32:19.207

-1, not enough jQuery in all those answers. – Blackhole – 2016-10-16T18:56:52.140

Answers

14

Javascript + jQuery, 23 bytes

_=>+$("h2>span").text()

Johan Karlsson

Posted 2016-10-15T10:15:41.943

Reputation: 670

Couldn't this fail if there's an h2>span in the body of the challenge? – Martin Ender – 2016-10-15T11:00:19.367

@MartinEnder No, <span>-tags will be stripped: http://meta.stackexchange.com/a/135909

– Johan Karlsson – 2016-10-15T11:10:17.987

3Might be good to add that to the answer then. :) – Martin Ender – 2016-10-15T11:11:14.893

Is the + really needed ? I was thinking that "4" as a string, it's still the integer number of answers – Hedi – 2016-10-15T11:31:56.273

1Could this be made runnable ("run code snippet")? – RudolfJelin – 2016-10-15T20:11:26.367

@RudolfL.Jelínek, No, it will not work from a snippet. Snippets don't have access to the overall page's <document>. They are contained within an <iframe> which is used to make sure anything in the snippet does not impact the rest of the page. – Makyen – 2016-10-15T21:35:39.343

13Note 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. – Makyen – 2016-10-15T22:25:27.990

1@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. – Ismael Miguel – 2016-10-16T14:16:12.050

4

Mathematica, 33 bytes

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

The input is the url of this page.

JungHwan Min

Posted 2016-10-15T10:15:41.943

Reputation: 13 290

4

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]

Daniel

Posted 2016-10-15T10:15:41.943

Reputation: 6 425

1Try using requests library – noɥʇʎԀʎzɐɹƆ – 2016-10-15T15:18:33.737

I think the trailing slash can be omitted...? – TheInitializer – 2016-10-16T02:55:18.320

@TheInitializer, oh yes thanks! – Daniel – 2016-10-16T02:57:06.637

If you change the line import urllib to import urllib as u then you can save 3 bytes – sonrad10 – 2016-10-16T17:36:26.063

@sonrad10, how so? It ends up being the same length, no? – Daniel – 2016-10-16T17:53:18.407

@Dopapp I forgot to consider the whitespace needed to add it in – sonrad10 – 2016-10-16T18:04:20.147

3

Javascript, 67 bytes

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

This look way too long

TuxCrafting

Posted 2016-10-15T10:15:41.943

Reputation: 4 547

3

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.

ASCII-only

Posted 2016-10-15T10:15:41.943

Reputation: 4 687

This isn't much different than the other Javascript answer. In fact, this is simply an un-optimized version of it.

– Ismael Miguel – 2016-10-16T14:43:07.957

@IsmaelMiguel 1. It was written before the other one, and 2. It uses the element before the hidden one the other answer uses. – ASCII-only – 2016-10-16T20:39:32.783

Fair enough. But you can use parseInt($('#answers').text()), which will work. That saves you a few bytes. – Ismael Miguel – 2016-10-16T21:39:24.197

3

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.

Dennis

Posted 2016-10-15T10:15:41.943

Reputation: 196 637

1

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

Roman Czyborra

Posted 2016-10-15T10:15:41.943

Reputation: 604

Count Count has just risen from 5 to 6 ;-) – Roman Czyborra – 2016-10-15T18:11:37.200

1You could replace http:// ... with http://codegolf.stackexchange.com/q/96298 – JungHwan Min – 2016-10-15T18:35:57.027

I preferred zero redirect as I would still be hopelessly behind even with 171-49=122. – Roman Czyborra – 2016-10-15T19:23:59.847

1

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.

LinusKrom

Posted 2016-10-15T10:15:41.943

Reputation: 113

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 – sergioFC – 2016-10-16T08:19:04.670

1

@sergioFC URL shorteners are forbidden by default.

– Dennis – 2016-10-16T14:33:18.823

@Dennis Didn't knew that, thank you. – sergioFC – 2016-10-16T14:41:45.473

1

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];

Roman Gräf

Posted 2016-10-15T10:15:41.943

Reputation: 2 915

1

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)

SuperJedi224

Posted 2016-10-15T10:15:41.943

Reputation: 11 342

0

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.

Makyen

Posted 2016-10-15T10:15:41.943

Reputation: 101

0

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))

Billywob

Posted 2016-10-15T10:15:41.943

Reputation: 3 363