-3
\$\begingroup\$

Your challenge: Connect to the Online Encyclopedia of Integer Sequences OEIS.org, access any random valid sequence, output it's ID, and ouput all of the members of the sequence provided by the OEIS entry. The only website you're allowed to connect to is OEIS.org.

Hints: Sequences are found in URLs such as http://oeis.org/A280629, however a more easily parseable version is found at URLs such as http://oeis.org/search?q=id:A280629&fmt=text. Every present and future sequence matches the regex "[A-Z]\d{6}". (The regex is there to describe what an ID looks like. There are a lot of invalid sequences that match it, too). The webcam (http://oeis.org/webcam) feature of OEIS generates random sequences, but you'll have to find a way to change the dropdown menu from 'best sequences' to 'all sequences' to use it.

Example output:

A280629
1, 1, 7, 139, 6913, 508921, 57888967, 9313574419, 1984690709953, 547467006437041, 188946742298214727, 79783392959511537499, 40498043815904027702593, 24314800861291379306213161, 17047720745682515427867108487, 13802952030641885344209574247779, 12780883488499783875309105315925633, 13420910251496135926622603184056054881, 15863354775169518855398667975850797997447, 20966527201075972453953302254528386060431659

Every existing sequence has to have a chance of being outputed, it does not have to be an equal chance.

Your program should continue to function properly regardless of future sequences being added. Even though all IDs presently start with A, there will eventually be a sequence B000001 after all the A sequences run out. You can assume that there will be no more sequences after Z999999 is defined, but you do have to assume that it will eventually be defined.

An invalid sequence, which your program should never output, is one that doesn't exist. You may treat sequences with no given members (such as A280611 either as invalid or give just the sequence ID and nothing else or a falsy value, but you must be consistent.

This is , fewest bytes wins!

\$\endgroup\$
6
  • \$\begingroup\$ Your regex isn't quite right - sequences can't end with 000000. Additionally, is outputting a valid sequence that doesn't exist yet acceptable? \$\endgroup\$
    – user45941
    Commented Jan 8, 2017 at 8:36
  • 1
    \$\begingroup\$ What about "sequences" like this one. How shall we handle those? Do we have to check if the entry contains a sequence, and if not find another entry that does? This makes it a lot harder/cumbersome. \$\endgroup\$ Commented Jan 8, 2017 at 8:39
  • \$\begingroup\$ When the sequences names have leading 0, can we omit those 0? (for instance, A15 instead of A000015) \$\endgroup\$
    – Dada
    Commented Jan 8, 2017 at 9:54
  • \$\begingroup\$ Arg, this is impossible in JS \$\endgroup\$ Commented Jan 8, 2017 at 22:40
  • \$\begingroup\$ @StewieGriffin That one now has member, so one can't fall to it when fail. OP should say if this fine though \$\endgroup\$
    – l4m2
    Commented Dec 15, 2022 at 17:06

3 Answers 3

4
\$\begingroup\$

Perl, 93 bytes

$u=chr(65+rand 26).sprintf("%06d",rand 1e6),$_=`curl oeis.org/$u`while!/tt>(.*?)</;say"$u
$1"

Run it with -E flag (and I suggest redirecting STDERR, as curl prints its progression on it):

perl -E '$u=chr(65+rand 26).sprintf("%06d",rand 1e6),$_=`curl oeis.org/$u`while!/tt>(.*?)</;say"$u
$1"' 2> /dev/null

Explanations:
-$u=chr(65+rand 26).sprintf("%06d",rand 1e6) generates a random name,
-$_=curl oeis.org/$u tries to retrieve the sequence,
-/tt>(.*?)</ checks if a tag <tt> is present on the page (it's only present on valid pages). If so, it stores the sequence's elements in $1. And if not, the while keeps looping.
-say"$u\n$1" outputs the sequence's name and its elements.

\$\endgroup\$
7
  • \$\begingroup\$ Do you know of an online interpreter where this works? \$\endgroup\$
    – Pavel
    Commented Jan 8, 2017 at 10:27
  • \$\begingroup\$ The easiest is to run it in a unix terminal... It does potentially a lot of http request, so I don't think there is an online interpreter capable of running it.. \$\endgroup\$
    – Dada
    Commented Jan 8, 2017 at 10:32
  • \$\begingroup\$ This actually fails for oeis.org/A280611 ... \$\endgroup\$ Commented Jan 8, 2017 at 20:41
  • \$\begingroup\$ @TrojanByAccident I don't think it does: replace $u=chr(65+rand 26).sprintf("%06d",rand 1e6) by $u=A280611 and you'll have the right output... \$\endgroup\$
    – Dada
    Commented Jan 8, 2017 at 20:45
  • \$\begingroup\$ @Dada well, I'm no expert on Perl, and I can't run it, as I'm on Windows, but that page contains <tt> tags, so... \$\endgroup\$ Commented Jan 8, 2017 at 20:57
2
\$\begingroup\$

Python 3, 221,208 bytes

from requests import*
from random import*
import re
r=randint
g=lambda x:re.findall(r'(?<=tt>)[^<]*',get('https://oeis.org/'+x).text)
a=''
while not g(a):a=chr(r(65,90))+str(r(0,999999))
print(a+'\n'+g(a)[0])

I'm sure there's a few tricks I missed (and requests is an external lib), but this was a fun first go at python code golf

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice answer! Could b be inlined? It only seems to be used in one place \$\endgroup\$
    – rydwolf
    Commented Dec 13, 2022 at 17:28
  • \$\begingroup\$ Good catch! Was a holdover from when I had structured it slightly differently \$\endgroup\$
    – brubsby
    Commented Dec 13, 2022 at 17:32
1
\$\begingroup\$

JavaScript, 104 bytes

Because someone commented that this isn't possible in JS!

Must be run from the root directory of OEIS (consensus). Returns a 2-element array, with the first element being the sequence number and the second the sequence itself.

f=async n=>(s=/tt>[^<]+/.exec(await(await fetch(n+=Math.random()*1e6|0)).text()))?[n,s[0].slice(3)]:f`A`

To test it, go to the OEIS homepage (or any sequence's page), open your browser's console and copy & paste the code below:

f=async n=>(s=/tt>[^<]+/.exec(await(await fetch(n+=Math.random()*1e6|0)).text()))?[n,s[0].slice(3)]:f`A`
await f()

97 bytes

This should work and could maybe be golfed a little further but I'm on my phone so can't test it properly.

f=async n=>(s=/(?<=tt>)[^<]+/.exec(await(await fetch(n+=Math.random()*1e6|0)).text()))?[n,s]:f`A`
\$\endgroup\$
3
  • \$\begingroup\$ Count the URL you need to go, to avoid encoding program in URL \$\endgroup\$
    – l4m2
    Commented Dec 15, 2022 at 17:11
  • \$\begingroup\$ @l4m2, see the consensus I very deliberately linked to in order to avoid comments like yours. See also the consensus linked from that one. \$\endgroup\$
    – Shaggy
    Commented Dec 15, 2022 at 17:16
  • \$\begingroup\$ The link isn't answered \$\endgroup\$
    – l4m2
    Commented Dec 16, 2022 at 1:49

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.