8
\$\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\$
  • 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 '17 at 1:57
  • \$\begingroup\$ I'm surprised the answers aren't using URL shorteners: v.ht/b6QI \$\endgroup\$ – Engineer Toast Jun 19 '17 at 13:05
  • \$\begingroup\$ @EngineerToast They're not usually allowed. \$\endgroup\$ – Esolanging Fruit Sep 20 '17 at 18:24
  • \$\begingroup\$ @Challenger5 Indeed. I should probably re-read that list... \$\endgroup\$ – Engineer Toast Sep 20 '17 at 19:09
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\$
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\$
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\$
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
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Martin Ender Jun 18 '17 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\$
  • \$\begingroup\$ Can you edit your answer and mention that it is Python 3? Thanks! :-) \$\endgroup\$ – Mauro Baraldi Jun 18 '17 at 20:28
  • 2
    \$\begingroup\$ @MauroBaraldi Edited. Sorry about that. \$\endgroup\$ – Dair Jun 18 '17 at 20:29
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\$
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\$
  • \$\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 '17 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 '17 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 '17 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 '17 at 17:11

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.