Python Forum
Padlock of alphabetical strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Padlock of alphabetical strings
#6
I think you are taking this too literally:
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:
for example if s = j and f = a loop would be ran until 'j' eventually becomes 'a'.
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.

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?
Reply


Messages In This Thread
Padlock of alphabetical strings - by Men - Jan-12-2022, 05:32 PM
RE: Padlock of alphabetical strings - by Larz60+ - Jan-13-2022, 10:47 AM
RE: Padlock of alphabetical strings - by Men - Jan-13-2022, 03:44 PM
RE: Padlock of alphabetical strings - by deanhystad - Jan-13-2022, 08:20 PM
RE: Padlock of alphabetical strings - by Men - Jan-13-2022, 09:15 PM
RE: Padlock of alphabetical strings - by deanhystad - Jan-13-2022, 10:48 PM
RE: Padlock of alphabetical strings - by Men - Jan-14-2022, 04:36 AM
RE: Padlock of alphabetical strings - by deanhystad - Jan-14-2022, 06:56 PM
RE: Padlock of alphabetical strings - by Men - Jan-14-2022, 09:46 PM
RE: Padlock of alphabetical strings - by deanhystad - Jan-15-2022, 12:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting row values alphabetical PolskaYBZ 1 2,506 Jan-27-2019, 01:49 PM
Last Post: stullis
  Write a code to output in alphabetical order AbdelaliPython 1 4,646 Jan-19-2018, 09:03 PM
Last Post: j.crater
  Strings inside other strings - substrings OmarSinno 2 3,698 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020