How to Fix Van Eck’s Series in Python

0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, ...

This is the Van Eck’s Series.

Let’s go through it step by action.

Term 1: The very first term is 0.
Term 2: Because we have not seen 0 in the past, the 2nd term is 0.
Term 3: Because we had actually seen a 0 prior to, one action back, the 3rd term is 1
Term 4: Because we have not seen a 1 prior to, the 4th term is 0
Term 5: Because we had actually seen a 0 prior to, 2 actions back, the 5th term is 2.
And so on&& mldr;

Your job is to discover the n_th number in Van Eck’s Series. (1-based)

The Option in Python

Choice 1

 from collections import Counter

c= Counter().
SEQ =[0]
for i in variety( 1000 ):.
n = SEQ[-1]
if not c[n]: c[n]= i.
SEQ.append( i-c[n]).
c[n]= i.

seq= SEQ. __ getitem __.

Choice 2

 def dist( arr):.
for i in variety (1, len( arr)):.
if arr[-1-i] == arr[-1]:.
return i.
return 0.

def seq( n):.
s =[0, 0]
for _ in variety (n):.
s.append( dist( s)).
return s[n-1]
 def seq( n):.
van, eck = [0], 0.
while n:= n - 1:.
van.insert( 0, eck:= van.index( eck, 1) if eck in van[1:] else 0).
return eck.

Test cases to verify the service

 from service import seq.
import test.

from random import randint.

@test. explain(" Sample tests:").
def tests():.
@test. it(" Little numbers").
def _():.
s =[0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6]
for i in variety (len( s)):.
test.assert _ equates to( seq( i +1), s[i]).
@test. it(' Larger numbers').
def __():.
s =[3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11,
             18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3,
             6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0,
             5, 37, 0, 3, 8, 8, 1, 46, 0, 6, 23, 0]
for i in variety (len( s)):.
test.assert _ equates to( seq( i +50), s[i]).

@test. explain(' Random checks:'-RRB-.
def r():.

def dist( arr):.
for i in variety (1, len( arr)):.
if arr[-1-i] == arr[-1]:.
return i.
return 0.

def ref_sol( n):.
s =[0, 0]
for _ in variety (n):.
s.append( dist( s)).
return s[n-1]

@test. it(' 200 random tests:'-RRB-.
def _():.
for _ in variety (200 ):.
a = randint( 100, 1000).
exp = ref_sol( a).
test.assert _ equates to( seq( a), exp).

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: