13
\$\begingroup\$

I am too bored and want a challenge to solve. But I do not see any interesting challenges, and I am too lazy to search for one. Can you (Well, technically your code) suggest me one?


I/O

Input: None.

Output: A link to a random open(i.e. non-closed) challenge("Challenge" excludes !) on PPCG. You may not link to an answer in a challenge, Just a challenge. (I didn't ask for an interesting answer!)


Examples

Valid:

http://codegolf.stackexchange.com/questions/93288/make-me-a-mooncake
http://codegolf.stackexchange.com/questions/113796/raise-a-single-number
http://codegolf.stackexchange.com/questions/113898

Invalid:

http://codegolf.stackexchange.com/questions/78152/tips-for-golfing-in-matl (tips question)
http://codegolf.stackexchange.com/questions/113896/josephus-problem (closed)
http://codegolf.stackexchange.com/questions/84260/add-two-numbers/84365#84365 (linked to answer)
\$\endgroup\$
  • \$\begingroup\$ Can we output http://codegolf.stackexchange.com/questions/93288/ instead of http://codegolf.stackexchange.com/questions/93288/make-me-a-mooncake (note the lack of challenge name). \$\endgroup\$ – Cows quack Mar 26 '17 at 11:53
  • \$\begingroup\$ @Kritixi Lithos Sure, that works. \$\endgroup\$ – lol Mar 26 '17 at 11:54
  • \$\begingroup\$ Not all tips questions have the word "tips" in their name. See this one for example. Is that allowed or did you mean to remove challenges in the "tips" tag? \$\endgroup\$ – seshoumara Mar 26 '17 at 12:11
  • \$\begingroup\$ @seshoumara The latter, that's also the reason I typed 'tips' as a tag. \$\endgroup\$ – lol Mar 26 '17 at 12:12
  • \$\begingroup\$ There are about 6k questions fulfilling your criteria on PPCG. Do all need to have a chance of being picked by random, or can I pick at random from the first 50 results I get from a search link? \$\endgroup\$ – seshoumara Mar 26 '17 at 12:21
4
\$\begingroup\$

JavaScript (ES6), 209 bytes

Does work, but will most likely hit the StackExchange API limit very quickly. It will probably take a while to output the link, as it's checking randomly for a valid question.

_=>fetch(`//api.stackexchange.com/2.2/questions/${new Date%2e4}?site=codegolf`).then(_=>_.json()).then((a,b=a.items)=>{if(!b.length||b[0].tags.includes`tips`||b[0].closed_date)f();else console.log(b[0].link)})

f=_=>fetch(`//api.stackexchange.com/2.2/questions/${new Date%2e4}?site=codegolf`).then(_=>_.json()).then((a,b=a.items)=>{if(!b.length||b[0].tags.includes`tips`||b[0].closed_date)f();else console.log(b[0].link)})

f();

\$\endgroup\$
  • \$\begingroup\$ The OP said that all questions fitting the requirements need to have a chance of being chosen at random. \$\endgroup\$ – seshoumara Mar 27 '17 at 12:31
  • \$\begingroup\$ @seshoumara should be fixed now. \$\endgroup\$ – Tom Mar 27 '17 at 13:06
  • \$\begingroup\$ Save 4 bytes by dropping the API version from the URL. \$\endgroup\$ – Shaggy Apr 25 '17 at 16:18
  • \$\begingroup\$ Got codegolf.stackexchange.com/questions/173/… which is not valid \$\endgroup\$ – sergiol Jul 25 '17 at 22:29
  • \$\begingroup\$ Can confirm that this generates tips challenges too... \$\endgroup\$ – FlipTack Dec 29 '17 at 13:47
2
\$\begingroup\$

Python 3, 452 339 bytes

from requests import*
from random import*
while 1:
    n=randrange(999999)
    r=get("http://api.stackexchange.com/questions/%d?site=codegolf"%n).json()
    if r["items"]:
        m=r["items"][0]
        if "tips" not in m["tags"]:
            try:m["closed_date"]
            except:print("http://codegolf.stackexchange.com/q/%d"%n);break

A bit unsightly and not very golf-y, but I couldn't figure out a better way to do it. Since this more or less brute-forces the API, you'll spend up your quota quite quickly and start getting errors, but after a few hours it'll work again.

EDIT: Saved 113 bytes by removing multiple unnecessary things, thanks to NoOneIsHere.

\$\endgroup\$
  • \$\begingroup\$ You only use l once, so it is unnecessary. \$\endgroup\$ – NoOneIsHere Jul 23 '17 at 4:31
  • \$\begingroup\$ That's correct. I did that so I could print the link afterwards if it was valid, but that printed the API link instead of the normal one. Thanks for reminding me. \$\endgroup\$ – LyricLy Jul 23 '17 at 4:37
  • \$\begingroup\$ Also, you can online the try and maybe the except. \$\endgroup\$ – NoOneIsHere Jul 23 '17 at 4:38
  • \$\begingroup\$ That is also correct. Thanks again. \$\endgroup\$ – LyricLy Jul 23 '17 at 4:43
  • \$\begingroup\$ Glad to help. Pad \$\endgroup\$ – NoOneIsHere Jul 23 '17 at 4:49
1
+50
\$\begingroup\$

SEDE SQL: 79 bytes

Select Id from posts where ClosedDate IS NULL AND Tags !='tips' ORDER BY RAND()

There are 2 catches. One you have to wait for a while before running again or you have a cache hit (as a mod told me) and you only get the question id number.

\$\endgroup\$
  • \$\begingroup\$ I think it is valid because I am not that lazy to not even being able to type \$\endgroup\$ – lol Mar 27 '17 at 22:52
  • \$\begingroup\$ Yes! Your word is lay on your challenge \$\endgroup\$ – Christopher Mar 27 '17 at 23:51

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.