New course! Every coder should learn Generative AI!
Try a free lesson0
Sequence of numbers that reads the previous numbers and adds them in a list (I canāt solve this on Python)
The list starts with a 0. If the last number is the first time that number appears on the list, then you add another zero. So now we have [0,0]. If the last number isnāt the first time that number appears, then you count the number of terms youāll have to cover to reach the last time that number showed before that. So, before that second 0, thereās another, and from that second 0, to reach the first one, you go back 1 number. So the list is now [0,0,1]. The list with 6 numbers is [0,0,1,0,2,0]. I need help.
6 Answers
+ 3
Ipang my bad my english was terribleā¦
Basically, if it isnāf the first time that a number appears on the list, you put a number that would tell you how many numbers you would have to go back to find that number
Still bad but hopefully better and sorry about it
+ 2
I'm confused about this paragraph ...
"If the last number isnāt the first time that number appears, then you count the number of terms youāll have to cover to reach the last time that number showed before that."
The "last time that number showed up before that" in particular, lots of "that" there ...
+ 2
JoĆ£o Joana Hi! I give it a try:
https://code.sololearn.com/cSRRFxP8M93b/?ref=app
+ 2
JoĆ£o Joana Hi, again!
I even made an more compact variant you can try:
def countPassedEquals(seq):
l = []
for i in range(len(seq)):
l.append(seq[:i].count(seq[i]))
return tuple(l)
https://code.sololearn.com/c2c2MDEh1Juk/?ref=app
+ 1
Hello JoĆ£o Joana
Maybe not the smartest way:
seq = [0]
n = seq[-1] (n is last element)
now use a for loop to store each index where n occurs
if the length of this index list is 1 -> add 0 (the element occurs the first time)
if it is >1 -> calc last element of index list - second last element
For example:
[0,0,1,0]
n = 0
#0 is at index 0,1,3
index_list = [0,1,3]
len(index_list) > 1:
3 - 1 = 2
So you add 2 to the sequence:
[0,0,1,0,2]
n = 2
index_list = [4]
len = 1 -> add 0
[0,0,1,0,2,0]
and so on...
+ 1
JoÄo Joana,
I'm not good in English either, I just use Google translator, it's okay ...
Maybe you can show me a little example (sample list), hopefully I can understand better with that ...
It's quite an intriguing task I guess ...