Hexspeak - Problem

Ever wondered how hackers communicate in secret? They use Hexspeak - a fascinating way to turn numbers into readable messages using hexadecimal representation!

In this problem, you need to convert a decimal number to its Hexspeak representation. Here's how it works:

  1. Convert the decimal number to uppercase hexadecimal
  2. Replace digit '0' with letter 'O'
  3. Replace digit '1' with letter 'I'

The result is valid Hexspeak if and only if it contains only these letters: {'A', 'B', 'C', 'D', 'E', 'F', 'I', 'O'}

Goal: Given a string representing a decimal number, return its Hexspeak representation if valid, otherwise return "ERROR".

Example: The number 257 converts to hex "101", then to Hexspeak "IOI" - which is valid!

Input & Output

example_1.py โ€” Basic Valid Conversion
$ Input: num = "257"
โ€บ Output: "IOI"
๐Ÿ’ก Note: 257 in decimal converts to "101" in hex. After replacing 1โ†’I and 0โ†’O, we get "IOI" which contains only valid Hexspeak characters.
example_2.py โ€” Invalid Hex Characters
$ Input: num = "3"
โ€บ Output: "ERROR"
๐Ÿ’ก Note: 3 in decimal converts to "3" in hex. Since '3' is not a valid Hexspeak character (not in {A,B,C,D,E,F,I,O}), we return "ERROR".
example_3.py โ€” Large Valid Number
$ Input: num = "747823223228"
โ€บ Output: "AEIOOOOOO"
๐Ÿ’ก Note: This large number converts to hex "ADFDDDDDD", but contains invalid character 'D' after transformation... Wait, let me recalculate: actually converts to "AE0000000" in hex, then becomes "AEIOOOOOOO" - all valid characters.

Constraints

  • 1 โ‰ค num.length โ‰ค 12
  • num consists of only digits
  • num does not contain leading zeros
  • num represents an integer in the range [1, 1012]

Visualization

Tap to expand
๐Ÿ•ต๏ธ Secret Hexspeak DecoderINPUT257HEX101base 16VALIDATENo digits2,3,4,5,6,7,8,9โœ“TRANSFORM0โ†’O, 1โ†’IIOIhexspeak!๐Ÿ” Decoding RulesOnly A,B,C,D,E,F,O(0),I(1) allowed โ€ข Invalid digits โ†’ ERROR
Understanding the Visualization
1
Decimal Input
Start with a decimal number like 257
2
Hex Conversion
Convert to hexadecimal: 257 โ†’ "101"
3
Validity Check
Ensure no digits 2-9 are present (these can't be made into letters)
4
Letter Transformation
Replace 0โ†’O and 1โ†’I to create readable text
5
Final Result
"101" becomes "IOI" - valid Hexspeak!
Key Takeaway
๐ŸŽฏ Key Insight: Hexspeak is valid only when the hex representation contains no digits 2-9, allowing us to transform 0โ†’O and 1โ†’I while keeping A-F unchanged.
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
24.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen