Python Forum
Padlock of alphabetical strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Padlock of alphabetical strings
#8
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 steps
This 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 steps
Converting 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)
Men likes this post
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,504 Jan-27-2019, 01:49 PM
Last Post: stullis
  Write a code to output in alphabetical order AbdelaliPython 1 4,645 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