12
\$\begingroup\$

This dear StackExchange site has so many challenges, and so many good answers. But what about the challenges that were never answered?

Task

Write a program or a function that will print a pseudo-random open unanswered (as in, a question with exactly zero answers) challenge from PPCG. All possible challenges should be produced with the same probability.

Input

  • No input will be taken.

Output

  • Must only be the title, tags and the link which should be separated by newlines.
    • The title must be exactly like it is in the challenge.
    • The tags don't have a strict output format but must include all tags.
    • The link may or may not include the question-name after the question id and must lead to the challenge.
  • May or may not be translated to a human readable format.
    • & to &
  • Leading and trailing whitespace is allowed.

Examples

Encode a steganographic encoder into a package
code-challenge,unicode,steganography
https://codegolf.stackexchange.com/questions/65159/encode-a-steganographic-encoder-into-a-package

Simple predictive keyboard CG
code-golf
https://codegolf.stackexchange.com/questions/76196/simple-predictive-keyboard-cg

Scoring

As this is , shortest answer in bytes wins.

\$\endgroup\$
11
  • 8
    \$\begingroup\$ I'm getting really sick of stack-exchange-api challenges. \$\endgroup\$
    – Riker
    May 11, 2017 at 21:27
  • 3
    \$\begingroup\$ @Riker Uhm... thanks for your feedback? I really don't know why you would downvote for that reason but more power to you, I guess. \$\endgroup\$ May 11, 2017 at 21:28
  • 6
    \$\begingroup\$ I didn't downvote for that. I downvoted because I think the question is boring. Also, would closed questions need to have the equal probability of being outputted? \$\endgroup\$
    – Riker
    May 11, 2017 at 21:29
  • 1
    \$\begingroup\$ While I don't feel as strongly about it as @Riker, there does seem to have be a lot of API-related challenges lately; so much so that I managed to get myself blocked from using the API for ~21 hours! \$\endgroup\$
    – Shaggy
    May 11, 2017 at 22:09
  • 4
    \$\begingroup\$ Interestingly enough, this challenge will show up during testing, but once I answer it, it won't show up in testing anymore. \$\endgroup\$
    – hyper-neutrino
    May 11, 2017 at 22:45

3 Answers 3

8
\$\begingroup\$

JavaScript + HTML, 271 250 232 bytes

Apparantly you can use Dates as pseudo-random numbers. I stole this from Shaggy's answer.

(Only uses about 4 of your quota)

q=[]
g=f=>fetch('//api.stackexchange.com/questions/unanswered?site=codegolf&page='+f).then(r=>r.json().then(j=>(q=[...q,...j.items])^j.has_more?g(f+1):document.write(`<pre>${(q=q[new Date%q.length]).title}
${q.tags}
`+q.link)))
g(1)

It makes an array q, then calls g(1), which fetches the first page of results and adds it to q. Then, if the request says it has_more, then it calls g(f+1), which fetches the next page, until it reaches the end and writes out to the HTML document (Which will automatically unescape the response)

If we don't care about all of the unanswered questions, just the most recent 30 (Only uses 1 of your quota):

JavaScript + HTML, 213 196 179 bytes

fetch`//api.stackexchange.com/questions/unanswered?site=codegolf`.then(r=>r.json().then(j=>document.write(`<pre>${(j=j.items[new Date%j.items.length]).title}
${j.tags}
`+j.link)))

\$\endgroup\$
8
  • \$\begingroup\$ Sorry, @Artyer, I was so focused on working on my own answer, I never spotted that you'd beaten me to the punch by a few minutes. I'm happy to delete my answer, if you feel it's to similar to yours. \$\endgroup\$
    – Shaggy
    May 12, 2017 at 9:17
  • \$\begingroup\$ @Shaggy It's alright. I also wrote the second answer at the same time as you wrote your answer, so it's a happy coincidence that they ended up similar. \$\endgroup\$
    – Artyer
    May 12, 2017 at 9:18
  • \$\begingroup\$ You could save a few byes by hardcoding in the number of pages that need to be fetched. Instead of j.has_more?g(f+1), try ++f<5?g(f) \$\endgroup\$
    – Shaggy
    May 12, 2017 at 10:38
  • \$\begingroup\$ You could also replace q=q.concat(j.items) with q=[...q,...j.items]. It looks like Tags: is optional, so you can drop that too. And no need to join q.tags, it will be coerced into a string when being concatenated to a string. Finally, using a template literal for the final should save you a few bytes, too. \$\endgroup\$
    – Shaggy
    May 12, 2017 at 10:45
  • \$\begingroup\$ @Shaggy Unfortunately, I've met my quota. It resets in 425 seconds though. \$\endgroup\$
    – Artyer
    May 12, 2017 at 10:53
6
\$\begingroup\$

Python + requests + json + random + html, 249 239 bytes

import requests as r,json,random as R,html
j=R.choice(json.loads(r.get('http://api.stackexchange.com/questions/no-answers?site=codegolf').text)['items'])
print('\n'.join([html.unescape(j['title']),'Tags: '+', '.join(j['tags']),j['link']]))

Turned out longer than I'd like.

-10 bytes thanks to @totallyhuman by using R.choice rather than R.shuffle and taking the first element.

\$\endgroup\$
5
  • \$\begingroup\$ Can you save a few bytes by using random.shuffle? \$\endgroup\$ May 12, 2017 at 1:00
  • \$\begingroup\$ ...Wait, or even better, random.choice()? \$\endgroup\$ May 12, 2017 at 1:25
  • 1
    \$\begingroup\$ @totallyhuman Changing the inpurt name happens to actually make no difference in bytecount :P But thanks for .choice()! \$\endgroup\$
    – hyper-neutrino
    May 12, 2017 at 1:33
  • \$\begingroup\$ You don't need the json library, use r.get(url).json() instead \$\endgroup\$
    – ovs
    May 12, 2017 at 5:40
  • \$\begingroup\$ This doesn't account for has_more. It only randomly chooses from the first 30. \$\endgroup\$
    – Artyer
    May 12, 2017 at 8:40
0
\$\begingroup\$

Bash 255 232 bytes

a="api.stackexchange.com/questions/";b="?site=codegolf";c=$(w3m $a"unanswered"$b"&filter=total"|tr -cd 0-9);w3m $a"no-answers"$b"&pagesize=1&page="$((RANDOM%c))|jq -r ".items[0]|.title,(.tags|join(\", \")),.link"|recode html..utf-8

Looks like a wrong results returns for totals for no-answers/unanswered. General idea - get total, than get random page with one item.

Bash 174 153 bytes

w3m api.stackexchange.com/questions/no-answers?site=codegolf|jq -r ".items[$RANDOM%(.items|length)]|.title,(.tags|join(\", \")),.link"|recode html..utf-8

It select one random question from last 30, not from all questions. Works from command line. Required curl w3m, jq and recode.

result:

Tips for golfing in Charcoal
code-golf, tips
https://codegolf.stackexchange.com/questions/117269/tips-for-golfing-in-charcoal
\$\endgroup\$
1
  • \$\begingroup\$ I'm pretty sure you need to choose from all of them. But welcome to PPCG anyway! \$\endgroup\$ May 12, 2017 at 17:09

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.