Create Your Own Encoding Codehs Answers - 83 8
Below is a fully functional solution that passes the CodeHS autograder. This example uses a custom mapping where each lowercase letter is replaced with a 2-symbol code.
A: Yes, the test cases often include uppercase. Use .toLowerCase() inside encode() to normalize.
Report: Understanding and Solving "8.3 Create Your Own Encoding" (CodeHS) 83 8 create your own encoding codehs answers
Subject: Solution and Explanation for CodeHS AP Computer Science Principles (Unit 8, Lesson 3) Topic: Data Encoding, Binary Representation, and Text Encoding
Always test with: decode(encode("your test string")) — should return identical string. Below is a fully functional solution that passes
A: No. The autograder only checks that decode(encode(message)) === message for several test cases. You can use any mapping.
If you want to be fancy, you can turn the entire sentence into a string of numbers. This is easier to write but harder to read. Below is a functional Python solution that defines
The Code:
def encoder(text):
result = ""
for char in text:
# Convert character to ASCII number and add 5
new_num = ord(char) + 5
# Convert back to character
new_char = chr(new_num)
# Add to result
result += new_char
return result
# Test
print(encoder("abc"))
# Output: fgh (ascii numbers shifted)
Below is a functional Python solution that defines a custom encoding dictionary, allows the user to encode a message, and decode a binary string.
# Part 1: Define the Encoding Scheme (The "Code Book")
# We map characters to unique binary strings.
# Note: A real scheme might use ASCII values, but here we create our own.
my_encoding =
'A': '00001',
'B': '00010',
'C': '00011',
'D': '00100',
'E': '00101',
'F': '00110',
'G': '00111',
'H': '01000',
'I': '01001',
'J': '01010',
'K': '01011',
'L': '01100',
'M': '01101',
'N': '01110',
'O': '01111',
'P': '10000',
'Q': '10001',
'R': '10010',
'S': '10011',
'T': '10100',
'U': '10101',
'V': '10110',
'W': '10111',
'X': '11000',
'Y': '11001',
'Z': '11010',
' ': '11111' # Encoding for Space