Answers | 83 8 Create Your Own Encoding Codehs

: You should use the fewest number of bits possible to represent all 27 required characters. (too few) and (enough), a 5-bit encoding is the most efficient solution. Example 5-Bit Encoding Scheme

# Check if the character is a letter if char.isalpha(): # We are creating a "Shift Cipher" # ord(char) gets the ASCII number. # We subtract 97 to make 'a' equal to 0, 'b' equal to 1, etc. # We add 1 to shift it. # We use % 26 to wrap around from 'z' back to 'a'. # We add 97 back to get the real ASCII number. new_char = chr((ord(char) - 97 + 1) % 26 + 97) result += new_char else: # If it's a space or punctuation, leave it exactly as it is result += char 83 8 create your own encoding codehs answers

def encode(text): result = "" for char in text.lower(): if char == "a": result += "4" elif char == "e": result += "3" elif char == "i": result += "1" elif char == "o": result += "0" elif char == "s": result += "5" else: # If the character isn't in our rules, keep it as is result += char return result # Get user input user_input = input("Enter a message to encode: ") encoded_message = encode(user_input) print("Encoded message: " + encoded_message) Use code with caution. Key Tips for CodeHS Success : You should use the fewest number of

You need an empty string to store the encoded version of your message as you build it. # We subtract 97 to make 'a' equal to 0, 'b' equal to 1, etc

Go to Top