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:
- Convert the decimal number to uppercase hexadecimal
- Replace digit
'0'with letter'O' - 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code