Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jou-Jou's Restricted Arrays #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
278 changes: 168 additions & 110 deletions using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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