IDNLearn.com: Your one-stop destination for finding reliable answers. Our experts are ready to provide prompt and detailed answers to any questions you may have.

Please create a pseudocode function that will check if all the words in an array are palindromes. For example, an input of ['racecar', 'noon', 'civic'] should return True, but an input of ['racecar', 'shoe', 'moon'] should return False."
The following are three solutions that were returned by the AI:
Solution A

Solution B

Solution C

function reverse_word(string word)
reversed = ""
for letter in word:
reversed = letter + reversed
return reversed

function check_all_palindromes(array arr)
if arr[0] == reverse_word(arr[0])
if arr[1] == reverse_word(arr[1])
if arr[2] == reverse_word(arr[2])
return true
return false

function reverse_word(string word)
reversed = ""
for letter in word:
reversed = letter + reversed
return reversed

function is_palindrome(string word)
return word == reverse_word(word)

function check_all_palindromes(array arr)
for word in arr:
if is_palindrome(word) == false
return false
return true

function reverse_word(string word)
reversed = ""
for letter in word:
reversed = letter + reversed
return reversed

function check_all_palindromes(array arr)
reversed1 = reverse_word(word1)
reversed2 = reverse_word(word2)
reversed3 = reverse_word(word3)

if arr[0] does not equal reversed1:
return false

if arr[1] does not equal reversed2:
return false

if arr[2] does not equal reversed3:
return false
return true


Sagot :

Answer:

See below.

Explanation:

What is a Palindrome?

A palindrome is a string of characters that appears the same when reversed. As mentioned in the question, some examples of palindromes are racecar, noon and civic.

Another good way to look at palindromes, which will help a lot with writing the pseudocode for this program, is to say that every character at index i is equal to the character at index palindrome's length - 1 - i. For 'racecar', for example, the character at index 1 ('a') is equal to the character at index 5 (index 7 - 1 - 1 = 5, also 'a').

Writing the Pseudocode

While the code generated by AI does work, it's always good practice to examine space-time complexity using Big-O notation. For all 3 of the suggested programs, the time complexity is O(n^2) and the space complexity is O(n) as you're storing an array of the reversed n characters with every run.

The following code runs in time complexity of O(n^2) and space complexity of O(1):

Function AllPalindromes(string[] strs)

   For i = 0 To Len(strs)

       l = 0

       r = Len(strs[i]) - 1

       

       While l < r

           if strs[i, l] != strs[i, r]

               // If (str[i, l] != str[i, len(str) - 1 - l]) then the string is

               // not a palindrome and we can return False.

               Return False

           

           // Move onto the next letter

           l++

           r++

       Endwhile

       

   Endfor

   

   Return True // If all strings were palindromes, return true.

end

View image Bqre
We greatly appreciate every question and answer you provide. Keep engaging and finding the best solutions. This community is the perfect place to learn and grow together. Find precise solutions at IDNLearn.com. Thank you for trusting us with your queries, and we hope to see you again.