From 6bc3f16b079e241b529136c1a01fa55ae50b11c9 Mon Sep 17 00:00:00 2001 From: Janice Lichtman Date: Tue, 15 Aug 2017 23:07:03 -0700 Subject: [PATCH] completed the exercises --- restricted_array.rb | 13 +++++ using_restricted_array.rb | 103 ++++++++++++++++++++++++++++++++++---- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/restricted_array.rb b/restricted_array.rb index b3b3ddc..aa9894a 100644 --- a/restricted_array.rb +++ b/restricted_array.rb @@ -33,4 +33,17 @@ def []=(key, value) @internal_array[key] = value end end + end + +# # Some intial exploration +# arr1 = RestrictedArray.new(4) +# +# (0..3).each do |n| +# puts arr1[n] +# end +# puts +# puts arr1[2] +# arr1[2]=6 +# +# puts arr1[2] diff --git a/using_restricted_array.rb b/using_restricted_array.rb index a26b4c5..92d8de0 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -8,61 +8,143 @@ ## 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 def print_array(array) - puts "NOT IMPLEMENTED" + i = 0 + while array[i] != nil + print "#{array[i]} " + i += 1 + end + puts end # Reverses the values in the integer array def reverse(array, length) # Ruby - puts "NOT IMPLEMENTED" + return if length < 2 + + i = 0 + j = length - 1 + while i < j + # Do the swap + + # one method + # temp = array[i] + # array[i] = array[j] + # array[j] = temp + + # OR since array values are small (<222) and positive + array[i] = array[i] + array[j] + array[j] = array[i] - array[j] + array[i] = array[i] - array[j] + + i += 1 + j -= 1 + end end # 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 array[i] != nil + return true if array[i] == value_to_find + i += 1 + end + return false end # Sorts the array in ascending order. def sort(array, length) - puts "NOT IMPLEMENTED" + + #Similar to selection sort, but may do more swaps + (0...length).each do |i| + ((i+1)...length).each do |j| + if array[j] 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 # is sorted in ascending manner. # Restricted arrays cannot be resized. Insert only if there is space in the array. -# (Hint: if there are elements with 'SPECIAL_VALUE', there is no room to insert) +# (Hint: if there are elements with 'SPECIAL_VALUE', there is no room to insert) ---> THIS WAS A TYPO. SHOULD BE: 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" + return if array[length-1] != SPECIAL_VALUE + + desired_index_found = false + (0...length).each do |i| + if desired_index_found == false && array[i] > value_to_insert + desired_index_found = true + end + + if desired_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 --- # A restricted array could be constructed of a given size like so @@ -82,6 +164,7 @@ def insert_ascending(array, length, value_to_insert) # print the current array print "Printing values in the array: " print_array(another_array) + # reverse the values in the current array reverse(another_array, another_array_length) # prints the reversed array