17
\$\begingroup\$

This question is inspired by the fact that I love seeing questions with equal vote and answer counts...


So here's a simple challenge for y'all:

Challenge:

Given a codegolf.stackexchange question id, output the ratio between the question's votes and number of answers (e.g. votes/answers).

Specifics:

  • You may access the internet, but you may only access stackexchange.com and its various sub-domains. You may not use URL shorteners.

  • You may take input and give output in any standard format.

  • You must output the ratio as a decimal number in base 10, with at least 4 {accurate} digits after the decimal (zeros may be truncated).

  • If the question is unanswered, your program may produce undefined behavior.

  • You should use the score of the question as the vote-count, see here.

This is , least bytes in each language wins for that language, least bytes overall wins overall.

Here is a sample program in Python 3 + requests:

import requests
import json

id = input("id> ")
url = "https://api.stackexchange.com/2.2/questions/" + id + "?site=codegolf"
content = requests.get(url).text
question = json.loads(content)["items"][0]

print(float(question["score"]) / question["answer_count"])
\$\endgroup\$
  • \$\begingroup\$ Does the ratio need at least 4 decimal digits after the decinal point even if they are zero? E.g. 41/4= 10.25 or 10.2500 \$\endgroup\$ – pizzapants184 Sep 18 '17 at 15:19
  • \$\begingroup\$ @pizzapants184 10.25 is fine \$\endgroup\$ – Socratic Phoenix Sep 18 '17 at 15:21
  • \$\begingroup\$ What if the challenge is not answered? Then the ratio would be infinite? \$\endgroup\$ – Mr. Xcoder Sep 18 '17 at 15:37
  • \$\begingroup\$ Do you mean score or total votes on the question? \$\endgroup\$ – AdmBorkBork Sep 18 '17 at 15:37
  • \$\begingroup\$ @Mr.Xcoder in that case, undefined behavior is okay \$\endgroup\$ – Socratic Phoenix Sep 18 '17 at 15:37
10
\$\begingroup\$

JavaScript (ES6), 103 102 bytes

Needs to be run from the root level of api.stackexchange.com. Returns a Promise object containing the result.

n=>fetch(`questions/${n}?site=codegolf`).then(r=>r.json()).then(({items:[j]})=>j.score/j.answer_count)

If requiring that it be run from a specific path is allowed then that becomes 92 90 bytes.

n=>fetch(n+`?site=codegolf`).then(r=>r.json()).then(({items:[j]})=>j.score/j.answer_count)

Try it

Full URL added to enable it to work here.

f=
n=>fetch(`//api.stackexchange.com/questions/${n}?site=codegolf`).then(r=>r.json()).then(({items:[j]})=>j.score/j.answer_count)
onchange=_=>f(+i.value).then(t=>o.innerText=t)
<input id=i type=number><pre id=o>

\$\endgroup\$
  • 7
    \$\begingroup\$ Replace i=>(j=i.items[0]) with ({items:[j]})=>j to save a byte. \$\endgroup\$ – kamoroso94 Sep 18 '17 at 20:14
  • \$\begingroup\$ Nice trick, thanks, @kamoroso94. I'll have to remember that one. \$\endgroup\$ – Shaggy Sep 18 '17 at 22:25
7
\$\begingroup\$

Stratos, 40 bytes

-4 bytes thanks to Shaggy

