From 63ddf9e75100fe08dc22af75b13f6fc88d234cdb Mon Sep 17 00:00:00 2001 From: char <110567531+char-adadev@users.noreply.github.com> Date: Wed, 7 Sep 2022 11:31:04 -0700 Subject: [PATCH 1/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18e0461..6d970a3 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The following two linked lists do _not_ intersect at all because while the node `linked_list_b: 1 -> 2 -> 5 -> 4 -> 3` -Thereare no cycles anywhere in the linked list structures. Assume any intersection includes the tails of each list. +There are no cycles anywhere in the linked list structures. Assume any intersection includes the tails of each list. The linked lists must retain their original structure after the function returns. From 6d48a79a4103b963779ad103a8394004924b7d87 Mon Sep 17 00:00:00 2001 From: shelby Date: Tue, 13 Sep 2022 07:23:00 -0700 Subject: [PATCH 2/5] made a testcase for if the last value is the same to return the last value --- linked_lists/intersection.py | 19 +++++++++++++++++++ tests/test_intersection.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f07e2ae..f36bf5d 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -11,4 +11,23 @@ def intersection_node(headA, headB): """ Will return the node at which the two lists intersect. If the two linked lists have no intersection at all, return None. """ +''' create an empty variable to maintain the first seqential digit''' + ## first = None + ## while headA and headB: +'''check to see if current_a.value is equal to current_b.value:''' + ## while current current_a.value != current_b.value: + ## current_a = current_a.next + ## current_b = current_b.next + ## return first +'''set the empty variable to first same node value''' + ## if current_a.value == current_b.value: + ## first = current_a + ## while current_a.next.value == current_b.next.value: + ## current_a = current_a.next + ## current_b = current_b.next + ## > if current_a.value != current_b.value: + ## > return None + ## else return empty variable + + pass \ No newline at end of file diff --git a/tests/test_intersection.py b/tests/test_intersection.py index aa0937e..a773cd4 100644 --- a/tests/test_intersection.py +++ b/tests/test_intersection.py @@ -151,4 +151,32 @@ def test_will_return_none_nodes_have_same_value_different_reference(): answer = intersection_node(head_a, head_b) #Assert - assert answer is None \ No newline at end of file + assert answer is None + + + ## create a test case to chek if the end of the linked lists are the same value + '''Assume any intersection includes the tails of each list.''' + +def test_will_return_tail_if_tail_only_intersection(): + #arrange + node_d1 = Node("D") + node_q1 = Node("Q") + node_f1 = Node("F") + + node_c1 = Node("C") + node_s1 = Node("S") + node_f2 = Node("F") + #List A: [d, q, f1] + node_d1.next = node_q1 + node_q1.next = node_f1 + + #List B: [c, s, f2] + node_c1.next = node_s1 + node_s1.next = node_f2 + + head_a = node_d1 + head_b = node_c1 + #act + answer = intersection_node(head_a, head_b) + #assert + assert answer == node_f1 \ No newline at end of file From 8df28ae5f040b27687028a9b1f2d13433726640c Mon Sep 17 00:00:00 2001 From: Shelby Faulconer Date: Fri, 16 Sep 2022 09:05:31 -0700 Subject: [PATCH 3/5] fixed test, intersection points to same node. not same node value. intersection.py adjusted for the same reason (removed .value) --- linked_lists/intersection.py | 11 ++++++----- tests/test_intersection.py | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f36bf5d..f6a8b33 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -9,25 +9,26 @@ def __init__(self, value): def intersection_node(headA, headB): """ Will return the node at which the two lists intersect. + --> pointing TO THE SAME REFERENCE IN MEMORY If the two linked lists have no intersection at all, return None. """ ''' create an empty variable to maintain the first seqential digit''' ## first = None ## while headA and headB: '''check to see if current_a.value is equal to current_b.value:''' - ## while current current_a.value != current_b.value: + ## while current current_a != current_b: ## current_a = current_a.next ## current_b = current_b.next ## return first '''set the empty variable to first same node value''' - ## if current_a.value == current_b.value: + ## if current_a == current_b: ## first = current_a - ## while current_a.next.value == current_b.next.value: + ## while current_a.next == current_b.next: ## current_a = current_a.next ## current_b = current_b.next - ## > if current_a.value != current_b.value: + ## > if current_a != current_b: ## > return None - ## else return empty variable + ## else return first pass \ No newline at end of file diff --git a/tests/test_intersection.py b/tests/test_intersection.py index a773cd4..9bccc7f 100644 --- a/tests/test_intersection.py +++ b/tests/test_intersection.py @@ -165,14 +165,14 @@ def test_will_return_tail_if_tail_only_intersection(): node_c1 = Node("C") node_s1 = Node("S") - node_f2 = Node("F") + node_f1 = Node("F") #List A: [d, q, f1] node_d1.next = node_q1 node_q1.next = node_f1 - #List B: [c, s, f2] + #List B: [c, s, f1] node_c1.next = node_s1 - node_s1.next = node_f2 + node_s1.next = node_f1 head_a = node_d1 head_b = node_c1 From d2ff049cf31cf72e6459cc637d5abade5f0d6472 Mon Sep 17 00:00:00 2001 From: Shelby Faulconer Date: Wed, 21 Sep 2022 10:59:34 -0700 Subject: [PATCH 4/5] linting and working through tests-- need to create a function that works properly with links of different length --- linked_lists/intersection.py | 47 +++++++++++++++++++++--------------- tests/test_intersection.py | 2 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f6a8b33..1fc2aa1 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -1,6 +1,9 @@ +from email.errors import HeaderParseError + + class Node: def __init__(self, value): self.val = value @@ -12,23 +15,27 @@ def intersection_node(headA, headB): --> pointing TO THE SAME REFERENCE IN MEMORY If the two linked lists have no intersection at all, return None. """ -''' create an empty variable to maintain the first seqential digit''' - ## first = None - ## while headA and headB: -'''check to see if current_a.value is equal to current_b.value:''' - ## while current current_a != current_b: - ## current_a = current_a.next - ## current_b = current_b.next - ## return first -'''set the empty variable to first same node value''' - ## if current_a == current_b: - ## first = current_a - ## while current_a.next == current_b.next: - ## current_a = current_a.next - ## current_b = current_b.next - ## > if current_a != current_b: - ## > return None - ## else return first - - - pass \ No newline at end of file +# ''' create an empty variable to maintain the first seqential digit''' + first = None + current_a = headA + current_b = headB + + while headA and headB: +# '''check to see if current_a is equal to current_b:''' + while current_a != current_b: + current_a = current_a.next + current_b = current_b.next + ## if current_a is none or current_b is None + # reset pointer to head of opposite list + + # return first + # '''set the empty variable to first same node value''' + if current_a == current_b: + first = current_a + return first + + elif current_a != current_b: + return first + else: + return first + diff --git a/tests/test_intersection.py b/tests/test_intersection.py index 9bccc7f..97ed296 100644 --- a/tests/test_intersection.py +++ b/tests/test_intersection.py @@ -157,7 +157,7 @@ def test_will_return_none_nodes_have_same_value_different_reference(): ## create a test case to chek if the end of the linked lists are the same value '''Assume any intersection includes the tails of each list.''' -def test_will_return_tail_if_tail_only_intersection(): +def test_will_return_tail_if_tail_intersection(): #arrange node_d1 = Node("D") node_q1 = Node("Q") From d5f185a43474a0d902e28cff900e878c138f8a3e Mon Sep 17 00:00:00 2001 From: Shelby Faulconer Date: Wed, 21 Sep 2022 21:32:38 -0700 Subject: [PATCH 5/5] trying different things to see how to get the test for different length lists to pass --- linked_lists/intersection.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index 1fc2aa1..55b782c 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -22,12 +22,15 @@ def intersection_node(headA, headB): while headA and headB: # '''check to see if current_a is equal to current_b:''' + if current_a is None and current_b is not None: + current_a = headA + current_b = current_b.next + if current_b is None and current_a is not None: + current_b = headB + current_a = current_a.next while current_a != current_b: current_a = current_a.next current_b = current_b.next - ## if current_a is none or current_b is None - # reset pointer to head of opposite list - # return first # '''set the empty variable to first same node value''' if current_a == current_b: