rrr
rrr

Reputation: 371

Finding the smallest element greater than a given value in a list

I am writing a function that returns the minimum value of numbers that are greater than a certain value in a list. For instance, if the given value is 3 from [1,2,3,4,5], it should return 4. However, none of my attempts are working.

I have used the 'min' function, and tried the while and for loop to solve the problem.

def smallest_greater(seq, value):
    i = 0
    while i < len(seq):
        if seq[i] > value:
            i = i + 1
    return min(seq[i])

def smallest_greater(seq, value):
    i = 0
    for value in seq:
        if seq[i] > value:
            i = i + 1
    return min(seq[i])

If I try running the code with the while loop, it does not execute the code. If I run the code with for loop, it gives "TypeError: 'int' object is not iterable"

Upvotes: 2

Views: 6893

Answers (5)

Seraph Wedd
Seraph Wedd

Reputation: 864

Proven that you are given an unordered list of numbers, then you would need to sort the list first.

on your code, the first smallest_greater should work if you switch the > with <= in if seq[i] > value:. However, the return value shouldn't be min(seq[i]) but instead only seq[i]. For the other one, the problem is that you are overwriting the value of value with the for loop leading to your code's failure.

Fixing your first function, you will have a working code with:

def smallest_greater(seq, value):
    seq.sort()
    i = 0
    while i < len(seq):
        if seq[i] <= value:
            i = i + 1
        else: return seq[i]

Which should give your desired output.

Edit:

If I were to do this, then it will be:

def smallest_greater(seq, value):
    try:
        return min(x for x in seq if x > value)
    except ValueError:
        return None

Upvotes: 0

Sajeer Noohukannu
Sajeer Noohukannu

Reputation: 678

sorted and min all uses extra looping. You could do it in a single loop for better performance

k = 3
x = None
for item in a:
    x = item if item > k and (x is None or x > item) else x

Now you have the smallest greater element in x if there is any else None

Upvotes: 0

Ordec
Ordec

Reputation: 109

seq.sort()
i = seq.index(value)
if (i < seq.size() - 1):
    return seq[i+1]
else return seq[i]

Sort array, find index of the value, the next value in list will be the least greater than your value. If statement checks if your value is the greatest in array

Upvotes: 0

Osman Mamun
Osman Mamun

Reputation: 2882

Your code is taking minimum of one value rather it should take minimum of the filtered list. First populate another list with the desired value that satisfies the criteria and then take the minimum:

In [3]: def smallest_greater(seq, value):
   ...:     lst = []
   ...:     for s in seq:
   ...:         if s > value:
   ...:             lst.append(s)
   ...:     return min(lst)


In [4]: smallest_greater([1, 2, 3, 4, 5, 6], 5)
Out[4]: 6

In [5]: smallest_greater([1, 2, 3, 4, 5, 6], 3)
Out[5]: 4

Upvotes: 0

Chris
Chris

Reputation: 29742

Your while loop will go infinite loop unless every number in seq is greater than value (which is what's happening, since i will never exceed len(seq))

Besides, incrementing i is doing a count of number greater than value, and not exactly giving the index of smallest greater.

You can do this in one line with list comprehension:

min(i for i in [1,2,3,4,5] if i > 3)

Output:

4

Upvotes: 6

Related Questions