Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find the position of a target value within an array sorted after applying a function
ResourceFunction["BinarySearchBy"][list,val,f] gives the position of the last element ei in list such that f[ei]≤val. |
Find the position of the last pair whose first element is not greater than 5:
In[1]:= | ![]() |
Out[1]= | ![]() |
BinarySearchBy is essentially equivalent to the resource function BinarySearch after applying f to elements of list:
In[2]:= | ![]() |
Out[2]= | ![]() |
In[3]:= | ![]() |
Out[3]= | ![]() |
In[4]:= | ![]() |
Out[4]= | ![]() |
BinarySearchBy[list,val,f] can give an erroneous result if elements obtained by applying f to list are not arranged in ascending order:
In[5]:= | ![]() |
In[6]:= | ![]() |
Out[6]= | ![]() |
Get the result after sorting the list:
In[7]:= | ![]() |
Out[7]= | ![]() |
In[8]:= | ![]() |
Out[8]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License