Suggest me a challenge!

14

1

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)

Matthew Roh

Posted 2017-03-26T11:51:06.120

Reputation: 5 043

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). – user41805 – 2017-03-26T11:53:25.080

@Kritixi Lithos Sure, that works. – Matthew Roh – 2017-03-26T11:54:23.647

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?

– seshoumara – 2017-03-26T12:11:44.580

@seshoumara The latter, that's also the reason I typed 'tips' as a tag. – Matthew Roh – 2017-03-26T12:12:41.383

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? – seshoumara – 2017-03-26T12:21:49.020

@seshoumara the former option. – Matthew Roh – 2017-03-26T12:24:03.010

@SIGSEGV I believe there is no way to choose a random question from every open question asked. – Okx – 2017-03-27T09:31:15.457

@Okx Use your brain.. (Hint: every question and answer has an unique id) – Matthew Roh – 2017-03-27T09:32:51.383

Thanks. Also, can we only give [tag:code-golf] questions? The SE API needs either a tagged parameter or an intitle parameter. – Okx – 2017-03-27T09:34:40.050

@Okx Huh? I do not understand what you mean. I believe you can put newlines in code-golf too. – Matthew Roh – 2017-03-27T09:37:27.347

@SIGSEGV I mean questions with the tag [tag:code-golf] – Okx – 2017-03-27T09:38:00.670

@Okx you could generate the post id first, then see if it's valid. That way your problem is solved. – Matthew Roh – 2017-03-27T09:39:35.100

Let us continue this discussion in chat.

– Okx – 2017-03-27T09:40:56.137

I am going to answer with SEDE! – Christopher – 2017-03-27T15:53:39.357

@DownChristopher Sure, Why not! – Matthew Roh – 2017-03-27T15:57:18.760

Actually one problem is it won't be a url but a id for the question. Would that be invalid? (I think so) – Christopher – 2017-03-27T15:58:57.230

Answers

4

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();

Tom

Posted 2017-03-26T11:51:06.120

Reputation: 3 078

The OP said that all questions fitting the requirements need to have a chance of being chosen at random. – seshoumara – 2017-03-27T12:31:34.310

@seshoumara should be fixed now. – Tom – 2017-03-27T13:06:34.100

Save 4 bytes by dropping the API version from the URL. – Shaggy – 2017-04-25T16:18:21.457

Got https://codegolf.stackexchange.com/questions/173/tips-for-code-golfing-in-c which is not valid

– sergiol – 2017-07-25T22:29:42.317

Can confirm that this generates [tag:tips] challenges too... – FlipTack – 2017-12-29T13:47:09.543

2

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.

LyricLy

Posted 2017-03-26T11:51:06.120

Reputation: 3 313

You only use l once, so it is unnecessary. – NoOneIsHere – 2017-07-23T04:31:58.020

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. – LyricLy – 2017-07-23T04:37:17.353

Also, you can online the try and maybe the except. – NoOneIsHere – 2017-07-23T04:38:19.657

That is also correct. Thanks again. – LyricLy – 2017-07-23T04:43:00.697

Glad to help. Pad – NoOneIsHere – 2017-07-23T04:49:12.697

1

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.

Christopher

Posted 2017-03-26T11:51:06.120

Reputation: 3 428

I think it is valid because I am not that lazy to not even being able to type – Matthew Roh – 2017-03-27T22:52:51.363

Yes! Your word is lay on your challenge – Christopher – 2017-03-27T23:51:39.777