| | |
Argorithm: palindromes, write recursive function.
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Solved Threads: 0
Consider the language in the alphabet {a,b} defined by the grammar.
<S> = a<S>a | b<S>b | a | b
Question: List all the 5-character strings that are in this language.
My answer is there are eight 5-character strings in this language and they are
ababa
abbba
aaaaa
aabaa
baaab
babab
bbabb
bbbbb
.....is this right??
<S> = a<S>a
<S> = b<S>b
<S> = a
<S> = b
so therefore,
<S> = a<S>a = aa<S>aa = aaaaa or aabaa
<S> = a<S>a = ab<S>ba = ababa or abbba
<S> = b<S>b = ba<S>ab = baaab or babab
<S> = b<S>b = bb<S>bb = bbabb or bbbbb
my next questions is to write a recursive function that, when passed a character array S and integers First and Last, determines whether S[First...Last] is in this language?? any help plz.
<S> = a<S>a | b<S>b | a | b
Question: List all the 5-character strings that are in this language.
My answer is there are eight 5-character strings in this language and they are
ababa
abbba
aaaaa
aabaa
baaab
babab
bbabb
bbbbb
.....is this right??
<S> = a<S>a
<S> = b<S>b
<S> = a
<S> = b
so therefore,
<S> = a<S>a = aa<S>aa = aaaaa or aabaa
<S> = a<S>a = ab<S>ba = ababa or abbba
<S> = b<S>b = ba<S>ab = baaab or babab
<S> = b<S>b = bb<S>bb = bbabb or bbbbb
my next questions is to write a recursive function that, when passed a character array S and integers First and Last, determines whether S[First...Last] is in this language?? any help plz.
•
•
Join Date: Jul 2005
Posts: 60
Reputation:
Solved Threads: 4
Your answer for the first question is right.
The second question for a recursive function is pretty easy, assuming it only is supposed to handle odd length arrays of the language that grammar implements. Seems to be a simple recursive function that checks through a heirarchy of ifs.
Pseudocode:
I r teh l33t w/ Recursion
The second question for a recursive function is pretty easy, assuming it only is supposed to handle odd length arrays of the language that grammar implements. Seems to be a simple recursive function that checks through a heirarchy of ifs.
Pseudocode:
palindrome (s[], first, last)
if s[first] == s[last] and s[first] == 'a' or 'b'
if first == last
This string is in the language
else
So far this string is in the language
palindrome (s[], first+1, last-1)
else
This string is not in the languageI r teh l33t w/ Recursion
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
i just want a simple explanation on how to write paliandrome algorithm in psedu code.
boolean isPalindrome(string s){
char[] letters = s.toCharArray() //convert string to array of characters
int firstCnt = 0 //counter for left side
int secCnt = letters.length - 1 //counter for right side
while (firstCnt < secCnt){ //work toward middle
if (letters[firstCnt] != letters[secCnt]){
return false //if left and right does not match, is not a palindrome
}
}
return true //if loop completes without a false, is a palinrome
} Last edited by Tekmaven; May 30th, 2009 at 3:15 am. Reason: Code Tags
![]() |
Similar Threads
- recursive function (C)
- Recursive function - checking for palindromes (C)
- recursive function (C++)
- recursive findaverage function for Binary search tree (C)
Other Threads in the Computer Science Forum
- Previous Thread: Seeking IT advice
- Next Thread: Getting Desktop Display RGB Values and Keystroke Code
Views: 10198 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Computer Science
ai algorithm algorithms amazon assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bizarre bletchleypark bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware connect conversion csc data dataanalysis dataintepretation development dissertations dissertationthesis ebook employment energy extensions floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez graphics hardware history homeowners homework homeworkassignment homeworkhelp humor ibm idea ideas internet ipod itcontracts jobs kindle laser lazy lsmeans mainframes marketing mining msaccess nano networking news os p2p parser piracy piratebay principles programming science security sex simulation software spoonfeeding spying sql sql-server student study supercomputer supercomputing sweden syntactic technology textfield tree turing turingtest two'scompliment uk virus warehouse





