International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
'a'
maps to ".- "
,'b'
maps to "-... "
,'c'
maps to "-.-. "
, and so on.For convenience, the full table for the 26
letters of the English alphabet is given below:
[ ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. "]
Given an array of strings words
where each word can be written as a concatenation of the Morse code of each letter.
"cab "
can be written as "-.-..--... "
, which is the concatenation of "-.-. "
, ".- "
, and "-... "
. We will call such a concatenation the transformation of a word.Return the number of different transformations among all words we have.
Example 1:
Input: words = [ "gin ", "zen ", "gig ", "msg "] Output: 2 Explanation: The transformation of each word is: "gin " -> "--...-. " "zen " -> "--...-. " "gig " -> "--...--. " "msg " -> "--...--. " There are 2 different transformations: "--...-. " and "--...--. ".
Example 2:
Input: words = [ "a "] Output: 1
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 12
words[i]
consists of lowercase English letters.program main
implicit none
integer :: i, j, n
character(len=12) :: morse_code(26)
character(len=12), allocatable :: words(:)
integer, allocatable :: transforms(:)
integer :: num_transforms
! Define the Morse code for each letter of the English alphabet
morse_code(1) = '.-'
morse_code(2) = '-...'
morse_code(3) = '-.-.'
morse_code(4) = '-..'
morse_code(5) = '.'
morse_code(6) = '..-'
morse_code(7) = '--.'
morse_code(8) = '....'
morse_code(9) = '..'
morse_code(10) = '.---'
morse_code(11) = '-.-'
morse_code(12) = '.-..'
morse_code(13) = '--'
morse_code(14) = '-.'
morse_code(15) = '---'
morse_code(16) = '.--.'
morse_code(17) = '--.-'
morse_code(18) = '.-.'
morse_code(19) = '...'
morse_code(20) = '-'
morse_code(21) = '..-'
morse_code(22) = '...-'
morse_code(23) = '.--'
morse_code(24) = '-..-'
morse_code(25) = '-.--'
morse_code(26) = '--..'
! Read in the list of words
read(*,*) n
allocate(words(n))
do i = 1, n
read(*,*) words(i)
end do
! Count the number of distinct transformations
allocate(transforms(n))
num_transforms = 0
do i = 1, n
transforms(i) = 0
do j = 1, len(words(i))
if (index(morse_code(ichar(words(i)(j:j)) - ichar('a') + 1), &
morse_code(ichar(words(i)(j+1:j+1)) - ichar('a') + 1)) /= 0) then
transforms(i) = transforms(i) + 1
end if
end do
end do
! Print the number of distinct transformations
write(*,*) sum(transforms)
end program main
At line 39 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7a2f860fb960 in ??? #1 0x7a2f860fc4d9 in ??? #2 0x7a2f8635017b in ??? #3 0x7a2f86349684 in ??? #4 0x7a2f8634a2aa in ??? #5 0x5a8fec9406fb in MAIN__ #6 0x5a8fec940d33 in main
program morse_code
implicit none
integer :: i, j, k, n
character(len=12) :: word
character(len=26) :: morse_code(26)
character(len=12) :: transformation
integer :: transformations(100)
! Initialize the Morse code table
morse_code = reshape([character(len=2) :: &
".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. "], &
[26])
! Read the number of words
read(*,*) n
! Initialize the transformations array
do i = 1, n
transformations(i) = 0
end do
! Read the words and calculate the transformations
do i = 1, n
read(*,*) word
transformation = ""
do j = 1, len_trim(word)
k = iachar(word(j:j)) - iachar('a') + 1
transformation = transformation // morse_code(k)
end do
transformations(i) = count(transformations(1:i) /= transformation) + 1
end do
! Print the number of different transformations
write(*,*) transformations(n)
end program morse_code
temp.f95:6:37: 6 | character(len=26) :: morse_code(26) | 1 Error: Symbol โmorse_codeโ at (1) cannot have a type temp.f95:11:17: 11 | morse_code = reshape([character(len=2) :: & | 1 Error: โmorse_codeโ at (1) is not a variable temp.f95:12:132: 12 | ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. "], & | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:13:11: 13 | [26]) | 1 Error: Invalid character in name at (1) temp.f95:29:58: 29 | transformation = transformation // morse_code(k) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:31:36: 31 | transformations(i) = count(transformations(1:i) /= transformation) + 1 | 1 Error: Operands of comparison operator โ/=โ at (1) are INTEGER(4)/CHARACTER(12) f951: some warnings being treated as errors
def rotated_digits(n):
count = 0
for i in range(1, n + 1):
is_valid_after_rotation = True
is_different_after_rotation = False
num = i
while num:
digit = num % 10
if digit in {3, 4, 7}:
is_valid_after_rotation = False
break
if digit in {2, 5, 6, 9}:
is_different_after_rotation = True
num //= 10
if is_valid_after_rotation and is_different_after_rotation:
count += 1
return count
The algorithm loops through all the integers from 1 to n, for each integer it checks if it is a good integer (valid and different after rotation). For checking if the integer is good, we loop through each digit of the integer and check if the digit remains valid after rotation and if it changes or not. If all digits are valid after rotation and at least one digit is different, the integer is a good integer, and we increment the count.
int rotatedDigits(int n) {
int count = 0;
for (int i = 1; i <= n; ++i) {
bool is_valid_after_rotation = true;
bool is_different_after_rotation = false;
int num = i;
while (num != 0) {
int digit = num % 10;
if (digit == 3 || digit == 4 || digit == 7) {
is_valid_after_rotation = false;
break;
}
if (digit == 2 || digit == 5 || digit == 6 || digit == 9) {
is_different_after_rotation = true;
}
num /= 10;
}
if (is_valid_after_rotation && is_different_after_rotation) {
count++;
}
}
return count;
}
The algorithm loops through all the integers from 1 to n, for each integer it checks if it is a good integer (valid and different after rotation). For checking if the integer is good, we loop through each digit of the integer and check if the digit remains valid after rotation and if it changes or not. If all digits are valid after rotation and at least one digit is different, the integer is a good integer, and we increment the count.