Sequence of numbers that reads the previous numbers and adds them in a list (I canā€™t solve this on Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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.

26th Nov 2021, 7:15 PM
JoĆ£o Joana
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
27th Nov 2021, 2:49 AM
JoĆ£o Joana
+ 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 ...
27th Nov 2021, 2:43 AM
Ipang
27th Nov 2021, 6:25 AM
Per Bratthammar
Per Bratthammar - avatar
+ 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
27th Nov 2021, 2:28 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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...
26th Nov 2021, 8:18 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 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 ...
27th Nov 2021, 2:54 AM
Ipang