samelist(L1,L2) {
if(L1==[] and L2==[]) return true;
else return (head(L1)==head(L2) and samelist(tail(L1), tail(L2)));
}
Problem is with my recursive pseudo code
Page 1 of 11 Replies - 841 Views - Last Post: 05 February 2012 - 04:54 PM
#1
Problem is with my recursive pseudo code
Posted 05 February 2012 - 03:49 PM
Ok I have written a program samelist(List1, List2). IT is supposed to check two lists for equality. However whenever I try to implement it in Java it doesn't work. A friend told me it is with the pseudo code I have written shown below. There's a mistake there but I cannot see it. Sorry again to be cheeky but if you find a way to correct can you provide an example of how it would work? (please please please don't answer the question in Java)
Replies To: Problem is with my recursive pseudo code
#2
Re: Problem is with my recursive pseudo code
Posted 05 February 2012 - 04:54 PM
Something is wrong with your pseudo code.
I'm now sure what tail() returns but something doesn't feel right with
samelist(tail(L1), tail(L2))
The way I look at this problem is that we just simply want to check if each element in
the list is the same.
Pseudo code:
L1 = {1, 2, 3, 4}
L2 = {1, 2, 3, 4}
sameList(L1, L2, 0)
boolean sameList(L1, L2, index) {
// note NOT check for indexing exceptions
return (L1[index] == L2[index] && sameList(L1, L2, index + 1)
}
}
I'm now sure what tail() returns but something doesn't feel right with
samelist(tail(L1), tail(L2))
The way I look at this problem is that we just simply want to check if each element in
the list is the same.
Pseudo code:
L1 = {1, 2, 3, 4}
L2 = {1, 2, 3, 4}
sameList(L1, L2, 0)
boolean sameList(L1, L2, index) {
// note NOT check for indexing exceptions
return (L1[index] == L2[index] && sameList(L1, L2, index + 1)
}
}
Page 1 of 1

New Topic/Question
Reply


MultiQuote


|