Jan-13-2022, 10:48 PM
I think you are taking this too literally:
You could follow the instructions very literally and actually "preform an operation" on each letter until the input string matches the favorite string.
Quote:2. perform an operation to change the first letter in string to the previous or next letter alphabetically then loop back until it is == to that of string f:Note that the return value is not the string, because the modified string will be the same as the "favorite" string. Your function's purpose is to compute the number of "operations" required to convert the input string to the favorite string.
for example if s = j and f = a loop would be ran until 'j' eventually becomes 'a'.
You could follow the instructions very literally and actually "preform an operation" on each letter until the input string matches the favorite string.
def string_stepper(string, pattern): """Calculate number of steps required to move letters in string to the corresponding letter in pattern. """ plus = minus = 0 for s, p in zip(string, pattern): while s > p: s = chr(ord(s)-1) # The "Operation" minus += 1 while s < p: s = chr(ord(s)+1) plus += 1 return plus + minus, plus, minus total, plus, minus = string_stepper('ivhfmh', 'aeiouy') print(f"Total {total}, +{plus}, -{minus}")This does work, but why would anyone solve the problem this way? It is just as goofy as my earlier "doing addition using for loops" example. You know the ordinal value of the letters. The ordinal value is an integer. You can do math with integers. Do you need a loop to do math?