Given an integer num
, return a string of its base 7 representation.
Example 1:
Input: num = 100 Output: "202"
Example 2:
Input: num = -7 Output: "-10"
Constraints:
-107 <= num <= 107
program main
implicit none
integer, parameter :: num = 100
character(len=:), allocatable :: str
str = base_7_repr(num)
write (unit=*, fmt='(A)') trim(str)
contains
function base_7_repr(n) result(res)
implicit none
integer, intent(in) :: n
character(len=:), allocatable :: res
character(len=30), parameter :: digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
integer :: i, sign
if (n == 0) then
allocate (character(len=1) :: res)
res = '0'
return
end if
sign = 1
if (n < 0) then
sign = -1
end if
i = abs(n)
do while (i /= 0)
allocate (character(len=len(res)+1) :: res)
res = digits(mod(i, 7)+1) // res
i = i / 7
end do
if (sign < 0) then
allocate (character(len=len(res)+1) :: res)
res = '-' // res
end if
end function base_7_repr
end program main
temp.f95:34:34: 34 | res = digits(mod(i, 7)+1) // res | 1 Error: Invalid character in name at (1)
[SOURCE]
module base_7
implicit none
private
public :: to_base_7
contains
function to_base_7(num) result(str)
integer, intent(in) :: num
character(len=20) :: str
if (num == 0) then
str = '0'
else
str = ''
do while (num /= 0)
str = char(ichar('0') + mod(num, 7)) // str
num = num / 7
end do
end if
end function to_base_7
end module base_7
program main
use base_7
implicit none
integer :: num
num = 100
print *, to_base_7(num)
num = -7
print *, to_base_7(num)
end program main
[/SOURCE]
[TESTS]
num = 100
call assert(to_base_7(num) == "202")
num = -7
call assert(to_base_7(num) == "-10")
[/TESTS]
temp.f95:1:2: 1 | [SOURCE] | 1 Error: Invalid character in name at (1) temp.f95:17:16: 17 | num = num / 7 | 1 Error: Dummy argument ‘num’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:24:9: 24 | use base_7 | 1 Fatal Error: Cannot open module file ‘base_7.mod’ for reading at (1): No such file or directory compilation terminated.
def convert_to_base7(num: int) -> str:
if num == 0:
return "0"
result = ""
negative = num < 0
if negative:
num *= -1
while num > 0:
result = str(num % 7) + result
num //= 7
return "-" + result if negative else result
The algorithm first checks if the input number is 0, and if so, it simply returns the base-7 representation as "0". If the number is negative, it keeps track of the sign and converts the number to positive. Then, it iteratively divides the number by 7 (integer division) and prepends the remainder (modulus) to the result string. Finally, if the input number was negative, it adds the minus sign back to the result string. The same algorithm is implemented in C++, Java, Python, and JavaScript with minor syntax adjustments.
std::string convertToBase7(int num) {
if (num == 0) return "0";
std::string result = "";
bool negative = num < 0;
if (negative) num *= -1;
while (num > 0) {
result = std::to_string(num % 7) + result;
num /= 7;
}
return negative ? "-" + result : result;
}
The algorithm first checks if the input number is 0, and if so, it simply returns the base-7 representation as "0". If the number is negative, it keeps track of the sign and converts the number to positive. Then, it iteratively divides the number by 7 (integer division) and prepends the remainder (modulus) to the result string. Finally, if the input number was negative, it adds the minus sign back to the result string. The same algorithm is implemented in C++, Java, Python, and JavaScript with minor syntax adjustments.