A
A
Azamgl2021-10-03 08:43:01
Python
Azamgl, 2021-10-03 08:43:01

I can not understand the cycle of the program?

spisok = [1, 2, 3, 2, 3]
print(spisok)
spisok.sort()
n = len(spisok)
i=0
for l in range(n-1):
    for j in range(n-l-1):
        if spisok[j]==spisok[j+1]:
            i+=1
print(i)


To find the pairs, I used the bubble method, but I can't figure out why the number of pairs is not correct. Suppose in this code it shows that there are 4 pairs, not 2. But in the code where all elements are the same and there are five of them, it correctly calculates the number of pairs - 10.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-10-03
@Azamgl

If I understand the purpose correctly, then something like this should be:

for l in range(n-1):
    for j in range(l+1, n):
        if spisok[l]==spisok[j]:
            i+=1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question