16
\$\begingroup\$

What makes the ideal question? Perhaps it is merely the abstraction of a profound thought and the initialization of a dream, a hope, a thought, a (… 5,024.2 characters omitted). Therefore, I propose that we find such a factor to quantify questions. I propose also that this factor be called QF, or the Q-factor. I determine a question's Q-factor as thus:

(Recall that .)

Objective Given a number as input, determine the respective question's Q-factor on PPCG.SE. If the question does not exist, simply output ;-; (the crying emote).

Take, for example, . (this question):

The net votes, in this case, means .

views     = 23435
net votes = 33
edits     = 6
answers   = 30, answers^2 = 900
comments  = 19
charsIOPN = "Joe Z.".length = 6

So:

r_F = 6 / 6 = 1
Q_F = r_F * (23435 * 33 + 6) / (900 - 19)
    =  1  *      773361      /    881
    ~ 877.821793

Please provide at least 2 significant figures on QF.

This is a , so the shortest program in bytes wins.


Note: Please include three examples of your program running, one of which must be this question. The other two are yours to decide. (This is just a confirmation check.) Also report the time at which you entered the query.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ @AlexA. Either is fine. And look at my rep! :D It's a rep digit! (It's 888, in case someone upvotes.) \$\endgroup\$ Oct 13, 2015 at 2:29
  • 3
    \$\begingroup\$ It may be of interest to note that the SE API does not expose the number of edits on a post. \$\endgroup\$
    – Alex A.
    Oct 13, 2015 at 2:55
  • 2
    \$\begingroup\$ Alex makes a good point about the edits. It might require just as much code to get the number of edits as all the other stats combined since there's no quick way to use the API for it. Also, can url shorteners be used? \$\endgroup\$ Oct 13, 2015 at 3:07
  • 1
    \$\begingroup\$ @DJMcMayhem Comments make it a more valuable question- they make the denominator smaller. \$\endgroup\$
    – Daniel M.
    Oct 13, 2015 at 3:29
  • 5
    \$\begingroup\$ While I might not know what the question is, I'm pretty sure the answer is 42. \$\endgroup\$
    – Status
    Oct 13, 2015 at 16:38

4 Answers 4

11
\$\begingroup\$

Julia, 411 382 367 355 353 bytes

It's quite long but I'm very pleased that it works!

using Requests
n->(R(t)=readall(get(t));G(t)=JSON.parse(R("http://api.stackexchange.com/2.2/questions/$t?site=codegolf"))["items"];j=G(n)[1];d=parse(Int,match(r"<span \S+n (\d+)",R("http://codegolf.xyz/posts/$n/revisions")).captures[1]);d/length(j["owner"]["display_name"])*(j["view_count"]*j["score"]+d)/(j["answer_count"]^2-length(G("$n/comments"))))

This creates an unnamed function that takes an integer as input and returns a float.

