0

I want to find the highest number not in the list. The break should end the last for loop and leave me with Ans = highest number not in list. Why doesn't it work

A = [1,3,5,9,11]

u = A[0]
for i in range(1, len(A)):
    if A[i] > u:
            u = A[i]
    if u <= 0:
        Ans = 1

    else:
        for j in range(u ,0, -1):
            if j not in A:
                Ans = j
                print(Ans)
                break
Peter Lynch
  • 119
  • 1
  • 2
  • 6

4 Answers4

1

break gets you out of the inner loop, you also need to exit the outer one. You could do this using a boolean flag, etc... but I prefer using a for - else construct

for-else, means no break, that is the loop will resume in the else attached to the for when there is no break in the inner loop, otherwise, it will execute the following. continue, takes you to the end of the loop.

Now, it seems your logic may also have a problem, the answer printed is 2

A = [1,3,5,9,11]

u = A[0]
for i in range(1, len(A)):
    if A[i] > u:
            u = A[i]
    if u <= 0:
        Ans = 1

    else:
        for j in range(u ,0, -1):
            if j not in A:
                Ans = j
                print(Ans)
                break
        else:    # <-- attention, this "else" must be aligned with the preceding "for", not the "if"
            continue
        break
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
1

As already mentioned, the break works fine, breaking from the loop it is in -- but not from the loop above that!

Alternatively to using for/else or moving the entire calculation into a function and using return instead of break, you could also replace the nested loop with a generator, lazily finding the next value that satisfies the condition. If that value is not None (the default), break from the outer loop.

    else:
        r = next((j for j in range(u ,0, -1) if j not in A), None)
        if r is not None:
            Ans = r
            print(Ans)
            break
tobias_k
  • 81,265
  • 12
  • 120
  • 179
1

Your break doesn't work, because your indentation for if u <= 0: and further is wrong.

A = [-1, -3, -5, -9, -11]
Ans = 1
for j in range(max(A)-1, max(0, min(A)), -1):
    if j not in A:
        Ans = j
        print(Ans)
        break

However, I advise you to use NumPy if your list is huge, because it is much faster.

# Find the highest number not in the list
import numpy as np

l = [1,3,5,9,11]
a = np.array(l)
ans = 1
for i in range(a.max()-1, max(0, a.min()), -1):
    if i not in a:
        ans = i
        print(i)
        break
FooBar167
  • 2,721
  • 1
  • 26
  • 37
  • What makes you think that the indentation is wrong? – tobias_k Sep 14 '17 at 14:25
  • @tobias_k obviously there is no need to set "Ans = 1" many times in the cycle. First, find max. Second, find value that is not in the list. These are 2 different operations. – FooBar167 Sep 14 '17 at 14:32
1

@ foo bar et al.

Seems the indentation was wrong as per your suggestions. While all your other suggestions were more elegant my solution turned out to be:

A = [1,3,5,9,11]

u = A[0]
for i in range(1, len(A)):
    if A[i] > u:
            u = A[i]
if u <= 0:
    Ans = 1
    print(Ans)

else:
    for j in range(u ,0, -1):
            if j not in A:
                Ans = j
                print(Ans)
                break
Peter Lynch
  • 119
  • 1
  • 2
  • 6