Jan-14-2022, 06:56 PM
Programming has little to do with writing code and much to do with problem solving. If you had to solve this problem with pencil and paper you might start out by writing down the alphabet to make it easier to count.
abcdefghijklmnopqrstuvwxyz |12345678 | a->i = 8 steps |12345678901234567 e->v = 17 stepsThis works fine for a few short strings, but if you had to do 100 long strings you would quickly find it is much faster to write down the ordinal values of all the letters and do the math.
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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 a->i = i - a = 9 - 1 = 8 steps e->v = v - a = 22 - 5 = 17 stepsConverting to Python
plus = minus = 0 for s, p in zip(string, pattern): steps = ord(p) - ord(s) if steps < 0: minus -= steps else plus += steps print("Total steps", plus + minus, "+", plus, "-", minus)