Reputation: 209
chars=input()
a=([int(x.strip()) for x in chars.split(',')])
b=len(a)
count=1
result=0
max=0
for i in range(b-1):
for j in range(i,b):
if a[i] == a[j]:
print(a[i])
count=count+1
result=result+(a[i]*count)
count=0
In the above code I check condition if a[i]==a[j]
is true and I am printing the result if they are equal.
But I get something wrong output.
Given the input
-9,3,0,20,-10,-11,11
The above prints
-9
3
0
20
-10
-11
There are no equal values in the input, why are they all printed?
Upvotes: 0
Views: 41
Reputation: 842
chars=input()
a=chars # input() Equivalent to eval(raw_input(prompt))
b=len(a)
count=1
result=0
max=0
for i in range(b-1):
for j in range(i+1,b): # start from i + 1
if a[i] == a[j]:
print(a[i])
count=count+1
result=result+(a[i]*count)
count=0
Upvotes: 0
Reputation: 1122352
You always have one iteration per iteration over range(i, b)
, where i == j
. You started your inner loop at i
after all, so of course a[i] == a[j]
is going to be true, they are the same index in your list.
Start your inner loop at i + 1
instead:
for i in range(b-1):
for j in range(i + 1, b):
Upvotes: 3