Question

In: Computer Science

1. A finite sequence of letters is called a palindrome if it reads the same forward...

1. A finite sequence of letters is called a palindrome if it reads the same forward or backward. Devise an algorithm for determining whether or not a string of n letters is a palindrome. Write your algorithm using the the same sort of pseudocode used in the text. Your algorithm should start with procedure palindrome (a1,a2,...,an: lowercase letters) Your procedure should return the value 0 if the string is a palindrome, and the first integer i such that ai 6=an−i+1 if the string is not a palindrome.

2. Show all steps taken by your algorithm if you input the string the string abcdba. What number does the procedure return?

3. Show all steps taken by your algorithm if you input the string the string azyza. What number does the procedure return?

Solutions

Expert Solution

a)

procedure palindrome(a1, a2, ..., an: lowercase letters)
Set j = n/2
Set i = 1
Repeat while i<=j and a[i]==a[n-i+1]
i = i + 1
[End of loop]
If i>j then
Return 0
Else
Return i


b) input string = abcdba
Here, n = 6
Therefore, j = n/2 = 3
i = 1
a[i] = a[1] = 'a' and a[n-i+1] = a[6] = 'a'
The conditions of the while loop i<=j and a[i]==a[n-i+1] both are satisfied.
Execute the body of the loop: i = i + 1 = 2
a[i] = a[2] = 'b' and a[n-i+1] = a[5] = 'b'
The conditions of the while loop i<=j and a[i]==a[n-i+1] both are satisfied.
Execute the body of the loop: i = i + 1 = 3
a[i] = a[3] = 'c' and a[n-i+1] = a[4] = 'd'
The condition of the while loop i<=j is satisfied, but another condition a[i]==a[n-i+1], which is not satisfied
Therefore, terminate the loop.
Now, check i>j condition, which is not satisfied, since i=3 and j=3
Finally return the value of i, that means returns 3


c) input string = azyza
Here, n = 5
Therefore, j = n/2 = 2
i = 1
a[i] = a[1] = 'a' and a[n-i+1] = a[5] = 'a'
The conditions of the while loop i<=j and a[i]==a[n-i+1] both are satisfied.
Execute the body of the loop: i = i + 1 = 2
a[i] = a[2] = 'z' and a[n-i+1] = a[4] = 'z'
The conditions of the while loop i<j and a[i]==a[n-i+1] both are satisfied.
Execute the body of the loop: i = i + 1 = 3
a[i] = a[3] = 'y' and a[n-i+1] = a[3] = 'y'
The condition of the while loop i<=j is not satisfied, therefore, terminate the loop.
Now, check i>j condition, which is satisfied, since i=3 and j=2, therefore, return 0

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Thank you.


Related Solutions

A palindrome is a string that reads the same forward and backward, i.e., the letters are...
A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.      Examples: radar à is a palindrome Able was I ere I saw Elba à is a palindrome good à not a palindrome Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character...
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
making a python code for this: A palindrome is a sequence that reads the same backwards...
making a python code for this: A palindrome is a sequence that reads the same backwards as forwards. Numbers can also be palindromes if we consider their digits as a sequence, for example 12121 and 8228 are palindromes. We can find palindromes from an initial seed number using the reverse and add method: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For example, ‘Eve’, ‘madam’, and 20502, are palindromes. Write a function called testPalindrome() that asks the user to input a string and returns if that string is a palindrome with the output as follows, without red: >>> Please enter a string: eve Your string "eve" is a palindrome. >>> testPalindrome() Please enter a string: end Your string "end" is not a palindrome. >>> testPalindrome() Please...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
Foundation of Computer Science A palindrome is a string that reads the same forward and backward....
Foundation of Computer Science A palindrome is a string that reads the same forward and backward. 1. Describe an algorithm that determines whether a string of n characters is a palindrome. 2. Write its corresponding program using your favorite programming language.
A palindrome is a string that reads the same forward and backward, for example, radar, toot,...
A palindrome is a string that reads the same forward and backward, for example, radar, toot, and madam. Your task is to construct a python algorithm to receive as input a string of characters and check whether it is a palindrome using a stack and a queue. Your ADTs contains the following methods: Queue Queue() – constructor enqueue(e) – enqueue an element dequeue(e) – dequeue an element len() – returns the number of elements in the queue Stack Stack() -...
In Java A palindrome is a word or sequence of characters which reads the same backward...
In Java A palindrome is a word or sequence of characters which reads the same backward and forward, such as madam, dad, racecar, 5885. In java Write a program that asks user to enter a word and prints if the word is a palindrome or not.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT