A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit '0' with the letter 'O', and the digit '1' with the letter 'I'. Such a representation is valid if and only if it consists only of the letters in the set {'A', 'B', 'C', 'D', 'E', 'F', 'I', 'O'}.
Given a string num representing a decimal integer n, return the Hexspeak representation of n if it is valid, otherwise return "ERROR ".
Example 1:
Input: num = "257 " Output: "IOI " Explanation: 257 is 101 in hexadecimal.
Example 2:
Input: num = "3 " Output: "ERROR "
Constraints:
1 <= num.length <= 12num does not contain leading zeros.[1, 1012].program main
implicit none
character(len=12) :: num
character(len=4) :: hexspeak
! Example 1
num = "257"
write(*,*) "Example 1:"
write(*,*) "Input: ", trim(num)
hexspeak = hexspeak_representation(num)
write(*,*) "Output: ", trim(hexspeak)
! Example 2
num = "3"
write(*,*) "Example 2:"
write(*,*) "Input: ", trim(num)
hexspeak = hexspeak_representation(num)
write(*,*) "Output: ", trim(hexspeak)
contains
function hexspeak_representation(num) result(hexspeak)
implicit none
character(len=*), intent(in) :: num
character(len=4) :: hexspeak
character(len=12) :: hexstring
hexstring = num
call toupper(hexstring)
hexspeak = ""
do i = 1, len_trim(hexstring)
select case (hexstring(i:i))
case ("0")
hexspeak = hexspeak // "O"
case ("1")
hexspeak = hexspeak // "I"
case default
hexspeak = hexspeak // hexstring(i:i)
end select
end do
if (all(hexspeak == ["A", "B", "C", "D", "E", "F", "I", "O"])) then
hexspeak = trim(hexspeak)
else
hexspeak = "ERROR "
end if
end function hexspeak_representation
end program main
temp.f95:32:12:
32 | do i = 1, len_trim(hexstring)
| 1
Error: Symbol โiโ at (1) has no IMPLICIT type
PROGRAM Hexspeak
IMPLICIT NONE
CHARACTER(len=12) :: num
CHARACTER(len=12) :: hexspeak
INTEGER :: n
! Read input
READ(*,*) num
! Convert to integer
READ(num,*) n
! Convert to hexspeak
hexspeak = num
CALL replace_zero(hexspeak)
CALL replace_one(hexspeak)
! Check if valid
IF (hexspeak .EQ. "ERROR ") THEN
WRITE(*,*) "ERROR "
ELSE
WRITE(*,*) hexspeak
END IF
CONTAINS
SUBROUTINE replace_zero(str)
CHARACTER(len=12), INTENT(INOUT) :: str
INTEGER :: i
DO i = 1, 12
IF (str(i:i) .EQ. "0") THEN
str(i:i) = "O"
END IF
END DO
END SUBROUTINE replace_zero
SUBROUTINE replace_one(str)
CHARACTER(len=12), INTENT(INOUT) :: str
INTEGER :: i
DO i = 1, 12
IF (str(i:i) .EQ. "1") THEN
str(i:i) = "I"
END IF
END DO
END SUBROUTINE replace_one
END PROGRAM Hexspeak
temp.f95:6:29:
6 | CHARACTER(len=12) :: hexspeak
| 1
Error: Symbol โhexspeakโ at (1) cannot have a type
temp.f95:16:9:
16 | hexspeak = num
| 1
Error: โhexspeakโ at (1) is not a variable
temp.f95:17:27:
17 | CALL replace_zero(hexspeak)
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:18:26:
18 | CALL replace_one(hexspeak)
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:21:13:
21 | IF (hexspeak .EQ. "ERROR ") THEN
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:23:4:
23 | ELSE
| 1
Error: Unexpected ELSE statement at (1)
temp.f95:24:23:
24 | WRITE(*,*) hexspeak
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:25:3:
25 | END IF
| 1
Error: Expecting END PROGRAM statement at (1)
def to_hexspeak(num: str) -> str:
n = int(num)
hex_str = []
while n:
rem = n % 16
if rem == 1: hex_str.insert(0, 'I')
elif rem == 0: hex_str.insert(0, 'O')
elif rem > 9: hex_str.insert(0, chr(rem - 10 + ord('A')))
else: return 'ERROR'
n //= 16
return ''.join(hex_str)
The algorithm first converts the input number (given as a string) to a long integer (we'll call it n). We then use a loop to construct the Hexspeak representation in reverse order. In each iteration, we calculate the remainder rem of n divided by 16. If rem is 1, we append 'I' to the beginning of the string; if it is 0, we append 'O'; if it is greater than 9, we append the corresponding letter ('A' to 'F'); and if it is none of these, then the Hexspeak representation is invalid, and we return "ERROR". After processing each character, we update n by dividing it by 16. Finally, we return the constructed Hexspeak representation.
This algorithm runs in O(log(n)) time complexity, as each iteration of the loop reduces n by a factor of 16. The space complexity is also O(log(n)), as the constructed Hexspeak representation string has a length proportional to the number of hexadecimal digits in n.
#include <string>
std::string toHexspeak(std::string num) {
long n = std::stol(num);
std::string hex = "";
while (n) {
int rem = n % 16;
if (rem == 1) hex = "I" + hex;
else if (rem == 0) hex = "O" + hex;
else if (rem > 9) hex = (char)(rem - 10 + 'A') + hex;
else return "ERROR";
n /= 16;
}
return hex;
}
The algorithm first converts the input number (given as a string) to a long integer (we'll call it n). We then use a loop to construct the Hexspeak representation in reverse order. In each iteration, we calculate the remainder rem of n divided by 16. If rem is 1, we append 'I' to the beginning of the string; if it is 0, we append 'O'; if it is greater than 9, we append the corresponding letter ('A' to 'F'); and if it is none of these, then the Hexspeak representation is invalid, and we return "ERROR". After processing each character, we update n by dividing it by 16. Finally, we return the constructed Hexspeak representation.
This algorithm runs in O(log(n)) time complexity, as each iteration of the loop reduces n by a factor of 16. The space complexity is also O(log(n)), as the constructed Hexspeak representation string has a length proportional to the number of hexadecimal digits in n.