As part of my college class, I have to write a program that returns the "smallest" letter in the string. Input is assumed to be a non empty string. "Smallest" is defined as:
The smallest decimal value of the character, pertaining to this chart.
The range of acceptable values is 0 <= x <= 127
I would like to get feedback on the algorithm of the code. Other suggestions are accepted and welcome, but the main algorithm is the main focus.
def smallest_letter(string: str) -> str:
"""
Returns the smallest letter in the string
Input is restricted to ASCII characters in range 0 <= character <= 127
:param string: A string containing only ASCII letters
:return: A string length one of the smallest letter
"""
# Ensure all characters are within the acceptable range #
for character in string:
assert ord(character) <= 127
smallest_letter = 1000
for letter in string:
if ord(letter) < smallest_letter:
smallest_letter = ord(letter)
return chr(smallest_letter)
if __name__ == "__main__":
# Test Cases #
assert smallest_letter("abydsufaksjdf") == "a"
assert smallest_letter("bfsufsjfbeywafbiu") == "a"
assert smallest_letter("ABYUVGDuibfsafuofiw") == "A"
min("abydsufaksjdf")
? \$\endgroup\$min
method could do those computations. I'll tag this asreinventing-the-wheel
. \$\endgroup\$