What is the error that is presented in this code to determine if there are equal neighbors (repeated consecutive) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 11

What is the error that is presented in this code to determine if there are equal neighbors (repeated consecutive)

arr = [3,6,4,7,1,6,4,4] for item in arr: if arr.count(item) > 1: if item == arr[arr.index(item)+1]: print ("Repeated") break else: print ("Not Repeated") #output => Not Repeated In the "arr" array there are elements repeated together, however the code shows that there are no neighbors together

28th Feb 2018, 1:29 PM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
8 ответов
+ 3
the index method returns is the position of the element, when the element is repeated more than once it will take the element in its smallest position, in this case the number 4 is repeated three times, when it reaches position 6 the will take the element (4) and will take it in its smallest position which is the position 2, then add the value of 1 and results in 3, and the element that is in position 3 is the 7 and that does not work
8th Mar 2018, 12:12 PM
victor
victor - avatar
+ 1
this code throws an error because it is looking for the repeated elements of an array and is using the index method which is the one that finds out the position of an element and will always return the first index it finds in this case, observes that there are several Four times repeated and will only take the first four that you find and it is the one that will compare if you have a repeated neighbor and as this finding out by positions is going to be compared with the next element that is seven so that is the error
8th Mar 2018, 11:12 PM
lisbeth luna
lisbeth luna - avatar
+ 1
good my humble appreciation and if they can be corrected I appreciate the error is in the index and when comparing for example item number 4 compares it with 7 and says it does not repeat and enters the else and if there are consecutive neighbors repeated As my partner Jesus said it would be solved initialize and finished the index
9th Mar 2018, 1:40 AM
deibys
+ 1
the error is found by adding +1 arr [arr.index (item) +1] the error is found on line 5 if item == arr [arr.index (item) +1]: happens in the comparison because arr [arr.index (item) +1] indicates a position if it does not have the 1 enter and verify
10th Mar 2018, 5:23 AM
Doniel Acosta
Doniel Acosta - avatar
+ 1
the arrangement contains a seguidilla.
10th Mar 2018, 5:37 AM
Doniel Acosta
Doniel Acosta - avatar
0
arr = [3,6,4,7,1,6,4,4] n = 0 for item in arr: if arr.count(item) > 1: if item == arr[(arr.index(item, n, len(arr)))+1]: print ("Repeated") break n = n+1 else: print ("Not Repeated") the index returns the first element it finds, it is solved with the start and end of the search
8th Mar 2018, 5:34 PM
David Avila
David Avila - avatar
- 1
Try: for i in range (len (arr) - 1): if arr [i] == arr [i+1]: print ("Repeated") break else: print ("Not repeated")
28th Feb 2018, 1:33 PM
spcan
spcan - avatar
- 1
el método index devuelve es la posición del elemento, pero como el elemento ( en este caso el 4) se repite mas de una vez el va a devolver su posición mas pequeña es decir la primera que encuentre, el esta tomando la posición 2 y le suma 1 es 3 y el elemento que esta en la posición 3 es el 7 y por eso no se cumple la condición
7th Mar 2018, 10:36 PM
victor
victor - avatar