DEV Community

HHMathewChan
HHMathewChan

Posted on • Originally published at rebirthwithcode.tech

Python Exercise 16: find the highest index alphabet

Question

  • Given a name, return the letter with the highest index in alphabetical order,
    • with its corresponding index,
    • in the form of a string.
  • You are prohibited to use max() nor is reassigning a value to the alphabet list allowed.

Example

alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] alphabet_index(alphabet, "Flavio")  "22v" alphabet_index(alphabet, "Andrey")  "25y" alphabet_index(alphabet, "Oscar")  "19s" 
Enter fullscreen mode Exit fullscreen mode

My attempt

  • algorithm
>find the letter with the highest index in alphabetical order initialise highest_index to 0 initialise highest_index_character to empty string >>Find the highest index of the character and the relevant character in the name initialise highest_index to 0 initialise highest_index_character to empty string for each character in the name: if character's index in alphabet is higher than in highest_index set highest_index to character's index set highest_index_character to character return string of highest_index and highest_index_character 
Enter fullscreen mode Exit fullscreen mode
  • code
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] def alphabet_index(alphabet: list, name: str): # The name are in upper case, but alphabet is in lower case  name = name.lower() # initialise highest_index to 0  highest_index = 0 # initialise highest_index_character to empty string  highest_index_character = str() # for each character in the name:  for character in name: # if character's index in alphabet is higher than in highest_index  if alphabet.index(character) + 1 > highest_index: # set highest_index to character's index  highest_index = alphabet.index(character) + 1 # set highest_index_character to character  highest_index_character = character # return string of highest_index and highest_index_character  return str(str(highest_index) + highest_index_character) 
Enter fullscreen mode Exit fullscreen mode

Other solution

  • don't find any particular impressive code. If there is shorter way to do this, please let me know.

My reflection

  • This takes me long time to write out the algorithm in appropriate formate, need to practice more

Credit

Top comments (0)