10 Clickbaits you can't resist

8

0

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.

Moartem

Posted 2017-06-17T16:04:15.940

Reputation: 343

1Re: 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. – None – 2017-06-18T01:57:17.757

I'm surprised the answers aren't using URL shorteners: v.ht/b6QI

– Engineer Toast – 2017-06-19T13:05:23.980

@EngineerToast They're not usually allowed. – Esolanging Fruit – 2017-09-20T18:24:28.513

@Challenger5 Indeed. I should probably re-read that list...

– Engineer Toast – 2017-09-20T19:09:25.493

Answers

1

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

colsw

Posted 2017-06-17T16:04:15.940

Reputation: 3 195

4

Python + requests, 128 bytes

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

ovs

Posted 2017-06-17T16:04:15.940

Reputation: 21 408

3

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.

LegionMammal978

Posted 2017-06-17T16:04:15.940

Reputation: 15 731

3

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.

Dair

Posted 2017-06-17T16:04:15.940

Reputation: 141

Can you edit your answer and mention that it is Python 3? Thanks! :-) – Mauro Baraldi – 2017-06-18T20:28:39.023

2@MauroBaraldi Edited. Sorry about that. – Dair – 2017-06-18T20:29:57.337

3

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

Mauro Baraldi

Posted 2017-06-17T16:04:15.940

Reputation: 189

1Welcome to PPCG! – Martin Ender – 2017-06-18T21:30:03.990

2

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!

Okx

Posted 2017-06-17T16:04:15.940

Reputation: 15 025

1

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>

Shaggy

Posted 2017-06-17T16:04:15.940

Reputation: 24 623

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) – Moartem – 2017-06-19T09:34:49.330

@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. – Shaggy – 2017-06-19T10:37:21.607

I will fix it to 1-indexing, so all have the same prequesites, please update your answer. Anyway thanks for pointing this out. – Moartem – 2017-06-19T14:32:01.970

@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. – Shaggy – 2017-06-19T17:11:13.047