-
Notifications
You must be signed in to change notification settings - Fork 30
Sai's restricted array hw #22
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,28 +8,92 @@ | |
|
|
||
| ## Calculates the length of the restricted integer array_size | ||
| def length(array) | ||
| puts "NOT IMPLEMENTED" | ||
| count = 0 | ||
|
|
||
| until array[count] == nil do | ||
| count += 1 | ||
| end | ||
|
|
||
| return count | ||
| # puts "NOT IMPLEMENTED" | ||
| end | ||
|
|
||
| # Prints each integer values in the array | ||
| def print_array(array) | ||
| puts "NOT IMPLEMENTED" | ||
| i = 0 | ||
| until array[i] == nil do | ||
| # only print spaces in array that are used | ||
| if array[i] != SPECIAL_VALUE | ||
| puts array[i] | ||
| end | ||
| i += 1 | ||
| end | ||
| # puts "NOT IMPLEMENTED" | ||
| end | ||
|
|
||
| # Reverses the values in the integer array | ||
| def reverse(array, length) # Ruby | ||
| puts "NOT IMPLEMENTED" | ||
| if length <= 1 | ||
| return array | ||
| end | ||
|
|
||
| i = 0 | ||
| j = length-1 | ||
|
|
||
| while i < j do | ||
| # puts "i: #{i} array[i]: #{array[i]}" | ||
| # puts "j: #{j} array[j]: #{array[j]}" | ||
| temp = array[j] | ||
| array[j] = array[i] | ||
| array[i] = temp | ||
| i += 1 | ||
| j -= 1 | ||
| end | ||
|
|
||
| return array | ||
|
|
||
| # puts "NOT IMPLEMENTED" | ||
| 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 i < length | ||
| if array[i] == value_to_find | ||
| return true | ||
| else | ||
| i += 1 | ||
| end | ||
| end | ||
| return false | ||
| # puts "NOT IMPLEMENTED" | ||
| end | ||
|
|
||
| # Sorts the array in ascending order. | ||
| def sort(array, length) | ||
| puts "NOT IMPLEMENTED" | ||
|
|
||
| i = 0 | ||
| j = 1 | ||
| while i < length do | ||
| # puts "****i: #{i} array#{i}: #{array[i]}" | ||
| while j < length do | ||
| # puts "j: #{j} array#{j}: #{array[j]}" | ||
| if array[j] < array[i] | ||
| temp = array[j] | ||
| array[j] = array[i] | ||
| array[i] = temp | ||
| end | ||
| j+= 1 | ||
| end | ||
| i += 1 | ||
| j = i+1 | ||
| end | ||
|
|
||
| return array | ||
|
|
||
| # puts "NOT IMPLEMENTED" | ||
| end | ||
|
|
||
| # Restricted arrays cannot be resized. So, we follow a convention. | ||
|
|
@@ -38,34 +102,101 @@ 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" | ||
| i = 0 | ||
| while i < length do | ||
| if array[i] == value_to_delete | ||
| until array[i] == nil do | ||
| if array[i+1] == nil | ||
| array[i] = SPECIAL_VALUE | ||
| else | ||
| array[i] = array[i+1] | ||
| end | ||
| i += 1 | ||
| end | ||
| else | ||
| i += 1 | ||
| end | ||
| end | ||
| return array | ||
|
|
||
| # puts "NOT IMPLEMENTED" | ||
| 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" | ||
| i = 0 | ||
| while i < length | ||
| array[i] = SPECIAL_VALUE | ||
| i += 1 | ||
| end | ||
| # puts "NOT IMPLEMENTED" | ||
| 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" | ||
| largest = array[0] | ||
| i = 1 | ||
| while i < length | ||
| if array[i] > largest && array[i] != SPECIAL_VALUE | ||
| largest = array[i] | ||
| end | ||
| i += 1 | ||
| end | ||
| return largest | ||
| # puts "NOT IMPLEMENTED" | ||
| 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" | ||
| print_array(array) | ||
| # puts "value to insert: #{value_to_insert}" | ||
| has_room = false | ||
| i = 0 | ||
| until array[i] == nil || has_room == true do | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another optimization suggestion: You could eliminate this loop (which is O(n) operations) since you have access to the length of the array. You could instead check on array[length-1] == SPECIAL_VALUE to set has_room to true. (This would be O(1) operation since length is provided.)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha! I see what you're doing - going backwards to insert. Nice trick! Ignore my previous comment. |
||
| if array[i] == SPECIAL_VALUE | ||
| has_room = true | ||
| else | ||
| i += 1 | ||
| end | ||
| end | ||
| # puts "has room: #{has_room} index: #{i}" | ||
|
|
||
| if has_room == true | ||
| if i == 0 | ||
| array[i] = value_to_insert | ||
| else | ||
| while array[i] == nil || array[i-1] > value_to_insert do | ||
| # puts "#{array[i] > value_to_insert} // array [#{i}]: #{array[i]}; array[#{i-1}]: #{array[i-1]}" | ||
| array[i] = array[i-1] | ||
| i -= 1 | ||
|
|
||
| end | ||
| array[i] = value_to_insert | ||
| # puts "array #{i}: #{array[i]}" | ||
| # insert array | ||
| end | ||
| else | ||
| puts "no room" | ||
| return array | ||
| end | ||
|
|
||
| return array | ||
|
|
||
| # puts "NOT IMPLEMENTED" | ||
| end | ||
|
|
||
| ## --- END OF METHODS --- | ||
|
|
||
|
|
||
| # A restricted array could be constructed of a given size like so | ||
| puts "********Test 1********" | ||
| size = 5 | ||
| my_integer_array = RestrictedArray.new(size) | ||
| my_integer_array_length = length(my_integer_array) | ||
|
|
@@ -74,22 +205,28 @@ def insert_ascending(array, length, value_to_insert) | |
| puts | ||
|
|
||
| # A restricted array could be constructed of a random size (1 to 20) like so | ||
| puts "********Test 2********" | ||
| 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 | ||
| puts "********Test 3********" | ||
| print "Printing values in the array: " | ||
| print_array(another_array) | ||
| puts | ||
|
|
||
| # reverse the values in the current array | ||
| puts "********Test 4*******" | ||
| 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 | ||
| puts "********Test 5********" | ||
| value_to_find = 120 | ||
| if search(another_array, another_array_length, value_to_find) | ||
| puts "#{value_to_find} found in the array!" | ||
|
|
@@ -99,6 +236,7 @@ def insert_ascending(array, length, value_to_insert) | |
| puts | ||
|
|
||
| # search for value_to_find in the array - find the last value | ||
| puts "********Test 6********" | ||
| 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!" | ||
|
|
@@ -109,57 +247,67 @@ def insert_ascending(array, length, value_to_insert) | |
| puts | ||
|
|
||
| # print the largest value in the array | ||
| puts "********Test 7********" | ||
| largest = find_largest(another_array, another_array_length) | ||
| puts "The largest value in the array is #{largest}" | ||
| puts | ||
|
|
||
| # sort the array | ||
| puts "********Test 8********" | ||
| 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 | ||
| puts "********Test 9********" | ||
| 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 | ||
| puts "********Test 10********" | ||
| 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 | ||
| puts "********Test 11********" | ||
| largest = find_largest(another_array, another_array_length) | ||
| puts "The largest value in the array is #{largest}" | ||
| puts | ||
|
|
||
| # sort the array | ||
| puts "********Test 12********" | ||
| 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 | ||
| puts "********Test 13********" | ||
| 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 | ||
| puts "********Test 14********" | ||
| 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 | ||
| puts "********Test 15********" | ||
| empty(another_array, another_array_length) | ||
| print "Emptied array looks like: " | ||
| print_array(another_array) | ||
| puts | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you've gotten the basics right, just to make you think of further on another optimization: Imagine a case where the input array is sorted in descending order (opposite of what you are looking to do). In this case, you will swap at every step in the inner loop. Could we avoid or reduce this?
Since swapping is three times more work than saving a value, often, a min_index will be tracked. The min_index will be set to be the same as the value of i in the outer loop. In the inner loop, it will get updated to the value of j if array[j] < array[i]. After the inner loop completes, if i and min_index are not the same, only then, do the swap. Let me know if that makes sense.