Hi everyone. I am having a hard time modifying this code, I'm really new to python and I am trying to find the closest pair among the 10 input integers from a user. Here's my code so far and there is a syntax error...

a = [0,1,2,3,4,5,6,7,8,9]

a[0]=input()
a[1]=input()
a[2]=input()
a[3]=input()
a[4]=input()
a[5]=input()
a[6]=input()
a[7]=input()
a[8]=input()
a[9]=input()


a.sort()
b=sorted(set(a))

for item in enumerate(a):
    for item1 in enumerate(b):
        c = item - enumerate(b)
        if c = item-1:
            print item
            print c

I would really appreciate any help...

Thanks,
Ailen

im not sure but i guess this might work but it has also an error:

a = [0,1,2,3,4,5,6,7,8,9]

a[0]=input()
a[1]=input()
a[2]=input()
a[3]=input()
a[4]=input()
a[5]=input()
a[6]=input()
a[7]=input()
a[8]=input()
a[9]=input()


a.sort()
#b=sorted(set(a))
b=a
for item in enumerate(a):
    for item1 in enumerate(b)
        c = item - item1
        if c == item-1:
            print item
            print c

Create a programming code that will accept ten integers
and will display the closest pair among the group.

sample Run

Enter 10 integers
90 5 27 63 12 47 10 150 120 48

The closest pair is 47 and 48

Add a print statement after this line to see what you are doing

for item in enumerate(a):
    for item1 in enumerate(b)
        print "checking" item, item1

You want to subtract 2 items in the list, convert the difference to a positive number, and store the smallest number found, and if you want, store the two numbers as well. See Looping Over Lists here and re-read the answers to your post on StackOverflow.com which say you are not using enumerate properly.

You can also use the Schwartzian approach. Create a list of (abs(x - y), x, y) tuples, excluding matches where abs(x - y) == 0. Then use the min() function on the list.

a = [0,1,2,3,4,5,6,7,8,9]

a[0]=input()
a[1]=input()
a[2]=input()
a[3]=input()
a[4]=input()
a[5]=input()
a[6]=input()
a[7]=input()
a[8]=input()
a[9]=input()

In Python we dont like long code that dos repating stuff.

>>> [int(raw_input('Enter numbers: ')) for i in range(10)]
[90, 5, 27, 63, 12, 47, 10, 150, 120, 40]

After sorting you can use zip() to make number par.

>>> lst
[5, 10, 12, 27, 47, 48, 63, 90, 120, 150]
>>> zip(lst,lst[1:])
[(5, 10),
 (10, 12),
 (12, 27),
 (27, 47),
 (47, 48),
 (48, 63),
 (63, 90),
 (90, 120),
 (120, 150)]

The next step can be to iterate over number par and and get subtract result.
Then from that list take out min() number index value in zip() list.

So here is here is my version,not tested probably only with this list.

def foo(lst):
    lst = sorted(set(lst))
    number_par = zip(lst,lst[1:])
    lowest_par_result = [abs(y-x) for x,y in number_par]
    lowest_par_index = lowest_par_result.index(min(lowest_par_result))
    return number_par[lowest_par_index]

lst = [90, 5, 27, 63, 12, 47, 10, 150, 120, 48]
print foo(lst) #(47, 48)
commented: best n*log(n) approach +13

Since this is obvioulsy homework, I doubt they can use min, but will have to iterate over the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.