Mar-31-2024, 09:40 AM
(This post was last modified: Mar-31-2024, 04:45 PM by ForsakenDusk.)
Hello everyone,
I am currently doing procedural programming exercices, and there are a lot of extra outputs I want to understand before switching to OOP exercises, I still already checked some OOP concepts to see if It could help me understanding such outputs but currently I'm in a dead end.
The goal of the current exercise is to take each letter of the sentence to code and to forward it 4 rows further into the alphabet, not saving spaces.
Example : "Ave caesar" -> "ezigeiwev"
I succeed with concatenating str, but when I try adding space, I fail.
I fail too with using str.replace()
Here's my code :
Any help would be appreciated.
I use Win10 x64 - Python 3.10 - IDE Wing Personal 10.0.3.0
edit : for method 3 there are 26 spaces (which is len(ALPHABET)) between both words but it's showing only 1 I don't know why.
I am currently doing procedural programming exercices, and there are a lot of extra outputs I want to understand before switching to OOP exercises, I still already checked some OOP concepts to see if It could help me understanding such outputs but currently I'm in a dead end.
The goal of the current exercise is to take each letter of the sentence to code and to forward it 4 rows further into the alphabet, not saving spaces.
Example : "Ave caesar" -> "ezigeiwev"
I succeed with concatenating str, but when I try adding space, I fail.
I fail too with using str.replace()
Here's my code :
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
GAP = 4
chain_to_code = "ave caesar"
concatenated_chain = ""
for character in range(len(chain_to_code)):
for letter in range(len(ALPHABET)):
if chain_to_code[character] == ALPHABET[letter]:
concatenated_chain += ALPHABET[(letter + GAP) % len(ALPHABET)]
copied_and_coded_chain = chain_to_code
for character in range(len(copied_and_coded_chain)):
for letter in range(len(ALPHABET)):
if copied_and_coded_chain[character] == ALPHABET[letter]:
copied_and_coded_chain = copied_and_coded_chain.replace(copied_and_coded_chain[character], ALPHABET[(letter + GAP) % len(ALPHABET)], 1)
break
concatenated_chain_with_spaces = ""
for character in range(len(chain_to_code)):
for letter in range(len(ALPHABET)):
if chain_to_code[character] == ALPHABET[letter]:
concatenated_chain_with_spaces += ALPHABET[(letter + GAP) % len(ALPHABET)]
elif chain_to_code[character] == " ":
concatenated_chain_with_spaces += " "
print(f"chain to code : {chain_to_code}")
print(f"method 1, concatenaded chain : {concatenated_chain}")
print(f"method 2, with str.replace and spaces : {copied_and_coded_chain}")
print(f"method 3, concatenaded chain with spaces : {concatenated_chain_with_spaces}")Here's the output :Output:chain to code : ave caesar
method 1, concatenaded chain : ezigeiwev
method 2, copied and coded chain : eve ceeser
method 3, concatenaded chain with spaces : ezi geiwevI tried understanding results of method 2 & 3, but I failed.Any help would be appreciated.
I use Win10 x64 - Python 3.10 - IDE Wing Personal 10.0.3.0
edit : for method 3 there are 26 spaces (which is len(ALPHABET)) between both words but it's showing only 1 I don't know why.
