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

Restricted-Array Hyunji Kim #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
146 changes: 137 additions & 9 deletions using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,163 @@

## Calculates the length of the restricted integer array_size
def length(array)
puts "NOT IMPLEMENTED"
i = 0
until 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
until i == length(array)
print array[i].to_s + " "
i += 1
end
puts
end

# Reverses the values in the integer array
def reverse(array, length) # Ruby
puts "NOT IMPLEMENTED"
return array if length == 0 || length == 1
i = 0
j = length - 1
until i >= j
temp = array[i]
array[i] = array[j]
array[j] = temp
i += 1
j -= 1
end
return array
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"
return false if length == 0
i = 0
until i == length
if array[i] == value_to_find
return true
end
i += 1
end
return false
end

# Sorts the array in ascending order.

def sort(array, length)
puts "NOT IMPLEMENTED"
return array if length == 0 || length == 1
i = 0
k = length-1
until i == length
j = 0
until j == k
temp = array[j]
if array[j + 1] < temp
array[j] = array[j + 1]
array[j + 1] = temp
end
j += 1
end
k -= 1
i += 1
end
end


#=================Mergesort ======================#
# def sort(array, length) # mergesort!
# if length <= 1
# return array
# end
# array_a = []
# array_b = []
# n = length/2
#
# n.times do |i|
# array_a << array[i]
# end
# # print "array_a: #{array_a}"
# (length-n).times do
# array_b << array[n]
# n += 1
# end
# # puts "array_b: #{array_b}"
#
# return merge(array, sort(array_a, array_a.length), sort(array_b, array_b.length))
# end
#
# def merge(array, array1, array2)
#
# n = array1.length + array2.length
#
# n.times do |i|
# if array1[0] == nil
# array[i] = array2[0]
# array2.shift
# elsif array2[0] == nil
# array[i] = array1[0]
# array1.shift
# elsif array1[0] <= array2[0]
# array[i] = array1[0]
# array1.shift
# else
# array[i] = array2[0]
# array2.shift
# end
# end
#
# return array
#
# 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"
return array if length == 0
i = 0
until i == length
if array[i] == value_to_delete
array[i] = SPECIAL_VALUE
end
i += 1
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"
return array if length == 0
i = 0
until i == length
array[i] = SPECIAL_VALUE
i += 1
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"
return nil if length == 0
return array[0] if length == 1
i = 0
max = 0
until i == length
if array[i] > max && array[i] != SPECIAL_VALUE
max = array[i]
end
i += 1
end
return max
end

# Insert value to insert at the correct index into the array assuming the array
Expand All @@ -60,7 +173,22 @@ def find_largest(array, length)
# (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.
def insert_ascending(array, length, value_to_insert)
puts "NOT IMPLEMENTED"
if array[length-1] != SPECIAL_VALUE || length == 0
puts "no room to insert"
return
end

i = 0
until array[i] == SPECIAL_VALUE
i += 1
end

if array[i] == SPECIAL_VALUE
array[i] = value_to_insert
end

sort(array, length)
return array
end

## --- END OF METHODS ---
Expand Down