-1

I have a list. I want to find out which number in the list is closest to k between highest and lowest but without using lambda or abs. How can I do it?

a=[1,5,8,4,9,25,10,16,54]
k=12
highest = max(a)
lowest = min(a)
Red
  • 26,798
  • 7
  • 36
  • 58
  • [bisect](https://docs.python.org/3/library/bisect.html) – dawg Feb 07 '21 at 17:20
  • 2
    Does this answer your question? [from list of integers, get number closest to a given value](https://stackoverflow.com/questions/12141150/from-list-of-integers-get-number-closest-to-a-given-value) – zvi Feb 07 '21 at 17:21
  • 1
    Why the arbitrary constraint? Both `abs` and `lambda` are built-in features of python. – Ted Klein Bergman Feb 07 '21 at 17:21
  • sort the list and then use linear search or binary search – sahasrara62 Feb 07 '21 at 17:23
  • @dawg What does `bisect` have to do with any of this? And how is a question dupe where all answers use `abs` or `lambda`? – user2390182 Feb 07 '21 at 17:23
  • @schwobaseggl [This answer](https://stackoverflow.com/a/12141511/6486738) use bisect without either `abs` or `lambda`. – Ted Klein Bergman Feb 07 '21 at 17:25
  • actually I don't wanna use any built in function. is there any way without built in function ? – Attic Rahman Feb 07 '21 at 17:27
  • @TedKleinBergman And yet it assumes a sorted list. – user2390182 Feb 07 '21 at 17:29
  • @AtticRahman Lambda is not a built-in function though, it's a anonymous function declaration. It's exactly the same as if you were to define a function but without a name. Also, `abs` is trivial to define yourself, so you could create it yourself and use that. – Ted Klein Bergman Feb 07 '21 at 17:30
  • @schwobaseggl Yes, but then you sort it before passing it in. Maybe not the most efficient, but that way you avoid using `abs` and `lambda`. – Ted Klein Bergman Feb 07 '21 at 17:32

2 Answers2

1

Try this tricky code,

Code Syntax

for each in sorted(a):
    if each < k:
       lowest = each
    else:
        highest = each
        break

print(f"Original List: {a}\nLowest according to ({k}): {lowest}\nHighest according to ({k}): {highest}")

Output

Original List: [1, 5, 8, 4, 9, 25, 10, 16, 54]
Lowest according to (12): 10
Highest according to (12): 16

[Program finished]
Ahmed
  • 796
  • 1
  • 5
  • 16
0
a=[1,5,8,4,9,25,10,16,54]
k=12


import sys

def func(lis, val):
    # minimum_difference is used to check the difference between value in list and key we have and it will keep record of minimum difference
    minimum_difference = sys.maxsize
    # nearest_value will save the list value for which we got minimum difference

    nearest_value = None
    # iterating through the list elements
    for i in lis:
        # getting the difference b/w key value and list element
        difference_key_element = val - i
        # checking if difference is neagtive or not, if negative make
        # it positive
        if difference_key_element<0:
            difference_key_element *= -1
        # checking if minimum value we find so far, is less then the 
        # current difference b/w key value and element value
        # if it is less then the current min value, replace it 
        # and store the list element in the store variable
        if difference_key_element<minimum_difference:
            minimum_difference=difference_key_element
            nearest_value = i
    # return the store variable
    return nearest_value

solution = func(a, k)
print(solution)

output

10
sahasrara62
  • 10,069
  • 3
  • 29
  • 44