Checkout
checkout
view
Your Cart Your Cart: item(s)
Add to Shopping Cart
$2.19 Instant Download
Computer Science, Data Structures and Algorithms
Other

Algorithm


Quest:
In English like pseudo-code, or structured English -- just to make sure everyone can read it; write an algorithm to determine if a string is a palindrome.  A palindrome is a word or phrase that is spelled the same whether you are reading it forwards or backwards (ex. race car, Madam I’m Adam). Your algorithm should ignore spaces and punctuation. Make sure to clearly state your assumptions before presenting your answer and to justify your use of data structure: what data structure did you choose (from lists, stacks, queues or trees) and why, and briefly explain why you did not choose the others
(CAN YOU JUST CHECK IF THE BELOW GIVEN 2 ALGORITHMS ARE CORRECT OR IS THERE SOME CHANGE THAT CAN BE MADE AND EXPLAIN WHY)


Sol 01
Procedure determinePalindrome ( st as string)
{
   /* ignoring spaces and punctuation */
   Declare a list variable to store the spaces and punctuation ignored string.  List[ ]
   len= length of st
   Dynamically make the list variable size as length of the string
   Declare i as integer and j as integer
   Initialize k value as 1
   Initialize j value as 1
   Loop
      {
          /* get j th  position character from the st . i.e if j=1  1st character, j=2 2nd character and so on*/
          
          If character is not a space and character not is not a punctuation then
              {
                  Add the character to the list
List[k] = character
Increment k by 1
              }
              Increment j by 1
      } continue until j < = len

     /* determining string is a palindrome or not */
    
    /* here length of the new string is k -1 */
    Declare variable x as integer
    Initialize x with floor(k/2)   /* floor is rounding the fractions down*/
    Assign j value as 1
    Decrement k value by 1   /* now this the length of the new string*/
    Loop
    {
      /*Get the j th postion character and k th  character from the list*/
      If list[x] <> list[k] then
         Display as the given string is not a palindrome
         Exit procedure
      End if
      Increment j by 1
      Decrement k by 1
    } continue until j <=x

   Display as Given String is a palindrome.

}

Sol 2
//In this procedure '=' refers to the assignment statement and '==' refers to the equal statement
//The array starts with '0' as the first pointer instead of '1'
procedure checkPalindrome (string inputString)

if the length of inputString is less than 2 characters
then
        return error "String too short"
end if

int i, j, k
char stringArray[length of inputString]

i = 0
for (i < length of inputString)
do
        {stringArray[i] = character i of inputString
        i = i + 1
end for

i = 0
j = length of stringArray - 1
k = 0

while (j - i >= 0) and (i <> j) and (k == 0)
do
        if (stringArray[i] is a space or punctutation)
        then {i = i + 1
               }
        else {
               if (stringArray[j] is a space or punctuation)
               then {j = j - 1
                      }
               else {
                      if (stringArray[i] == stringArray[j])
                      then {
                             i = i + 1
                             j = j + 1
                             }
                      else { k == 1}
                      end if
               end if
        endif
end do

if (k == 0)
then {report inputString is a palindrome
        }
else {report inputString is not a palindrome
        }
end if

end procedure

Attachments
Sol 01.doc  View File

By OTA:  Shawn Laliberte

OTA Rating:  4.8/5

Your Price:  $2.19  (original value ~$3.99)

What's included:

  • Plain text response
  • Attachment(s):
    • 94175sol.doc
$2.19 Download Add to Cart

Add to Shopping Cart
$2.19 Instant Download
Parallels between a traditional file index and the file directory system? - What are the parallels between a traditional file index and the file directory system maintained by an operating system. In what ways does an operating system's file directory differ from a traditional index?
Database management system - Q1. State advantages of separating the database management system from application software Q2. Explain one advantage that a. A sequential file has over an indexed file b. A sequential file has over a hash file c. An indexed file has over a sequential file d. An indexed file has over a hash file e. A hash file has over a sequential file f. A hash file ...
Artificial Intelligence - Brains & Computers If the brain is a computer and the mind its workings, is this a fitting analogy of the computer and its software? What would happen if we had dedicated computers with a huge number of neuron circuits? Would intelligence develop? Would we be able to understand it
How many cells can be in a computer's main memory if each cell's address can be represented by two hexadecimal digits? What if four hexadecimal digits are used? Explain your answer. - How many cells can be in a computer's main memory if each cell's address can be represented by two hexadecimal digits? What if four hexadecimal digits are used? Explain your answer.
Data Structure - Does a queue crawl through memory in the direction of its head or its tail? Explain your answer.

Page generated in 0.0732 seconds

About Us ·  Contact Us ·  Samples ·  Solutions ·  Legal Terms and Conditions ·  Privacy Policy

©2008 SolutionLibrary.com

Search for Solutions About Us Samples