6 kyu
van Eck sequences
34 of 36JohanWiltink
Description:
What's the next term? Don't know
How fast does it grow? Don't know
Does every number appear? Don't know
Consider a sequence that is generated as follows:
- the starting term(s) is or are given
- if a term did not appear previously in the sequence, the next term in the sequence is
0
- if a term did appear previously in the sequence, the next term in the sequence is the
number of steps back
in the sequence that it appeared
Given 0
, the sequence starts: 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, ..
- first term is
0
; sequence is now[0]
0
did not appear previously: add0
; sequence is now[0,0]
0
last appeared previously1
place before current term: add1
; sequence is now[0,0,1]
1
did not appear previously: add0
; sequence is now[0,0,1,0]
0
last appeared previously2
places before current term: add2
; sequence is now[0,0,1,0,2]
2
did not appear previously: add0
; sequence is now[0,0,1,0,2,0]
0
last appeared previously2
places before current term: add2
; sequence is now[0,0,1,0,2,0,2]
2
last appeared previously2
places before current term: add2
; sequence is now[0,0,1,0,2,0,2,2]
2
last appeared previously1
place before current term: add2
; sequence is now[0,0,1,0,2,0,2,2,1]
1
last appeared previously6
places before current term: add6
; sequence is now[0,0,1,0,2,0,2,2,1,6]
6
did not appear previously .. and so on, and so on
Task
Implement
class VanEck {}
initialised with the starting term(s) ( at least 1
)
constructor(a) { return instance; }
exposing the following methods
term(i) { return n; } // term at index `i` ( `0`-based )
sequence(i) { return [n,n,..]; } // the complete sequence of terms `0..i` ( exclusive )
first(n) { return i; } // the index of the first occurrence of the number `n`
every(n) { return i; } // the lowest index where all numbers `0..n` ( inclusive ) have occurred
fast(i) { return n; } // the maximum of terms `0..i` ( inclusive )
Notes
- All input is valid
- methods will be tested up to argument
1 000
- in
fixed tests
only,term
will additionally be tested up to argument1 000 000
Resources
Algorithms
Similar Kata:
Stats:
Created | Jun 18, 2019 |
Published | Jun 18, 2019 |
Warriors Trained | 264 |
Total Skips | 22 |
Total Code Submissions | 253 |
Total Times Completed | 36 |
JavaScript Completions | 34 |
Scala Completions | 3 |
Total Stars | 15 |
% of votes with a positive feedback rating | 95% of 19 |
Total "Very Satisfied" Votes | 17 |
Total "Somewhat Satisfied" Votes | 2 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 3 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |