Am I outgolfed by Dennis?

15

2

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.

Matthew Roh

Posted 2017-05-10T10:39:55.847

Reputation: 5 043

3Can we assume there will always be an answer by Dennis on the given question, and that the given answer will not be Dennis' answer? – Skidsdev – 2017-05-10T10:42:08.413

@Mayube No, and yes. – Matthew Roh – 2017-05-10T10:42:34.940

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? – Shaggy – 2017-05-10T10:42:45.510

@Shaggy former. – Matthew Roh – 2017-05-10T10:43:06.450

@SIGSEGV If there is no answer by Dennis do we output false because Dennis has not outgolfed the answer? – Skidsdev – 2017-05-10T10:43:43.693

@Mayube Yes.... – Matthew Roh – 2017-05-10T10:44:27.500

Answers

1

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

matsjoyce

Posted 2017-05-10T10:39:55.847

Reputation: 1 319

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

– Value Ink – 2017-05-10T19:57:30.747

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. – Value Ink – 2017-05-10T21:23:24.017

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. – Value Ink – 2017-05-11T01:19:13.407

@ValueInk Thanks, bit shorter now. Looks like we need a golfing language just for the API, its calls are sooo long... – matsjoyce – 2017-05-11T07:28:56.130

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. – Value Ink – 2017-05-11T19:58:05.973

True, but it turns into parsing SE's HTML oddities vs long but safe(r) API calls. Its amusing that both fail on the same link though... – matsjoyce – 2017-05-12T08:55:11.693

1

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

Value Ink

Posted 2017-05-10T10:39:55.847

Reputation: 10 608