Python Forum
Padlock of alphabetical strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Padlock of alphabetical strings
#4
You don't reset op_count. You probably don't want to reset op_code because it keeps track of the total number of "+" steps. Stepping from "a" to "i" is 8 "+" steps. Stepping "e" to "v" is 17 "+" steps. 17 + 8 == 25. Oops! Your code says there are 9 and 27 steps. Where are the extra steps coming from?

The extra step is here:
for i in range(valS, valF-1, -1):
                op_count += 1
You are counting fence posts when you should be counting sections. Take the case of stepping "a" to "i". The steps would be:
a->b->c->d->e->f->g->h->i
If you count the letters you get 9, but you should be counting the steps, not the letters. Each "->" is a step. Count them up and you get 8 steps. Your loop should go something like this:
for i in range(valS, valF, -1):
                op_count += 1
This gives the correct number of steps, but why are you looping at all? You already know the number of steps = valF - valS. You don't need to write a loop to increment a counter if you already know how many times the loop will execute. Would you do math like this?
# Add 8 + 17
total = 0
for _ in range(8):
    total += 1
for _ in range(17):
    total += 1
I hope not! But this is what you are doing in your code. You have too many loops in your code. There should only be one. You also have too many "if" statements. You only need one (or maybe none).
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,494 Jan-27-2019, 01:49 PM
Last Post: stullis
  Write a code to output in alphabetical order AbdelaliPython 1 4,637 Jan-19-2018, 09:03 PM
Last Post: j.crater
  Strings inside other strings - substrings OmarSinno 2 3,687 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