From 57c43d03dd0a0adee89ffe778e0e7466ed387f49 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 19:46:18 -0700 Subject: [PATCH 01/11] length method working as expected --- using_restricted_array.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 72a27ca..413ffb0 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -8,7 +8,13 @@ ## Calculates the length of the restricted integer array_size def length(array) - puts "NOT IMPLEMENTED" + i = 0 + + while array[i] != nil + i += 1 + end + + return i end # Prints each integer values in the array From 88e34269413fe309d97a20e9c1944396d352f8e0 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 19:52:46 -0700 Subject: [PATCH 02/11] printing out values in array --- using_restricted_array.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 413ffb0..daf64cb 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -14,12 +14,18 @@ def length(array) i += 1 end - return i + return i end # Prints each integer values in the array def print_array(array) - puts "NOT IMPLEMENTED" + i = 0 + + while array[i] != nil + print "#{array[i]} " + i += 1 + end + puts "\n" end # Reverses the values in the integer array From 0c9c29330950d079257c9c90c018fc6774e13333 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 19:59:01 -0700 Subject: [PATCH 03/11] reverse method implemented --- using_restricted_array.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index daf64cb..37be17b 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -25,12 +25,23 @@ def print_array(array) print "#{array[i]} " i += 1 end + puts "\n" end # Reverses the values in the integer array def reverse(array, length) # Ruby - puts "NOT IMPLEMENTED" + i = 0 + j = length-1 + + while i < j + saved_j = array[j] + array[j] = array[i] + array[i] = saved_j + + i += 1 + j -= 1 + end end # For an unsorted array, searches for 'value_to_find'. From 9a35ad2cf0da5728f7354d417fd8b66ef4ff02df Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:03:47 -0700 Subject: [PATCH 04/11] search method implemented --- using_restricted_array.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 37be17b..8d9b3f6 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -32,7 +32,7 @@ def print_array(array) # Reverses the values in the integer array def reverse(array, length) # Ruby i = 0 - j = length-1 + j = length - 1 while i < j saved_j = array[j] @@ -47,7 +47,17 @@ def reverse(array, length) # Ruby # For an unsorted array, searches for 'value_to_find'. # Returns true if found, false otherwise. def search(array, length, value_to_find) - puts "NOT IMPLEMENTED" + i = 0 + + while i < length - 1 + if array[i] == value_to_find + return true + end + + i += 1 + end + + return false end # Sorts the array in ascending order. From abab085c0da6a30f3108afd1e496b6d0cff19eb8 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:05:37 -0700 Subject: [PATCH 05/11] Better search implemented --- using_restricted_array.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 8d9b3f6..7fcc64c 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -47,14 +47,10 @@ def reverse(array, length) # Ruby # For an unsorted array, searches for 'value_to_find'. # Returns true if found, false otherwise. def search(array, length, value_to_find) - i = 0 - - while i < length - 1 + length.times do |i| if array[i] == value_to_find return true end - - i += 1 end return false From 5489994170ceff1f38e13065dc5fed76b6c512c5 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:16:27 -0700 Subject: [PATCH 06/11] Implemented sort method --- using_restricted_array.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 7fcc64c..b4e5b0d 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -58,7 +58,24 @@ def search(array, length, value_to_find) # Sorts the array in ascending order. def sort(array, length) - puts "NOT IMPLEMENTED" + length.times do |i| + min_index = i + pointer_index = i + 1 + + while pointer_index < length - 1 + if array[pointer_index] < array[min_index] + min_index = pointer_index + end + + pointer_index += 1 + end + + if min_index != i + new_min = array[min_index] + array[min_index] = array[i] + array[i] = new_min + end + end end # Restricted arrays cannot be resized. So, we follow a convention. From f4f51fb0d390033de69356bb5549634e1c25cf7e Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:19:11 -0700 Subject: [PATCH 07/11] Implemented empty method --- using_restricted_array.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index b4e5b0d..54714dc 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -92,6 +92,9 @@ def delete(array, length, value_to_delete) # Empties the restricted array by making all values = SPECIAL_VALUE def empty(array, length) puts "NOT IMPLEMENTED" + length.times do |i| + array[i] = SPECIAL_VALUE + end end # Finds and returns the largest value element in the array which is not 'SPECIAL_VALUE' From 861f57faa08710dfb062b5882c5bed248978dcd7 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:24:55 -0700 Subject: [PATCH 08/11] Implemented find_largest method --- using_restricted_array.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 54714dc..11e9d16 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -91,7 +91,6 @@ def delete(array, length, value_to_delete) # Convention: replace all values with 'SPECIAL_VALUE' # Empties the restricted array by making all values = SPECIAL_VALUE def empty(array, length) - puts "NOT IMPLEMENTED" length.times do |i| array[i] = SPECIAL_VALUE end @@ -100,7 +99,15 @@ def empty(array, length) # Finds and returns the largest value element in the array which is not 'SPECIAL_VALUE' # Assumes that the array is not sorted. def find_largest(array, length) - puts "NOT IMPLEMENTED" + largest = array[0] + + length.times do |i| + if array[i] > largest && array[i] != SPECIAL_VALUE + largest = array[i] + end + end + + return largest end # Insert value to insert at the correct index into the array assuming the array From 90ef4aeea7012157bbb041370f01fc1d3a0435a4 Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:29:59 -0700 Subject: [PATCH 09/11] Fixed bug in sort method. --- using_restricted_array.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 11e9d16..7850288 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -62,7 +62,7 @@ def sort(array, length) min_index = i pointer_index = i + 1 - while pointer_index < length - 1 + while pointer_index < length if array[pointer_index] < array[min_index] min_index = pointer_index end @@ -116,7 +116,7 @@ def find_largest(array, length) # (Hint: if there are no elements with 'SPECIAL_VALUE', there is no room to insert) # All subsequent elements will need to be moved forward by one index. def insert_ascending(array, length, value_to_insert) - puts "NOT IMPLEMENTED" + puts "insert ascending NOT IMPLEMENTED" end ## --- END OF METHODS --- From c1d56fe3f36f7b3f7677c995340407b5c7d527fd Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:37:42 -0700 Subject: [PATCH 10/11] implemented delete method --- using_restricted_array.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index 7850288..d5fea12 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -84,7 +84,16 @@ def sort(array, length) # constant, adds an element with 'SPECIAL_VALUE' in the end. Assumes the array # to be sorted in ascending order. def delete(array, length, value_to_delete) - puts "NOT IMPLEMENTED" + length.times do |i| + if array[i] == value_to_delete + while i + 1 < length && array[i] != SPECIAL_VALUE + array[i] = array[i + 1] + i += 1 + end + array[i] = SPECIAL_VALUE + return array + end + end end # Restricted array cannot be resized. So, we workaround by having a convention From 35d0e25471625cae3ba897af8cffb60b3049123d Mon Sep 17 00:00:00 2001 From: Kelsey McAlpine Date: Wed, 16 Aug 2017 20:44:21 -0700 Subject: [PATCH 11/11] Still need to work on understanding insert_ascending --- using_restricted_array.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/using_restricted_array.rb b/using_restricted_array.rb index d5fea12..bb52fca 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -124,8 +124,29 @@ def find_largest(array, length) # Restricted arrays cannot be resized. Insert only if there is space in the array. # (Hint: if there are no elements with 'SPECIAL_VALUE', there is no room to insert) # All subsequent elements will need to be moved forward by one index. + +# TBH I struggled with this one a bit - found solution in github repo.. def insert_ascending(array, length, value_to_insert) - puts "insert ascending NOT IMPLEMENTED" + if array[length - 1] != SPECIAL_VALUE + return "No room :(" + end + + insert_index_found = false + length.times do |i| + if insert_index_found == false && array[i] > value_to_insert + insert_index_found = true + end + + if insert_index_found == true + temp = array[i] + array[i] = value_to_insert + value_to_insert = temp + end + + if value_to_insert == SPECIAL_VALUE + break + end + end end ## --- END OF METHODS ---