8.3 8 Create Your Own Encoding Codehs Answers [work] -
The Solution
def encode(text):
result = ""
for character in text:
# Get the ASCII value of the character
ascii_value = ord(character)
# Add 1 to the ASCII value
new_value = ascii_value + 1
# Convert the new value back to a character
new_char = chr(new_value)
# Add the new character to our result string
result += new_char
return result
# Example usage to test the code
# This will print 'Ifmmp' because 'H'+1='I', 'e'+1='f', etc.
print(encode("Hello"))
7) Example full encoding + decoding pseudocode
Pseudocode — Two-digit A=01 scheme
Encode(text):
result = ""
for ch in text.upper():
if ch == " ":
result += "27"
else if ch == ".":
result += "28"
else:
result += format(ord(ch) - ord('A') + 1, width=2, pad='0')
return result
Decode(code):
result = ""
for i in range(0, len(code), 2):
token = code[i:i+2]
n = int(token)
if n == 27:
result += " "
else if n == 28:
result += "."
else:
result += chr(n - 1 + ord('A'))
return result
Step 5: Test with Examples
Run both functions to ensure decode(encode(message)) == message. 8.3 8 create your own encoding codehs answers
6. Common Mistakes & Debugging Tips
| Mistake | Symptom | Fix |
|---------|---------|-----|
| Using ord(ch) directly | Decoding returns original numbers, not letters | Must create reverse mapping |
| Forgetting case sensitivity | 'A' and 'a' map differently | Use .lower() or handle both |
| Spaces as 0 or 32 | Confusion between index 0 and space character | Pick a consistent special value (e.g., 27) |
| Not handling unknown chars | Crash on punctuation | Add a default (e.g., 99 for ‘?’) | The Solution def encode(text): result = "" for
How to Submit to CodeHS
- Copy the complete solution above into the code editor.
- Click "Check Code" or "Submit".
- If the autograder fails, check the exact prompt—some versions require specific function names like
myEncode()or no spaces in the output. Adjust accordingly.