Skip to content

Instantly share code, notes, and snippets.

@brainyfarm
Created August 8, 2016 14:50
Show Gist options
  • Save brainyfarm/de5b982980a57d3e848341ccec02d04e to your computer and use it in GitHub Desktop.
Save brainyfarm/de5b982980a57d3e848341ccec02d04e to your computer and use it in GitHub Desktop.
FreeCodeCamp: Where do I Belong (Python)
"""
Return the lowest index at which a value (second argument)
should be inserted into an array (first argument) once it has been sorted.
The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5) should return 1
because it is greater than 1 (index 0), but less than 2 (index 1).
Likewise, getIndexToIns([20,3,5], 19) should return 2
because once the array has been sorted it will look like [3,5,20]
and 19 is less than 20 (index 2) and greater than 5 (index 1).
Piece of cake ;)
"""
def getIndexToIns(the_list, num):
the_list.append(num)
the_list.sort()
return the_list.index(num)
print getIndexToIns([5, 3, 20, 3], 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment