10
\$\begingroup\$

Write a program/function/script that takes a natural number i from 1 to 10 as input and outputs the URL of the Question with the i th hottest question on the Code Golf Portal

The ordering should be like in https://codegolf.stackexchange.com/?tab=hot. For example:
input 1
output https://codegolf.stackexchange.com/questions/127047/the-spain-license-plates-game
(at the point of this edit)

There are no strong restrictions to the output, as long as the url can be reasonably retrieved. Valid outputs include strings, console printing etc.

It's Code Golf, so shortest code in Bytes wins.

Notes

If your program automatically opens a browser with the webpage, it counts as valid output and is totally cool.

If your program does not retrieve the ranking from current information (i.e. hardcoding the links), it is not valid.

The contest ends with the end of June.

As questioned: The indexing must be 1 based, ie: the input 1 must return the the first site of the hottest questions.

Comment

I hope this question is not too clickbaity, as the stack exchange editor program was mocking me for the title. Originally I planned this challenge with Youtube trends, where it would haven been more fitting.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Re: Stack Exchange mocking you for the question title, it's just triggering on the word you specifically. The assumption made by the software is that if you're asking people what they specifically think about something, then the question is probably going to be closed as primarily opinion-based (as if the question were factually-based, it wouldn't matter who you asked). Of course, this simple heuristic has a lot of false positives. \$\endgroup\$
    – user62131
    Jun 18, 2017 at 1:57
  • \$\begingroup\$ I'm surprised the answers aren't using URL shorteners: v.ht/b6QI \$\endgroup\$ Jun 19, 2017 at 13:05
  • \$\begingroup\$ @EngineerToast They're not usually allowed. \$\endgroup\$ Sep 20, 2017 at 18:24
  • \$\begingroup\$ @Challenger5 Indeed. I should probably re-read that list... \$\endgroup\$ Sep 20, 2017 at 19:09
  • \$\begingroup\$ Can we output a JSON object containing the URL under the link key or must we output just the URL? \$\endgroup\$
    – Makonede
    Jan 31 at 1:20

8 Answers 8

4
\$\begingroup\$

Python + requests, 128 bytes

from requests import*
lambda n:get('http://api.stackexchange.com/questions?sort=hot&site=codegolf').json()['items'][n-1]['link']
\$\endgroup\$
0
3
\$\begingroup\$

Mathematica, 125 bytes

b="http://codegolf.stackexchange.com";b<>"/q/"<>StringCases[Import[b<>"?tab=hot","Text"],"ns/"~~a:DigitCharacter..:>a][[3#]]&

Anonymous function. Takes no input and returns a string as output. Was going to use the XML feed, but it seems to follow a different ordering than on the page.

\$\endgroup\$
0
3
\$\begingroup\$

Python 2.7, 195 bytes

from urllib import*
import zlib, json
lambda n:json.loads(zlib.decompress(urlopen('http://api.stackexchange.com/questions?sort=hot&site=codegolf').read(),16+zlib.MAX_WBITS))['items'][n-1]['link']

This is same answer by Dair, but using Python 2

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jun 18, 2017 at 21:30
3
\$\begingroup\$

Python 3, 221 bytes

from urllib.request import*
import zlib, json
lambda n:json.loads(zlib.decompress(urlopen('http://api.stackexchange.com/questions?sort=hot&site=codegolf').read(),16+zlib.MAX_WBITS),encoding='utf-8')['items'][n-1]['link']

Based off of ovs answer. Also, thanks to Oluwafemi Sule for helping me with an issue I had.

\$\endgroup\$
3
  • \$\begingroup\$ Can you edit your answer and mention that it is Python 3? Thanks! :-) \$\endgroup\$ Jun 18, 2017 at 20:28
  • 2
    \$\begingroup\$ @MauroBaraldi Edited. Sorry about that. \$\endgroup\$
    – Dair
    Jun 18, 2017 at 20:29
  • \$\begingroup\$ 16+zlib.MAX_WBITS can be 31, the space between zlib and json in import zlib, json can be removed, and Python 3.9 lets you get rid of ,encoding='utf-8'. \$\endgroup\$
    – Makonede
    Jan 24 at 23:59
2
\$\begingroup\$

Stratos, 28 bytes

"-1"+
f"¹⁵s²&sort=hot"r"⁷s"@

Explanation:

"-1"+                    Decrement the input, and store it.
f"¹⁵s²&sort=hot"         Fetch the contents of the URL api.stackexchange.com/questions?site=codegolf&sort=hot.
                r"⁷s"    Get the array named "items"
                     @   Get the nth element, where n is retrieved from storage.

Try it!

\$\endgroup\$
7
  • \$\begingroup\$ I'm really questioning the validity of this, considering you made this language less than a month before posting this, and, coincidentally, there are one-byte builtins for api.stackexchange.com/, question, ?site=codegolf, and item. Oh yeah, and a builtin for parsing JSON and getting an array from it in 3+ bytes. \$\endgroup\$
    – Makonede
    Jan 24 at 23:46
  • \$\begingroup\$ More to the point, this returns a JSON object, not a URL like OP asked. Therefore, this is invalid. \$\endgroup\$
    – Makonede
    Jan 24 at 23:55
  • \$\begingroup\$ Try re-reading the challenge, it is valid. This language was specifically designed to be able to look up the stackexchange API and this challenge just happened to be posted \$\endgroup\$
    – Okx
    Jan 27 at 4:59
  • \$\begingroup\$ Try re-reading my second comment, it is invalid. \$\endgroup\$
    – Makonede
    Jan 27 at 19:03
  • \$\begingroup\$ Quoting the challenge: "There are no strong restrictions to the output, as long as the url can be reasonably retrieved. Valid outputs include strings, console printing etc." @Makonede \$\endgroup\$
    – Okx
    Jan 28 at 22:45
1
\$\begingroup\$

PowerShell v5, 83 Bytes

(irm api.stackexchange.com/questions?sort=hot`&site=codegolf).Items["$args"-1].Link

irm is short for Invoke-RestMethod which auto-parses the Json, makes life quite easy.

add four bytes (saps for start-process) for version which opens in browser.

saps(irm api.stackexchange.com/questions?sort=hot`&site=codegolf).Items["$args"-1].Link
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 106 bytes

Returns a Promise containing the JSON object for the desired question, which includes the link.

n=>fetch`//api.stackexchange.com/questions?sort=hot&site=codegolf`.then(r=>r.json()).then(j=>j.items[--n])
  • Sacrificed 2 bytes allowing for 1-indexing.

Try it

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

\$\endgroup\$
4
  • \$\begingroup\$ hm Im not sure at the moment how to handle 0 based indexing, but as the other answers appear to use 1 based indexing and it was specified that way, your answer is counted as 106 bytes (still current top score) \$\endgroup\$
    – Moartem
    Jun 19, 2017 at 9:34
  • \$\begingroup\$ @Moartem: "Im not sure at the moment how to handle 0 based indexing" - Let me know when you've figured it out and I'll update my answer accordingly. I would suggest allowing for either 1-indexing or 0-indexing. \$\endgroup\$
    – Shaggy
    Jun 19, 2017 at 10:37
  • \$\begingroup\$ I will fix it to 1-indexing, so all have the same prequesites, please update your answer. Anyway thanks for pointing this out. \$\endgroup\$
    – Moartem
    Jun 19, 2017 at 14:32
  • \$\begingroup\$ @Moartem: Answer updated. For future reference, unless there's good reason not too, the norm is to allow solutions to use their language's default indexing. \$\endgroup\$
    – Shaggy
    Jun 19, 2017 at 17:11
1
\$\begingroup\$

05AB1E + json, 85 bytes

<’¸¸.‚‹º.ŒŒ/…é?€¼=ƒËŠˆ&Šœ=…ß’.w„\"D„\\s‚‡’(EnÆá.€› (JSON.deƒË!"ÿ")["‚Õ"],ÿ)["ƒÚ"]’.E
\$\endgroup\$

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.