It requires the Requests package, which can be installed using Pkg.add("Requests"). That package provides methods for Base.get() for submitting HTTP requests. It also imports the JSON package (on which it depends, so that doesn't need to be installed separately) which we use to parse the JSON output in the response.

Ungolfed:

using Requests

function f(n::Int)
    # Define a function to submit an HTTP request and read
    # the response, returning a string
    R(t) = readall(get(t))

    # Define a function that submits a request, reads all text
    # as JSON, parses it, and extracts the items field
    G(t) = begin
        api = "http://api.stackexchange.com/questions"
        JSON.parse(R("$api/$t?site=codegolf"))["items"]
    end

    # Get the data for the question using the API
    j = G(n)[1]

    # Scrape the number of edits from the revision history HTML
    # using minxomat's handy shortened PPCG URL
    h = R("http://codegolf.xyz/posts/$n/revisions")
    d = parse(Int, match(r"<span \S+n (\d+)", h).captures[1])

    # Compute the coefficient r_F
    r = d / length(j["owner"]["display_name"])

    # Compute the Q-factor
    Q = r * (j["view_count"] * j["score"] + d) /
        (j["answer_count"]^2 - length(G("$n/comments")))

    return Q
end

Thanks to Dennis and Martin Büttner for golfing help!

\$\endgroup\$
2
  • \$\begingroup\$ Nice! Any place I can test this? \$\endgroup\$ Oct 13, 2015 at 11:04
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ For best results I recommend installing Julia 0.4.0 (currently the latest release) on your machine and running it locally. You can give the lambda a name like f=n->... and submit f(16327) at the REPL or println(f(16327)) if you're running it from a .jl file. It works on JuliaBox (requires a Google account) with Julia 0.4.0-rc2 but gives warnings when precompiling the Requests module. \$\endgroup\$
    – Alex A.
    Oct 13, 2015 at 19:12
3
\$\begingroup\$

Mathematica 10, 381 bytes

f=(a=StringTemplate["http://api.stackexchange.com/``````?site=codegolf"];j=Import[a["questions/",#,""],"JSON"];c=Import[a["questions/",#,"/comments"],"JSON"];r=Import[a["posts/",#,"/revisions"],"JSON"];N[#5/#6*(#1*#2+#5)/(#3^2-#4)]&@@j[[3,2,1,{5,2,12},2]]~Join~{Length@c[[3,2]],Length@DeleteCases[r[[3,2]],_?(("revision_type"/.#)=="vote_based"&)],StringLength@j[[3,2,1,3,2,6,2]]})&

Just three API queries and a lot of indexing, really. Hardest part was trying to understand how to get edits from the available revisions, hopefully I got it right.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This doesn't work for me in Mathematica 9. It says the StringTemplate argument is invalid, prints a bunch of errors, and doesn't return a result. \$\endgroup\$
    – Alex A.
    Oct 14, 2015 at 0:17
  • \$\begingroup\$ Sorry, I forgot to put the version number into the title. Fixed! \$\endgroup\$
    – user46060
    Oct 14, 2015 at 0:27
  • \$\begingroup\$ So it depends on features unique to Mathematica 10? \$\endgroup\$
    – Alex A.
    Oct 14, 2015 at 1:16
  • \$\begingroup\$ You don't have StringTemplate before 10, as far as I know. You can replace that part with a=Function["http://api.stackexchange.com/"<>#1<>ToString[#2]<>#3<>"?site=codegolf"]; and see if it works. \$\endgroup\$
    – user46060
    Oct 14, 2015 at 7:20
2
\$\begingroup\$

Python 2, 392 Bytes

Well, I gave it a shot.

from requests import*;from re import*;l=len
def i(n):
 try:s,r="http://api.stackexchange.com/2.2/questions/%s?site=codegolf","http://codegolf.xyz/posts/%i/revisions"%n;q,c=s%n,s%('%i/comments'%n);i=get(q).json()['items'][0];m=float(l(findall("<span \S+n (\d+)",get(r).text)));r=m/l(i["owner"]["display_name"]);print r*(i["view_count"]*i["score"]+m)/(i["answer_count"]**2-m)
 except:print';-;'

Very similar logic to Alex's Julia answer. I'd like to loop through this to see which question is the most ideal but I'd rather not continually call the api for hours on end.

\$\endgroup\$
2
  • \$\begingroup\$ Requests is a third-party package. Since when were those allowed. \$\endgroup\$ Oct 15, 2015 at 13:03
  • 1
    \$\begingroup\$ @ppperry Since always unless explicitly disallowed by the OP. Using external resources that exactly solve the challenge is forbidden by default but that is not the case here. \$\endgroup\$
    – Alex A.
    Oct 15, 2015 at 23:24
2
\$\begingroup\$

Groovy, 459 457 bytes

Pretty much like the rest of the answers.

import groovy.json.JsonSlurper
import java.util.zip.GZIPInputStream
def f={n->def j,d,u={p->new JsonSlurper().parseText(new GZIPInputStream("http://api.stackexchange.com/2.2/questions/$p?site=codegolf".toURL().getContent()).getText()).items}
j=u(n)[0]
d=("http://codegolf.xyz/posts/$n/revisions".toURL().text=~/<span \S+n (\d+)/).getCount()
println((d/j.owner.display_name.length())*(j.view_count*j.score+d)/(j.answer_count**2-u("$n/comments").size()))}

Saved 2 bytes thanks to Cᴏɴᴏʀ O'Bʀɪᴇɴ!

Ungolfed:

import groovy.json.JsonSlurper
import java.util.zip.GZIPInputStream

def f = { n->

    def stackApi = "http://api.stackexchange.com/2.2"

    // Fetch json from stackexchange rest api
    def getItems = { pathParam ->
        //Stackexchange compresses data, decompress before parsing json
        def httpData = "$stackApi/questions/$pathParam?site=codegolf".toURL().getContent()
        def deCompressedData = new GZIPInputStream(httpData).getText()
        def json = new JsonSlurper().parseText(deCompressedData)
        return json.items
    }

    // Get the edit count from the html page
    def htmlPage = "http://codegolf.xyz/posts/$n/revisions".toURL()
    def editCount = (htmlPage.text=~/<span \S+n (\d+)/).getCount()

    // apply formula
    def json = getItems(n)[0]
    def r = editCount/json.owner.display_name.length()
    def Q = r * ( json.view_count * json.score + editCount) /
                (json.answer_count**2 - getItems("$n/comments").size())
    println(Q)
}

f(16327)
\$\endgroup\$
0

Your Answer

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

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