From 16967c17b0f255a3f232a2a49fcb4e0ab82e375b Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 20:41:56 -0700 Subject: [PATCH 01/19] complete add_first, search and find_max methods --- lib/linked_list.rb | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..2ea09864 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,24 +18,43 @@ def initialize # method to add a new node with the specific data value in the linked list # insert the new node at the beginning of the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1)? + # Space Complexity: O(1)? def add_first(value) - raise NotImplementedError + if @head.nil? + @head = Node.new(value) + else + @head = Node.new(data, @head) + end end # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: o(n)? + # Space Complexity: O(n)? def search(value) - raise NotImplementedError + current = @head + + until current == nil + if current == value + return true + else + current = current.next + end + return false + end + end # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + max = nil + current = @head + until current == nil + current > max ? max = current : current = current.next + end + return max end # method to return the min value in the linked list From f54168e9ac85e81ed369f6cb142c76965731f9ad Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 21:07:33 -0700 Subject: [PATCH 02/19] refactor search and add first. write find_min, get_at_index, visit --- lib/linked_list.rb | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2ea09864..29344ae4 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -24,16 +24,17 @@ def add_first(value) if @head.nil? @head = Node.new(value) else - @head = Node.new(data, @head) + @head = Node.new(value, @head) end end # method to find if the linked list contains a node with specified value # returns true if found, false otherwise # Time Complexity: o(n)? - # Space Complexity: O(n)? + # Space Complexity: O(1)? def search(value) current = @head + return false if current = nil until current == nil if current == value @@ -59,10 +60,15 @@ def find_max # method to return the min value in the linked list # returns the data value and not the node - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n)? + # Space Complexity: O(1)? def find_min - raise NotImplementedError + min = nil + current = @head + until current == nil + current > min ? min = current : current = current.next + end + return min end @@ -70,7 +76,12 @@ def find_min # Time Complexity: ? # Space Complexity: ? def length - raise NotImplementedError + count = 0 + current = @head + until current == nil + count += 1 + current = current.next + end end # method that returns the value at a given index in the linked list @@ -79,14 +90,27 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + current = @head + index.times do + if current == nil + return nil + else + current = current.next + end + end + end # method to print all the values in the linked list # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + current = @head + + until current == nil + puts current + current = current.next + end end # method to delete the first node found with specified value From a36eb26393125215a2849c367d7b5380c80288c4 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 21:18:55 -0700 Subject: [PATCH 03/19] refactor get_at_index --- lib/linked_list.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 29344ae4..a72b1f88 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -34,7 +34,7 @@ def add_first(value) # Space Complexity: O(1)? def search(value) current = @head - return false if current = nil + return false if current == nil until current == nil if current == value @@ -42,9 +42,8 @@ def search(value) else current = current.next end - return false end - + return false end # method to return the max value in the linked list @@ -91,7 +90,8 @@ def length # Space Complexity: ? def get_at_index(index) current = @head - index.times do + count = indext + 1 + count.times do if current == nil return nil else From 125c12886bdec5952d603c9823dd9cbcec065c5f Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 21:27:50 -0700 Subject: [PATCH 04/19] fixed bugs in get_min --- lib/linked_list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index a72b1f88..a5c3001e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -65,7 +65,7 @@ def find_min min = nil current = @head until current == nil - current > min ? min = current : current = current.next + current < min ? min = current : current = current.next end return min end @@ -90,7 +90,7 @@ def length # Space Complexity: ? def get_at_index(index) current = @head - count = indext + 1 + count = index + 1 count.times do if current == nil return nil From 769ef270cb4c682e851277b18ab106589c532fd5 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 21:38:34 -0700 Subject: [PATCH 05/19] ... --- lib/linked_list.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index a5c3001e..82faf98b 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -52,7 +52,7 @@ def find_max max = nil current = @head until current == nil - current > max ? max = current : current = current.next + current > max? max = current : current = current.next end return max end @@ -65,7 +65,7 @@ def find_min min = nil current = @head until current == nil - current < min ? min = current : current = current.next + current < min? min = current : current = current.next end return min end @@ -90,6 +90,8 @@ def length # Space Complexity: ? def get_at_index(index) current = @head + return current if index == 0 + count = index + 1 count.times do if current == nil From 4319b49f0850f2945c10499f4e12f74c1ea2478c Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 21:42:04 -0700 Subject: [PATCH 06/19] ... --- lib/linked_list.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 82faf98b..009ff95c 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -50,9 +50,11 @@ def search(value) # returns the data value and not the node def find_max max = nil - current = @head + reutrn max if current = @head + + if @current == nil until current == nil - current > max? max = current : current = current.next + current > max ? max = current : current = current.next end return max end @@ -64,8 +66,10 @@ def find_max def find_min min = nil current = @head + + return min if current == nil until current == nil - current < min? min = current : current = current.next + current < min ? min = current : current = current.next end return min end From 7ad28b3bf38348ddfa73058fd599f3c68ac5e85c Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 21:43:11 -0700 Subject: [PATCH 07/19] ... --- lib/linked_list.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 009ff95c..fa6c00ce 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -52,7 +52,6 @@ def find_max max = nil reutrn max if current = @head - if @current == nil until current == nil current > max ? max = current : current = current.next end @@ -66,7 +65,6 @@ def find_max def find_min min = nil current = @head - return min if current == nil until current == nil current < min ? min = current : current = current.next From 47cb0ec984038e752e87de2405c5f9f11c06dbd2 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:02:58 -0700 Subject: [PATCH 08/19] ... --- lib/linked_list.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index fa6c00ce..dcf178e1 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -50,7 +50,7 @@ def search(value) # returns the data value and not the node def find_max max = nil - reutrn max if current = @head + return max if current = @head until current == nil current > max ? max = current : current = current.next @@ -92,6 +92,7 @@ def length # Space Complexity: ? def get_at_index(index) current = @head + return current if index == 0 count = index + 1 @@ -121,7 +122,7 @@ def visit # Time Complexity: ? # Space Complexity: ? def delete(value) - raise NotImplementedError + end # method to reverse the singly linked list @@ -129,7 +130,7 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse - raise NotImplementedError + end From 1485b352fe651a5b8941b93984f1cf23a3d46e90 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:04:37 -0700 Subject: [PATCH 09/19] ... --- lib/linked_list.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index dcf178e1..8bae5dae 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -83,6 +83,7 @@ def length count += 1 current = current.next end + return count end # method that returns the value at a given index in the linked list From 2183b494acb10a0d789a59e9c498eb2cc9b43e78 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:08:19 -0700 Subject: [PATCH 10/19] fixing bugs --- lib/linked_list.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8bae5dae..e6161eed 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,11 +21,9 @@ def initialize # Time Complexity: O(1)? # Space Complexity: O(1)? def add_first(value) - if @head.nil? - @head = Node.new(value) - else - @head = Node.new(value, @head) - end + + @head = Node.new(value, @head) + end # method to find if the linked list contains a node with specified value From 90429f42393e3d2b4e2f252939b69e178f275435 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:10:20 -0700 Subject: [PATCH 11/19] fixing bugs --- lib/linked_list.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e6161eed..e8df8392 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -48,7 +48,7 @@ def search(value) # returns the data value and not the node def find_max max = nil - return max if current = @head + return max if current = nil until current == nil current > max ? max = current : current = current.next @@ -63,7 +63,9 @@ def find_max def find_min min = nil current = @head + return min if current == nil + until current == nil current < min ? min = current : current = current.next end From 28ffc086f7030479d4ff383f28ee6c405d0b009c Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:19:29 -0700 Subject: [PATCH 12/19] fixing bugs --- lib/linked_list.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e8df8392..61e9de87 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -31,15 +31,12 @@ def add_first(value) # Time Complexity: o(n)? # Space Complexity: O(1)? def search(value) + return false if @head == nil current = @head - return false if current == nil until current == nil - if current == value - return true - else - current = current.next - end + return true if current == value + current = current.next end return false end @@ -47,9 +44,11 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node def find_max + return nil if @head = nil + + current = @head max = nil - return max if current = nil - + until current == nil current > max ? max = current : current = current.next end @@ -61,11 +60,11 @@ def find_max # Time Complexity: O(n)? # Space Complexity: O(1)? def find_min + return nil if @head == nil + min = nil current = @head - return min if current == nil - until current == nil current < min ? min = current : current = current.next end @@ -104,7 +103,7 @@ def get_at_index(index) current = current.next end end - + return current end # method to print all the values in the linked list From dcd89718fe5d25dd961b454f8729575324fd417c Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:25:36 -0700 Subject: [PATCH 13/19] fixing bugs --- lib/linked_list.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 61e9de87..79df22d3 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -52,7 +52,7 @@ def find_max until current == nil current > max ? max = current : current = current.next end - return max + return max.data end # method to return the min value in the linked list @@ -61,14 +61,13 @@ def find_max # Space Complexity: O(1)? def find_min return nil if @head == nil - min = nil current = @head until current == nil current < min ? min = current : current = current.next end - return min + return min.data end @@ -91,9 +90,9 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - current = @head + return @head if index == 0 - return current if index == 0 + current = @head count = index + 1 count.times do @@ -103,7 +102,7 @@ def get_at_index(index) current = current.next end end - return current + return current.data end # method to print all the values in the linked list From c717fd8bbec3942b8a710cce2c46a15b5b8e8197 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:28:43 -0700 Subject: [PATCH 14/19] fixing bugs --- lib/linked_list.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 79df22d3..8c96073f 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -44,7 +44,7 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node def find_max - return nil if @head = nil + return nil if @head == nil current = @head max = nil @@ -90,17 +90,11 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - return @head if index == 0 + return nil if @head.nil? current = @head - - count = index + 1 - count.times do - if current == nil - return nil - else - current = current.next - end + index.times do + current = current.next end return current.data end From 83fcfed8b4dc53cfdd18b284074f5a04d8977fd5 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:31:30 -0700 Subject: [PATCH 15/19] fixing bugs --- lib/linked_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8c96073f..e0446e2a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -35,7 +35,7 @@ def search(value) current = @head until current == nil - return true if current == value + return true if current.data == value current = current.next end return false From e0e120aac22897d5470f45ce8fab9ce2d8c35da1 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:33:22 -0700 Subject: [PATCH 16/19] fixing bugs --- lib/linked_list.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e0446e2a..ac920bec 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -46,8 +46,9 @@ def search(value) def find_max return nil if @head == nil - current = @head max = nil + current = @head + until current == nil current > max ? max = current : current = current.next From a2022a84ef3c5048d17537fce2f63dcf2b5e2647 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:38:49 -0700 Subject: [PATCH 17/19] fixing bugs --- lib/linked_list.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index ac920bec..15beb176 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -46,12 +46,11 @@ def search(value) def find_max return nil if @head == nil - max = nil + max = 1/.000000000001 current = @head - until current == nil - current > max ? max = current : current = current.next + current.data > max ? max = current : current = current.next end return max.data end @@ -62,11 +61,11 @@ def find_max # Space Complexity: O(1)? def find_min return nil if @head == nil - min = nil + min = -1/.000000000001 current = @head until current == nil - current < min ? min = current : current = current.next + current.data < min ? min = current : current = current.next end return min.data end @@ -107,7 +106,7 @@ def visit current = @head until current == nil - puts current + puts current.data current = current.next end end From d292664ee9ec61c7f094968b378ce4a563266a60 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:41:19 -0700 Subject: [PATCH 18/19] fixing bugs --- lib/linked_list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 15beb176..c928e45d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -46,7 +46,7 @@ def search(value) def find_max return nil if @head == nil - max = 1/.000000000001 + max = @head current = @head until current == nil @@ -61,7 +61,7 @@ def find_max # Space Complexity: O(1)? def find_min return nil if @head == nil - min = -1/.000000000001 + min = @head current = @head until current == nil From 6b158099c7d3b56f0bb59206c3e08cc2b4683288 Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 24 Aug 2020 22:42:43 -0700 Subject: [PATCH 19/19] fixing bugs --- lib/linked_list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index c928e45d..af415fd4 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -50,7 +50,7 @@ def find_max current = @head until current == nil - current.data > max ? max = current : current = current.next + current.data > max.data ? max = current : current = current.next end return max.data end @@ -65,7 +65,7 @@ def find_min current = @head until current == nil - current.data < min ? min = current : current = current.next + current.data < min.data ? min = current : current = current.next end return min.data end