f"¹⁵s/%²"r"⁷s"@0
{s"answer_⁰"
⁰s"score"/

Try it!

Stratos specialises in questions.

Explanation:

The code decompresses to the following:

f"api.stackexchange.com/questions/%?site=codegolf"r"items"@0
{s"answer_count"
⁰s"score"/

Starting from the first line, Stratos evaluates the dyads from right to left.

f"api.stackexchange.com/questions/%?site=codegolf"r"items"@0 means "evaluate the dyad @ with the left-hand argument f"api.stackexchange.com/questions/%?site=codegolf"r"items" and the right-hand argument 0. @ gets the nth element of a JSON array.

To evaluate f"api.stackexchange.com/questions/%?site=codegolf"r"items", we will next evaluate the dyad r with the left-hand argument f"api.stackexchange.com/questions/%?site=codegolf" and the right-hand argument "items". r gets the JSON array with the specified name.

Next, we will need to evaluate f"api.stackexchange.com/questions/%?site=codegolf". First, % is replaced with the input. f means "get the contents of this URL".

Now, we can move on to the second line. The newline means "add what we evaluated to the implicit argument list"

Next, we evaluate s (get string in JSON object with a certain name) with { and "answer_count". { takes an element from the implicit argument stack, returns it, and adds it back to the stack.

Then, we add the output of that to the implicit argument stack.

To evaluate ⁰s"score"/, we are applying the dyad / (divide) to ⁰s"score" and an element from the implicit argument stack.

To evaluate ⁰s"score" we are getting the string "score" from the JSON object from the 0th element in the implict argument stack.

Now, the output of / is printed and the program terminates.

\$\endgroup\$
  • \$\begingroup\$ Save 4 bytes by ditching the API version (2.2/). \$\endgroup\$ – Shaggy Sep 18 '17 at 17:14
  • 1
    \$\begingroup\$ @Shaggy Didn't realise the API version wasn't required, thanks. Not crossing out 44 :P \$\endgroup\$ – Okx Sep 18 '17 at 17:18
5
\$\begingroup\$

R + jsonlite, 111 bytes

function(n,x=jsonlite::fromJSON(sprintf('http://api.stackexchange.com/questions/%s?site=codegolf',n))$i)x$s/x$a

R-fiddle link

jsonlite is a nice JSON <-> R conversion library which works quite well. I wasn't about to golf a JSON parser for R...

\$\endgroup\$
  • \$\begingroup\$ You can save a byte by using the http protocol. Also, I'm guessing 142729 shouldn't be hardcoded in there? \$\endgroup\$ – Shaggy Sep 18 '17 at 15:50
  • \$\begingroup\$ @Shaggy good call, thank you. I copied and pasted too fast. \$\endgroup\$ – Giuseppe Sep 18 '17 at 15:53
4
\$\begingroup\$

T-SQL, 64 54 bytes

It is rare that SQL can beat (most) other languages! Instead of the API URL I went directly to the Stack Exchange Data Explorer:

SELECT 1.0*Score/AnswerCount FROM Posts WHERE Id=##i##

The ##i## isn't standard SQL, that's Stack Exchange's format to prompt for input.

Note that the data explorer source is only updated nightly, so values aren't current.

Will throw a divide by zero error on questions with no answers.

Edit: Saved 10 bytes by multiplying by 1.0 instead of an explicit CONVERT to FLOAT.

\$\endgroup\$
  • \$\begingroup\$ can beat others, but not all :) \$\endgroup\$ – Okx Sep 18 '17 at 19:58
  • \$\begingroup\$ Oh maaan, I had another solution but I was in school so I never ended up posting it... Is my answer too similar to yours? Should I delete mine? \$\endgroup\$ – totallyhuman Sep 18 '17 at 20:05
  • \$\begingroup\$ @icrieverytim Ha, yep looks like pretty much the same query, although I just edited mine to save 10 more bytes. \$\endgroup\$ – BradC Sep 18 '17 at 20:11
  • \$\begingroup\$ @BradC I edited my answer into a Mathematica answer lol. \$\endgroup\$ – totallyhuman Sep 18 '17 at 20:50
3
\$\begingroup\$

PowerShell, 130 bytes

($a=(ConvertFrom-Json(iwr("http://api.stackexchange.com/questions/"+$args+"?site=codegolf")).content).items).score/$a.answer_count

Performs an Invoke-WebRequest against the URL, gets the .content thereof, does a ConvertFrom-Json of that content, and gets the .items of that JSON object. Stores that into $a and pulls out the .score as the numerator. The denominator is the .answer_count. That value is left on the pipeline and output is implicit.

If the question is unanswered, this should toss a "Divide by zero" error.

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

Japt, 83 82 bytes

Wanted to give this a try to see how it would work out, seeing as Japt can't natively accomplish it. Essentially all this is doing is evaling a compressed version of my JS solution. As Japt is JS then we can require that this be run from the root level of api.stackexchange.com and also return a Promise object containing the result.

Ox`fet®("quÀËs/{U}?ÐÒ=¬¸golf").È(r=>r.js()).È(i=>(j=i.ems[0]).sÖ/j.s³r_Öt)
  • View it
  • Try it - the extra bytes in this version are accounted for by the inclusion of //api.stackexchange.com/ in the URL and of console.log so you can actually see it working
\$\endgroup\$
  • 1
    \$\begingroup\$ o0 Why have you made the code a link? It even messes with the userscript's byte count display lol. i.imgur.com/SO2zgAy.png \$\endgroup\$ – totallyhuman Sep 18 '17 at 20:54
1
\$\begingroup\$

Python 3 + requests, 149 bytes

-1 byte thanks to Mr. Xcoder.

from requests import*
u=get('http://api.stackexchange.com/questions/%s?site=codegolf'%input()).json()['items'][0]
print(u['score']/u['answer_count'])
\$\endgroup\$
  • \$\begingroup\$ The link is to Python 2. Btw, it throws an error both in Python 2 and Python 3 (on TIO). \$\endgroup\$ – user72349 Sep 18 '17 at 15:38
  • \$\begingroup\$ No internet access on TIO whoops. I just used TIO for the answer formatting lol. \$\endgroup\$ – totallyhuman Sep 18 '17 at 15:42
  • \$\begingroup\$ I think that (for now) http suffices. \$\endgroup\$ – Mr. Xcoder Sep 18 '17 at 15:49
  • \$\begingroup\$ 125 bytes \$\endgroup\$ – LyricLy Sep 19 '17 at 9:27
1
\$\begingroup\$

Mathematica, 124 bytes

N@("score"/.(u="items"/.Import["http://api.stackexchange.com/questions/"<>#<>"?site=codegolf","JSON"]))/("answer_count"/.u)&

Mathematica has a data type called Rule and it confuses the heck out of me. :P

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

PHP, 167 bytes

<?$f=json_decode(gzdecode(file_get_contents('https://api.stackexchange.com/2.2/questions/'.$argv[1].'?site=codegolf')))->items[0];echo $f->score/$f->answer_count;

Turbo-fast crack at this. Save as a file and execute in the terminal like so:

php -f file.php 143083

Might be a way to reduce this.

\$\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.