From 2db3a67707506405e170d263ae093c15591ce9cf Mon Sep 17 00:00:00 2001 From: jjousun Date: Sat, 19 Aug 2017 23:32:20 -0700 Subject: [PATCH] Implemented methods --- README.md | 4 +- using_restricted_array.rb | 278 +++++++++++++++++++++++--------------- 2 files changed, 170 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 2e06ae3..fd8676a 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ Implement the methods in using_restricted_array.rb. Methods to implement are: - *reverse* - which reverses the values in the array in place - *search* - which looks for a given integer value in the array. Returns true if found, false otherwise. - *sort* - which sorts the values in the array -- *delete* - restricted arrays cannot be resized. So, we follow a convention. +- *delete* - restricted arrays cannot be resized. So, we follow a convention. Following the convention of changing the value to be deleted to be some special-value, implement the delete method. - *empty* - restricted array cannot be resized. So, we follow a convention. Convention: replace all values with 'SPECIAL_VALUE'. - *find_largest* - Finds and returns the largest value element in the array which is not 'SPECIAL_VALUE' - *insert_ascending* - 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. All subsequent elements will need to be moved forward by one index. + 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. diff --git a/using_restricted_array.rb b/using_restricted_array.rb index a26b4c5..db73541 100644 --- a/using_restricted_array.rb +++ b/using_restricted_array.rb @@ -8,28 +8,60 @@ ## 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 end # Reverses the values in the integer array def reverse(array, length) # Ruby - puts "NOT IMPLEMENTED" + i = 0 + j = length - 1 + + while i < j + + temp = array[i] + array[i] = array[j] + array[j] = temp + + 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" + (0..length-1).each do |i| + if array[i] == value_to_find + return true + end + end + return false end # Sorts the array in ascending order. def sort(array, length) - puts "NOT IMPLEMENTED" + (0..length-1).each do |i| + (i+1..length-1).each do |j| + if array[i] > array[j] + temp = array[i] + array[i] = array[j] + array[j] = temp + end + end + end end # Restricted arrays cannot be resized. So, we follow a convention. @@ -38,128 +70,154 @@ 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" + (0..length-1).each do |i| + if array[i] == value_to_delete + until i+1 == length + array[i] = array[i+1] + i += 1 + end + array[i] = SPECIAL_VALUE + end + end end +# Not sure if this is implemented corrected - i.e., this way removes the value_to_delete and adds SPECIAL_VALUE to end to keep the array the same size # Restricted array cannot be resized. So, we workaround by having a convention # 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 end # 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" + max = array[0] + (0..length-1).each do |i| + if max < array[i] && array[i] != SPECIAL_VALUE + max = array[i] + end + end + return max 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 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" -end - -## --- END OF METHODS --- - -# A restricted array could be constructed of a given size like so -size = 5 -my_integer_array = RestrictedArray.new(size) -my_integer_array_length = length(my_integer_array) -puts "The length of my integer array is #{my_integer_array_length}, which should be the same as #{size}." -puts "BUG!" if my_integer_array_length != size -puts - -# A restricted array could be constructed of a random size (1 to 20) like so -another_array = RestrictedArray.new() -another_array_length = length(another_array) -puts "The length of my random length, integer array is #{another_array_length}." -puts - -# 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 -print "Reversed array: " -print_array(another_array) -puts - -# search for value_to_find in the array -value_to_find = 120 -if search(another_array, another_array_length, value_to_find) - puts "#{value_to_find} found in the array!" -else - puts "#{value_to_find} not found in the array!" + if array[length-1] == SPECIAL_VALUE + (0..length-1).each do |i| + if array[i] > value_to_insert + temp = array[i] + array[i] = value_to_insert + value_to_insert = temp + end + end + end end -puts - -# search for value_to_find in the array - find the last value -value_to_find = another_array[another_array_length-1] -if search(another_array, another_array_length, value_to_find) - puts "#{value_to_find} found in the array!" -else - puts "#{value_to_find} not found in the array!" - puts "BUG! #{value_to_find} should be at index #{another_array_length-1}" -end -puts - -# print the largest value in the array -largest = find_largest(another_array, another_array_length) -puts "The largest value in the array is #{largest}" -puts - -# sort the array -sort(another_array, another_array_length) -print "Array sorted in ascending order: " -print_array(another_array) -puts - -# delete the first entry with the value_to_delete -value_to_delete = another_array[another_array_length/2] -delete(another_array, another_array_length, value_to_delete) -print "#{value_to_delete} deleted from array: " -print_array(another_array) -puts - -# delete the first entry with the value_to_delete -value_to_delete = another_array[another_array_length/2] -delete(another_array, another_array_length, value_to_delete) -print "#{value_to_delete} deleted from array: " -print_array(another_array) -puts - -# print the largest value in the array -largest = find_largest(another_array, another_array_length) -puts "The largest value in the array is #{largest}" -puts - -# sort the array -sort(another_array, another_array_length) -print "Array sorted in ascending order: " -print_array(another_array) -puts - -# insert 123 in to the array sorted in ascending order -value_to_insert = 123 -insert_ascending(another_array, another_array_length, value_to_insert) -print "#{value_to_insert} inserted into the array: " -print_array(another_array) -puts - -# empty array -empty(another_array, another_array_length) -print "Emptied array looks like: " -print_array(another_array) -puts -# insert 123 in to the array sorted in ascending order -value_to_insert = 123 -insert_ascending(another_array, another_array_length, value_to_insert) -print "#{value_to_insert} inserted into the array: " -print_array(another_array) -puts +# # ## --- END OF METHODS --- +# +# # A restricted array could be constructed of a given size like so +# size = 5 +# my_integer_array = RestrictedArray.new(size) +# my_integer_array_length = length(my_integer_array) +# puts "The length of my integer array is #{my_integer_array_length}, which should be the same as #{size}." +# puts "BUG!" if my_integer_array_length != size +# puts +# +# # A restricted array could be constructed of a random size (1 to 20) like so +# another_array = RestrictedArray.new() +# another_array_length = length(another_array) +# puts "The length of my random length, integer array is #{another_array_length}." +# puts +# +# # 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 +# print "Reversed array: " +# print_array(another_array) +# puts +# +# # search for value_to_find in the array +# value_to_find = 120 +# if search(another_array, another_array_length, value_to_find) +# puts "#{value_to_find} found in the array!" +# else +# puts "#{value_to_find} not found in the array!" +# end +# puts +# +# # search for value_to_find in the array - find the last value +# value_to_find = another_array[another_array_length-1] +# if search(another_array, another_array_length, value_to_find) +# puts "#{value_to_find} found in the array!" +# else +# puts "#{value_to_find} not found in the array!" +# puts "BUG! #{value_to_find} should be at index #{another_array_length-1}" +# end +# puts +# +# # print the largest value in the array +# largest = find_largest(another_array, another_array_length) +# puts "The largest value in the array is #{largest}" +# puts +# +# # sort the array +# sort(another_array, another_array_length) +# print "Array sorted in ascending order: " +# print_array(another_array) +# puts +# +# # delete the first entry with the value_to_delete +# value_to_delete = another_array[another_array_length/2] +# delete(another_array, another_array_length, value_to_delete) +# print "#{value_to_delete} deleted from array: " +# print_array(another_array) +# puts +# +# # delete the first entry with the value_to_delete +# value_to_delete = another_array[another_array_length/2] +# delete(another_array, another_array_length, value_to_delete) +# print "#{value_to_delete} deleted from array: " +# print_array(another_array) +# puts +# +# # print the largest value in the array +# largest = find_largest(another_array, another_array_length) +# puts "The largest value in the array is #{largest}" +# puts +# +# # sort the array +# sort(another_array, another_array_length) +# print "Array sorted in ascending order: " +# print_array(another_array) +# puts +# +# # insert 123 in to the array sorted in ascending order +# value_to_insert = 123 +# insert_ascending(another_array, another_array_length, value_to_insert) +# print "#{value_to_insert} inserted into the array: " +# print_array(another_array) +# puts +# +# # empty array +# empty(another_array, another_array_length) +# print "Emptied array looks like: " +# print_array(another_array) +# puts +# +# # insert 123 in to the array sorted in ascending order +# value_to_insert = 123 +# insert_ascending(another_array, another_array_length, value_to_insert) +# print "#{value_to_insert} inserted into the array: " +# print_array(another_array) +# puts