Created
October 13, 2021 06:30
-
-
Save msyvr/613a025852c9b25dbecf2fcdad05272e to your computer and use it in GitHub Desktop.
recursion: find the highest index for a number in a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hi_index(n, l): | |
if l[-1] == n: | |
return len(l)-1 | |
else: | |
return hi_index(n, l[0:len(l)-1]) | |
if __name__ == "__main__": | |
l = [3, 5, 4, 3, 4, 9, 5, 7, 7, 9] | |
print(f'given the list {l}\n') | |
n = int(input('pick a number to find its largest index: ')) | |
print(f'The last index for {n} in {l} is {hi_index(n, l)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment