15
\$\begingroup\$

According to legend, almost everyone is outgolfed by Dennis. If not, they will be.

Now I'm curious if I'm one of those 'almost everyone'.

You will be given a link to an answer in the format of https://codegolf.stackexchange.com/questions/[QUESTION ID]/[QUESTION NAME]/#[ANSWER ID].

Find the length of the code, which we will assume as the last number on the first line (markdown wise) of the answer in the link.

Then, search for Dennis' answers, and do the same.

Now compare the input's and Dennis' answers code length, and if it is out-golfed (meaning one or more answer of Dennis' is shorter than that of the input answer), output a truthy value, and if not, a falsy value.

If there is no answer belonging to Dennis on the same question as the answer, output a falsy value.

Examples

  • Link : true
  • Link : false (At least for now, tell me when it changes)

Rules

  • You may use any form of truthy/falsy value.
  • The question of the answer you're given will always be .
  • The question may not have an answer of Dennis', but the inputted answer will never belong to Dennis.

  • You may assume there is always a number on the first line of the answer.

\$\endgroup\$
  • 3
    \$\begingroup\$ Can we assume there will always be an answer by Dennis on the given question, and that the given answer will not be Dennis' answer? \$\endgroup\$ – Skidsdev May 10 '17 at 10:42
  • \$\begingroup\$ @Mayube No, and yes. \$\endgroup\$ – Matthew Roh May 10 '17 at 10:42
  • \$\begingroup\$ Do we need to handle paging or can we assume both answers will be on the same page if there are multiple pages of answers? \$\endgroup\$ – Shaggy May 10 '17 at 10:42
  • \$\begingroup\$ @Shaggy former. \$\endgroup\$ – Matthew Roh May 10 '17 at 10:43
  • \$\begingroup\$ @SIGSEGV If there is no answer by Dennis do we output false because Dennis has not outgolfed the answer? \$\endgroup\$ – Skidsdev May 10 '17 at 10:43
1
\$\begingroup\$

Python 3.6 + requests + bs4 - 363 358 bytes

import bs4,re,requests
u,n=input().split("/#");i=1;d=y=float("inf")
while i:
 A=bs4.BeautifulSoup(requests.get(u+f"?page={i}").text,"html.parser")(class_="answer")
 for a in A:
  c=int(re.findall("\d+",(a("h1")+a("h2")+a("p"))[0].text)[-1])
  if "Dennis"in a(class_="user-details")[-1].text:d=min(c,d)
  if a["data-answerid"]==n:y=c
 i=A and i+1;
print(d<y)

Prints True or False.

Note: does not currently work on the second link because of invalid HTML produced by this answer (the em and strong tags are terminated in the wrong order at the end of the second line, and causes the parser to miss the username block). Try it on this link instead.

Using the API - 401 380 bytes

import requests,re
q,A=re.findall("\d+",input());i=1;d=y=float("inf")
while i:
 r=requests.get(f"https://api.stackexchange.com/2.2/questions/{q}/answers?site=codegolf&filter=withbody&page={i}").json();i=r["has_more"]and i+1
 for a in r["items"]:
  c=int(re.search("(\d+)\D+$",a["body"]).group(1))
  if a["owner"]["user_id"]==12012:d=min(d,c)
  if a["answer_id"]==A:y=c
print(d<y)

Note that this also fails on the second link, but because one answer started with This may be foul play. instead of the header...

\$\endgroup\$
  • \$\begingroup\$ Consider using the Stack Exchange API to get answer data instead via JSON parser, just make sure filter=withbody is in your GET request in order to obtain the answer body to grab the byte count \$\endgroup\$ – Value Ink May 10 '17 at 19:57
  • \$\begingroup\$ With the API version, you used a twice (once to set the answer ID, and another when iterating r["items"], which would lead to incorrect behavior. Also, you don't need to set the pagesize (it will default to 30). c=int(re.search(r'(\d+) bytes').group(1)) should give you a more accurate retrieval of bytecount and is shorter, and if it still fails, (\d+)\s*bytes does the trick but is a bit longer. Finally, i=r["has_more"]and i+1 is shorter than your ternary conditional. \$\endgroup\$ – Value Ink May 10 '17 at 21:23
  • \$\begingroup\$ Actually, forget about the regex match. I forgot to account for adding the answer body in the function call so that part is longer. The other suggestions are still valid, though. \$\endgroup\$ – Value Ink May 11 '17 at 1:19
  • \$\begingroup\$ @ValueInk Thanks, bit shorter now. Looks like we need a golfing language just for the API, its calls are sooo long... \$\endgroup\$ – matsjoyce May 11 '17 at 7:28
  • \$\begingroup\$ Perhaps. But honestly there's also the fact that you have a nice HTML parser, plus the fact that you're given the question URL by default. If the input were two numbers instead, there would be quite a bit of overhead for getting the page for the codegolf question as well. \$\endgroup\$ – Value Ink May 11 '17 at 19:58
1
\$\begingroup\$

Ruby, 314 315 308+20 = 334 335 328 bytes

Uses the flags -n -rjson -ropen-uri. +1 byte from fixing a minor bug.

-7 bytes by discovering the open-uri Ruby default library.

~/(\d+)\D+(\d+)/
u="http://api.stackexchange.com/2.2/questions/#$1/answers?site=codegolf&filter=withbody&page=%s"
n=eval$2
a="answer_id"
j=1
o=[]
(o+=r=JSON.parse(open(u%j).read)["items"]
j=r!=[]&&j+1)while j
p o.select{|e|e["owner"]["user_id"]==12012||e[a]==n}.min_by{|e|e["body"][/\d+\s*bytes/].to_i}[a]!=n
\$\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.