From b855d75da8251cef45250ba7b2dc344e785b8ded Mon Sep 17 00:00:00 2001 From: Chantal Rollison Date: Sun, 19 Mar 2017 13:32:17 -0700 Subject: [PATCH] answered problems --- efficiency.rb | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ examples.md | 70 ------------------------------------- 2 files changed, 97 insertions(+), 70 deletions(-) create mode 100644 efficiency.rb delete mode 100644 examples.md diff --git a/efficiency.rb b/efficiency.rb new file mode 100644 index 0000000..b9a35d9 --- /dev/null +++ b/efficiency.rb @@ -0,0 +1,97 @@ + +# 1) + +def largest?(array, value) + array.each do |item| + return false if item > value + end + return true +end + +# This is O(n) because the data grows proportionately along with the loop. + +# 2) + +def info_dump(customers) + puts "Customer Names: " + customers.each do |customer| + puts "#{customer[:name]}" + end + puts "Customer Locations: " + customers.each do |customer| + puts "#{customer[:country]}" + end +end + +# This is O(n) because the loops aren't nested, and they will run a certain number of times. + +# 3) + +def first_element_is_red?(array) + array[0] == 'red' ? true : false +end + +# this is constant, O(1) because we must only check the first element of the array and put it. + +# 4) +def duplicates?(array) + array.each_with_index do |item1, index1| + array.each_with_index do |item2, index2| + next if index1 == index2 + return true if item1 == item2 + end + end + false +end + +# This is quadratic, O(n^2) because there are nested loops. + +# 5) + +words = [chocolate, coconut, rainbow] +endings = [cookie, pie, waffle] + +words.each do |word| + endings.each do |ending| + puts word + ending + end +end + +# This is O(n * m) because though the loops are nested, they are independent and run linearly. + +# 6) +numbers = # some array (you don't know contents) + +def print_array(array) + array.each {|num| puts num} +end + +# This is linear, O(n) because the data grows proportionately along with the loop. + +# 7) + +# this is insertion sort +(2...num.length).each do |j| + key = num[j] + i = j - 1 + while i > 0 and num[i] > key + num[i+1] = num[i] + i = i - 1 + end + num[i+1] = key +end + +# This is quadratic because as we get more elements, things get exponentially more expensive. + +# 8) + +# this is selection sort +n.times do |i| + index_min = i + (i + 1).upto(n-1) do |j| + index_min = j if a[j] < a[index_min] + end + a[i], a[index_min] = a[index_min], a[i] if index_min != i +end + +# This is quadratic because the data grows proportionately along with the loops. diff --git a/examples.md b/examples.md deleted file mode 100644 index 9f084ae..0000000 --- a/examples.md +++ /dev/null @@ -1,70 +0,0 @@ -## Examples - -Snippet EX-A - Big O: O(n) because as the data set size grows the amount of loop iterations grows linearly -```ruby -(1..data.length).each do - puts "hello" -end -``` - -Snippet EX-B - Big O: O(n) because even though there is a loop inside of a loop here, the inside loop runs a fixed number of times making it constant time, and only the outer loop grows linearly -```ruby -(1..data.length).each do - (1..10).each do - puts "hello" - end -end -``` - -Snippet EX-C - Big O: O(n^2) because we have a O(n) loop inside a O(n) loop which means that we multiply them to get O(n^2) -```ruby -(1..data.length).each do - (1..data.length).each do - puts "hello" - end -end -``` - -Snippet EX-D - Big O: O(n^2) because in the worst case (i=1) the inner loop runs O(n) times and the outer loop runs O(n), which we multiply to be O(n^2) -```ruby -(1..data.length).each do |i| - (i..data.length).each do - puts i - end -end -``` - -Snippet EX-E - Big O: O(n * m) because the two loops do not depend on each other but both run linearly in time -```ruby -(1..data.length).each do - (1..otherdata.length).each do - puts "hello" - end -end -``` - -Snippet EX-F - Big O: O(n * m) because for every element in the data array (n) we must visit every element in the hash for that element (m). -```ruby -data.each do |item| - item.each do |key, value| - puts "#{key} maps to #{value}" - end -end -``` - -Snippet EX-G - Big O: O(n) because in order to get all the keys in the hash, we must visit every element one time, which is a linear operation -```ruby -puts hash.keys -``` - -Snippet EX-H - Big O: O(n) because the first set of nested loops is O(n) * O(1) = O(n) and then the next sequential loop is also O(n), but because they are not nested, we just choose the 'worst' case (though they are the same in this case) -```ruby -data.each do |i| - 3.times do - puts i - end -end -data.each do - puts "hello" -end -```