KayS Posted July 16, 2014 Posted July 16, 2014 I have a programming pseudo code question regarding palindrome theory, meaning a program that checks if the string is the same fowrds to backwrds and backwrds to forwrds. I chose A as my answer because If i use the string "dad" in the array it would return true, the other doe the checking but some results false any suggestions?
Sensei Posted July 16, 2014 Posted July 16, 2014 I have no idea in what language it's written but A answer is almost certainly wrong. J is always constant in whole inter loop.. Doing floor( value ) has no sense for integer also. It should be used only for float argument.
LaurieAG Posted July 17, 2014 Posted July 17, 2014 A only tests all elements against the last element, B and C test the first element against the second last element while D tests all the elements correctly.
KayS Posted July 17, 2014 Author Posted July 17, 2014 I get it now sorry it was my fault the index actuall starts at 1 and not 0 I just realised it..but what confuses me is that B and C test against the second last element which is a good test but return false :S for example lets say the String in the array is [ m a d a m ] <-- testing the second element "a" with second last "a" would suggest its the correct value.. but then again D seems more reasonable now as "laurieAG" suggested it checks all elements and doesnt rely on comparing the second last. So I think D is the correct answer no
LaurieAG Posted July 17, 2014 Posted July 17, 2014 For the first element both B and C test arr(i) or arr(index), where i or index = 1 (i.e. element 'm'), against len(arr) -1 (i.e. 5-1 = 4 or element 'a'), which is the second last element not the last element.
KayS Posted July 17, 2014 Author Posted July 17, 2014 Am i right to think that B and C are also wrong because they never increment the counter so it would be an endless loop checking the same condition whereas D increments its counter meaning the test changes each time?
LaurieAG Posted July 18, 2014 Posted July 18, 2014 The main problem with B and C is that they do not compare the first element with the last element and B also does not increment the counter. A is the option where all elements are compared with the last element. The FOR and WHILE loops operate differently as the FOR has the counter increment embedded. If you step through each option for each increment and write down what each of the variables are for each of the cycles you will be able to see the differences. KayS, please post your results for 3 cycles of B, C and D. A i j arr(i) arr(j) 1 5 m m 2 5 a m 3 5 d m
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now