Last answered:

01 Nov 2022

Posted on:

14 Oct 2022


Rank array numbers in Python

In python, how can we  provide a function that takes as input:
●     A number
●     An array

and then provides as an output the rank of the
number in the array. If the number is not part of the array then it should be
the rank of the number lower than the value given.

For example, if the function was given
●     the values 7.23 and
[1.2,4.3,5,7.23,63.1] then the rank should be 4.
●     the values 3.5 and
[1.2,4.3,5,7.23,63.1] then the rank should be 1.
●     the values 100 and
[1.2,4.3,5,7.23,63.1] then the rank should be 5.

Any suggestions would be really appreciated. Thanks

1 answers ( 0 marked as helpful)
Posted on:

01 Nov 2022


Hi Inderjeet!
Thanks for reaching out.

Please, use the following code:
def rank_arr(number, arr=[]):
    if number in arr:
        rank = arr.index(number) + 1
    return rank
This code works if the number is in the array. Could you please explain what the result should be in case the number is not in the array? Please, use new examples because from the provided examples it is not clear. Thank you.

Looking forward to your answer.

Submit an answer