Skip to content

Instantly share code, notes, and snippets.

@msyvr
Created October 13, 2021 06:30
Show Gist options
  • Save msyvr/613a025852c9b25dbecf2fcdad05272e to your computer and use it in GitHub Desktop.
Save msyvr/613a025852c9b25dbecf2fcdad05272e to your computer and use it in GitHub Desktop.
recursion: find the highest index for a number in a list
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