Output a random OEIS sequence

-3

3

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!

Pavel

Posted 2017-01-08T08:07:38.163

Reputation: 8 585

Your regex isn't quite right - sequences can't end with 000000. Additionally, is outputting a valid sequence that doesn't exist yet acceptable? – Mego – 2017-01-08T08:36:51.787

1

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.

– Stewie Griffin – 2017-01-08T08:39:49.453

When the sequences names have leading 0, can we omit those 0? (for instance, A15 instead of A000015) – Dada – 2017-01-08T09:54:34.670

Arg, this is impossible in JS – TrojanByAccident – 2017-01-08T22:40:28.113

Answers

4

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.

Dada

Posted 2017-01-08T08:07:38.163

Reputation: 8 279

Do you know of an online interpreter where this works? – Pavel – 2017-01-08T10:27:10.770

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.. – Dada – 2017-01-08T10:32:02.693

This actually fails for http://oeis.org/A280611 ...

– TrojanByAccident – 2017-01-08T20:41:28.697

@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... – Dada – 2017-01-08T20:45:14.323

@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... – TrojanByAccident – 2017-01-08T20:57:59.620

@TrojanByAccident The challenge says You may treat sequences with no given members (such as A280611 either as invalid or give just the sequence ID and nothing else. My code will output only the sequence name in that case, which follows the rules... – Dada – 2017-01-08T21:01:10.617

@Dada ah, sorry, as I said, I don't know much about Perl, I was just going by your explanation – TrojanByAccident – 2017-01-08T21:04:28.020