You are given a string sentence
that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin " (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:
'a'
, 'e'
, 'i'
, 'o'
, or 'u'
), append "ma "
to the end of the word."apple "
becomes "applema "
."ma "
."goat "
becomes "oatgma "
.'a'
to the end of each word per its word index in the sentence, starting with 1
."a "
added to the end, the second word gets "aa "
added to the end, and so on.Return the final sentence representing the conversion from sentence to Goat Latin.
Example 1:
Input: sentence = "I speak Goat Latin" Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: sentence = "The quick brown fox jumped over the lazy dog" Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Constraints:
1 <= sentence.length <= 150
sentence
consists of English letters and spaces.sentence
has no leading or trailing spaces.sentence
are separated by a single space.program main
implicit none
character(len=:), allocatable :: sentence
integer :: i
! Example 1
sentence = "I speak Goat Latin"
print '(A)', goat_latin(sentence)
! Example 2
sentence = "The quick brown fox jumped over the lazy dog"
print '(A)', goat_latin(sentence)
contains
function goat_latin(sentence) result(result_sentence)
implicit none
character(len=*), intent(in) :: sentence
character(len=:), allocatable :: result_sentence
logical :: is_vowel
integer :: i
allocate(character(len=len(sentence)+3) :: result_sentence)
do i = 1, size(sentence)
is_vowel = any(sentence(i:i) == ['a', 'e', 'i', 'o', 'u'])
if (is_vowel) then
result_sentence = trim(result_sentence) // sentence(i:i) // "ma "
else
result_sentence = trim(result_sentence) // sentence(i+1:) // sentence(i:i) // "ma "
end if
if (i < size(sentence)) then
result_sentence = trim(result_sentence) // "a "
end if
end do
end function goat_latin
end program main
temp.f95:33:25: 33 | if (i < size(sentence)) then | 1 Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array temp.f95:26:23: 26 | do i = 1, size(sentence) | 1 Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array
! Declare variables
integer :: i, j, k, l, m
character(len=150) :: sentence, word, new_word
! Read input sentence
read(*,*) sentence
! Initialize variables
i = 1
j = 1
k = 1
l = 1
m = 1
! Loop through each word in the sentence
do while (sentence(i:i) /= ' ')
! Get the current word
word = sentence(j:l)
! Check if the word starts with a vowel
if (index('aeiou', word(1:1)) /= 0) then
! Append "ma " to the end of the word
new_word = word // 'ma '
else
! Remove the first letter and append it to the end
new_word = word(2:) // word(1:1) // 'ma '
end if
! Add the letter 'a' to the end of the word
new_word = new_word // 'a'
! Increment variables
i = i + l
j = j + l
k = k + l
l = l + l
m = m + 1
! Print the new word
write(*,*) new_word
! Check if we are at the end of the sentence
if (sentence(i:i) == ' ') then
exit
end if
! Increment variables
i = i + 1
j = j + 1
k = k + 1
l = l + 1
m = m + 1
end do
end [FORTRAN]
temp.f95:55:3: 55 | end [FORTRAN] | 1 Error: Expecting END PROGRAM statement at (1) f951: Error: Unexpected end of file in โtemp.f95โ
def number_of_lines(widths, s):
lines = 1
current_pixel = 0
for c in s:
letter_pixels = widths[ord(c) - ord('a')]
if current_pixel + letter_pixels > 100:
lines += 1
current_pixel = 0
current_pixel += letter_pixels
return [lines, current_pixel]
The algorithm starts by initializing two variables, `lines` and `currentPixel`. `lines` denotes the number of lines it has taken so far to type out the string `s`, and `currentPixel` stores the width of the characters typed on the current line.
The algorithm iterates through each character c
in the input string s
. For each character, it calculates its width in pixels by using its respective entry in the widths
array. If the addition of the width of the character to the currentPixel
would cause it go over 100 pixels, it increases the line count by 1, resets the currentPixel
to 0, and continues processing the rest of the string s
. Otherwise, add the character width to the currentPixel
.
Finally, when the loop finishes iterating all characters in the string s
, the algorithm returns the lines
and currentPixel
as a result array of length 2.
#include <vector>
#include <string>
std::vector<int> numberOfLines(std::vector<int>& widths, std::string s) {
int lines = 1;
int currentPixel = 0;
for (char c : s) {
int letterPixels = widths[c - 'a'];
if (currentPixel + letterPixels > 100) {
lines++;
currentPixel = 0;
}
currentPixel += letterPixels;
}
return {lines, currentPixel};
}
The algorithm starts by initializing two variables, `lines` and `currentPixel`. `lines` denotes the number of lines it has taken so far to type out the string `s`, and `currentPixel` stores the width of the characters typed on the current line.
The algorithm iterates through each character c
in the input string s
. For each character, it calculates its width in pixels by using its respective entry in the widths
array. If the addition of the width of the character to the currentPixel
would cause it go over 100 pixels, it increases the line count by 1, resets the currentPixel
to 0, and continues processing the rest of the string s
. Otherwise, add the character width to the currentPixel
.
Finally, when the loop finishes iterating all characters in the string s
, the algorithm returns the lines
and currentPixel
as a result array of length 2.