0
\$\begingroup\$

Challenge

Given a positive integer, determine whether that number is currently being used to represent a question on PPCG.

The question must not have been removed, but may be open or closed.

The number is the one being used in the URL of the question, such as:

https://codegolf.stackexchange.com/questions/[input]

Note that this questions is tagged , but you may not need this to complete the challenge.

Examples

1: Truthy (represents Just Another Perl Hacker)

3: Falsey (was once Solve the Tower of Hanoi, but was removed by a moderator.

127030: Falsey (was once Helping Danny to Maximize his Score, but was removed by the author.)

127147: Truthy (Fastest algorithm to take the product of all subsets)

Scoring

This is , so the shortest answer in each language wins.

\$\endgroup\$
5
  • \$\begingroup\$ Just Another Perl Hacker is closed. \$\endgroup\$
    – ATaco
    Commented Jun 20, 2017 at 3:48
  • \$\begingroup\$ @ATaco It is closed, but the criteria state that The question must not have been removed, but may be open or closed. This is to benefit answers that don't use the Stack Exchange API because, I believe, a page will give 404 Not Found iff it is not a question or was removed \$\endgroup\$ Commented Jun 20, 2017 at 3:57
  • 1
    \$\begingroup\$ @totallyhuman Yes, standard I/O apply \$\endgroup\$ Commented Jun 20, 2017 at 3:58
  • \$\begingroup\$ Sandbox for those who can see deleted posts \$\endgroup\$ Commented Jun 20, 2017 at 3:59
  • 6
    \$\begingroup\$ Uhm, why is the title "Is it an open question?" if the question may be open or closed? \$\endgroup\$
    – xnor
    Commented Jun 20, 2017 at 5:56

6 Answers 6

3
\$\begingroup\$

bash, 52 bytes

curl -IL codegolf.stackexchange.com/q/$1|grep 2\ 200

Takes input as a command line argument, and outputs in the form of exit code: 0 or "success" (truthy) for an undeleted question, and 1 or "failure" (falsy) for a nonexistent or deleted question.

It also prints a bunch of junk to STDERR and potentially STDOUT, but this is allowed to be ignored as per meta.

As an explanation, the -I flag to curl sends an HTTP HEAD request (fetch only the headers), and -L will follow redirects (http to https, and then /q to /questions). The full output from the curl command will look something like this:

HTTP/1.1 301 Moved Permanently                             
Location: https://codegolf.stackexchange.com/q/127030
X-Request-Guid: 1a0b6059-057c-448c-bfc9-a96f1b373abf
Content-Length: 160
Accept-Ranges: bytes
Date: Tue, 20 Jun 2017 04:07:30 GMT
Via: 1.1 varnish
Age: 503
Connection: keep-alive
X-Served-By: cache-itm7423-ITM
X-Cache: HIT
X-Cache-Hits: 1
X-Timer: S1497931651.709631,VS0,VE0
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=933ae4b0-81b0-2b37-e26e-59a0fbb6991a; domain=.stackexchange.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly

HTTP/2 302 
date: Tue, 20 Jun 2017 04:07:31 GMT
content-type: text/html; charset=utf-8
location: https://codegolf.stackexchange.com/questions/127030/helping-danny-to-maximize-his-score
x-frame-options: SAMEORIGIN
x-request-guid: fdcaf556-7ae6-4240-89a1-ace7625f8821
strict-transport-security: max-age=15552000
accept-ranges: bytes
via: 1.1 varnish
age: 0
x-served-by: cache-nrt6132-NRT
x-cache: MISS
x-cache-hits: 0
x-timer: S1497931651.085546,VS0,VE166
vary: Fastly-SSL
x-dns-prefetch-control: off
set-cookie: prov=5e314a4f-0c50-08ef-afa2-9388b37e3b7a; domain=.stackexchange.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
cache-control: private
content-length: 204

HTTP/2 404 
date: Tue, 20 Jun 2017 04:07:31 GMT
content-type: text/html; charset=utf-8
x-frame-options: SAMEORIGIN
x-request-guid: 3f72afb7-81e8-4e69-a8ad-474534f2abe6
strict-transport-security: max-age=15552000
accept-ranges: bytes
via: 1.1 varnish
age: 0
x-served-by: cache-nrt6132-NRT
x-cache: MISS
x-cache-hits: 0
x-timer: S1497931651.323339,VS0,VE189
vary: Fastly-SSL
x-dns-prefetch-control: off
set-cookie: prov=cede4119-eba5-d799-e376-848e4e457f07; domain=.stackexchange.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
cache-control: private
content-length: 42158

Note that the 2\ 200 (equivalent to '2 200') will match the line HTTP/2 200 if it exists (HTTP OK), and it will not match HTTP/2 404 (Not Found) for questions that do not exist. (The inclusion of the 2 is necessary to avoid false positives if some of the other headers happen to contain the sequence 200.)

\$\endgroup\$
1
\$\begingroup\$

Python 2 + Requests, 112 bytes

Takes input as a string or integer and outputs 1 for truthy and 0 for falsy.

lambda n:len(get('http://api.stackexchange.com/posts/%s?site=codegolf'%n).json()['items'])
from requests import*
\$\endgroup\$
2
  • \$\begingroup\$ Won't this return truthy if the integer provided is the ID of an answer? \$\endgroup\$
    – Shaggy
    Commented Jun 20, 2017 at 10:00
  • \$\begingroup\$ @Shaggy All of these answers do. This is an oversight on my part. \$\endgroup\$ Commented Jun 20, 2017 at 13:37
1
\$\begingroup\$

Mathematica, 70 bytes

Takes input as a string and outputs 1 for truthy and 0 for falsy.

Check[Import["https://codegolf.stackexchange.com/questions/"<>#];1,0]&
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 104 102 bytes

Returns a Promise containing a JSON object for truthy or undefined for falsey.

n=>fetch(`//api.stackexchange.com/questions/${n}?site=codegolf`).then(r=>r.json()).then(j=>j.items[0])

Try it

f=
n=>fetch(`//api.stackexchange.com/questions/${n}?site=codegolf`).then(r=>r.json()).then(j=>j.items[0])
oninput=_=>f(+i.value).then(console.log);f(i.value=1).then(console.log)
<input id=i type=number>

\$\endgroup\$
1
\$\begingroup\$

Python 3 + Requests, 107 99 98 92 79 bytes

lambda m:get("http://codegolf.stackexchange.com/q/"+m).ok
from requests import*

-8 bytes by changing question to q
-1 byte by changing != to <, since 200 OK is less than 404 NOT FOUND.
-1 byte by changing https to http
-6 bytes by changing to a lambda
-13 bytes using the ok attribute

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You should add + Requests in the header as it's not in the Python standard library. \$\endgroup\$ Commented Jun 20, 2017 at 12:58
0
\$\begingroup\$

Stratos, 16 bytes

f"¹⁵s/%²"r"⁷s"
l

Explanation (decompressed:

f"api.stackexchange.com/questions/%?site=codegolf"         Replace % with the input, and get from this URL
                                                  r"items" Get the JSON array named "items"
l                                                          Length

Prints 1 for truthy, and 0 for falsy.

Try it!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.