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

Janice Lichtman - Restricted Array assignment #2

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
13 changes: 13 additions & 0 deletions restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
103 changes: 93 additions & 10 deletions using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]<array[i]
temp=array[i]
array[i]=array[j]
array[j]=temp
end
end
end
end


# Restricted arrays cannot be resized. So, we follow a convention.
# Convention: change the value to be deleted with 'SPECIAL_VALUE'
# Deletes 'value_to_delete' if found in the array. To keep the array size
# 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"
# Deletes FIRST occurance only of value_to_delete.
(0...length).each do |i|
if array[i] == value_to_delete
(i...length-1).each do |j|
array[j] = array[j+1]
end
array[length-1] = SPECIAL_VALUE
return
end
end
end

# 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"
# special case if array has no values
return -1 if length == 0
largest = array[0]
(0...length).each 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
# 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
Expand All @@ -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
Expand Down