The problem is:
Intersection: Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that the intersection is defined based on reference, not value.
That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, then they are intersecting. Below is an example:
enter image description here The code I wrote is as following, may I know any improvement can be done?
def intersecton[A](alist: List[A], blist:List[A]): Option[A]= (alist.last, blist.last)match{
case (a,b) if (a!=b) => None
case (_,_) if (alist.size>1 && blist.size>1 && alist(alist.size-2) != blist(blist.size-2)) => Some(alist.last)
case (a,b) if (alist.size ==1||blist.size ==1)&& a==b => Some(a)
case (_,_) => intersecton(alist.dropRight(1), blist.dropRight(1))
}
Below is the test case I already run:
val list1: List[Int]=List(5,6, 1, 1, 4)
val list2: List[Int]=List(6, 1, 1,4)
>>6
val list1: List[Int]=List(3,2,4)
val list2: List[Int]=List(5,6,7)
>>None
val list1: List[Int]=List(4,6,7)
val list2: List[Int]=List(5,6,7)
>>6
2 Answers 2
intersecton(List(1, 2, 4), List(4, 5, 6))
incorrectly returns None
.
You've asked several questions recently, and I keep seeing similar issues:
- Using
list.last
: You have been advised to avoid traversing to the end of the list, for efficiency. Lists should be manipulated at the head. - Functional thinking: These solutions involve too many tedious cases, and the code could be simplified by defining helper functions, or even restated entirely in terms of other functions.
- Spelling: What's an "intersecton"? (You previously misspelled "palindrome", twice.)
- Inadequate testing: The counterexample cited above just happened to be the first test I tried, and it failed. This isn't the first time I've seen an obviously wrong result. Your first question was closed due to broken code, and it still hasn't been fixed.
You are welcome to use Code Review as a learning resource, but it would be a good idea to show some improvement between questions.
-
\$\begingroup\$ intersecton(List(1, 2, 4), List(4, 5, 6)) incorrectly returns Non? Then, what do you think it should return? \$\endgroup\$sweetyBaby– sweetyBaby2016年12月04日 10:41:47 +00:00Commented Dec 4, 2016 at 10:41
-
\$\begingroup\$ @sweetyBaby it should return 4. because the 2nd index in list one is reference-equal to the 0th index in list two. I have elaborated a bit in a comment on your question \$\endgroup\$Vogel612– Vogel6122016年12月04日 11:37:52 +00:00Commented Dec 4, 2016 at 11:37
-
\$\begingroup\$ @Vogel612, nope, maybe I did not describe very clearly, it should be none , I edited the problem description, thanks \$\endgroup\$sweetyBaby– sweetyBaby2016年12月04日 11:40:37 +00:00Commented Dec 4, 2016 at 11:40
-
\$\begingroup\$ Even with your added explanation, the answer should still be 4. The lists still intersect, and the question doesn't say that degenerate intersections should be prohibited. \$\endgroup\$200_success– 200_success2016年12月04日 17:42:16 +00:00Commented Dec 4, 2016 at 17:42
-
\$\begingroup\$ @200_success I thought you misunderstand the problem, Note that the intersection is defined based on reference, not value, it is the reference not just the value, if it should be 4, then following part should be the same, thanks \$\endgroup\$sweetyBaby– sweetyBaby2016年12月05日 06:27:10 +00:00Commented Dec 5, 2016 at 6:27
Here is my updated code for this problem:
def intersection[A](alist: List[A], blist:List[A]): List[A]= {
alist.intersect(blist)
}
intersecton
"? \$\endgroup\$