From a1a41e9097ac3b15b9f54087b8efc339f6f50a55 Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:05:30 +0100 Subject: [PATCH 01/11] code for chapter 1 in ruby --- .../ruby/01_binary_search.rb | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 01_introduction_to_algorithms/ruby/01_binary_search.rb diff --git a/01_introduction_to_algorithms/ruby/01_binary_search.rb b/01_introduction_to_algorithms/ruby/01_binary_search.rb new file mode 100644 index 0000000..33762ad --- /dev/null +++ b/01_introduction_to_algorithms/ruby/01_binary_search.rb @@ -0,0 +1,26 @@ +def binary_search(list, item) + low = 0 + high = list.length - 1 + + while low <= high + mid = (low + high) / 2 + guess = list[mid] + + if guess == item + return mid + elsif guess > item + high = mid - 1 + else + low = mid + 1 + end + end + + return nil +end + +my_list = [1, 3, 5, 7, 9] +puts binary_search(my_list, 3) # => 1 + +# We need to use .inspect here because just printing nil +# would output an empty string +puts binary_search(my_list, -1).inspect # => nil From 1e2d586c44f8747f5ffe3af9af5c91e02a11809a Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:12:58 +0100 Subject: [PATCH 02/11] code for chapter 2 in ruby --- 02_selection_sort/ruby/01_selection_sort.rb | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 02_selection_sort/ruby/01_selection_sort.rb diff --git a/02_selection_sort/ruby/01_selection_sort.rb b/02_selection_sort/ruby/01_selection_sort.rb new file mode 100644 index 0000000..463d251 --- /dev/null +++ b/02_selection_sort/ruby/01_selection_sort.rb @@ -0,0 +1,29 @@ +# Finds the smallest value in an array +def findSmallest(arr) + # Stores the smallest value + smallest = arr[0] + # Stores the index of the smallest value + smallest_index = 0 + # (1...n) is the same as (1..(n-1)) + (1...(arr.length)).each do |i| + if arr[i] < smallest + smallest = arr[i] + smallest_index = i + end + end + smallest_index +end + +# Sort array +def selectionSort(arr) + newArr = [] + (0...(arr.length)).each do |i| + # Finds the smallest element in the array and adds it to the new array + smallest = findSmallest(arr) + newArr.push(arr.delete_at(smallest)) + end + newArr +end + +# 'p obj' is the same as 'puts obj.inspect' +p selectionSort([5, 3, 6, 2, 10]) From f6cb264c686bb899db1e7f16fb1da55f4dddbc8c Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:19:05 +0100 Subject: [PATCH 03/11] code for chapter 3 in ruby --- 03_recursion/ruby/01_countdown.rb | 12 ++++++++++++ 03_recursion/ruby/02_greet.rb | 16 ++++++++++++++++ 03_recursion/ruby/03_factorial.rb | 9 +++++++++ 3 files changed, 37 insertions(+) create mode 100644 03_recursion/ruby/01_countdown.rb create mode 100644 03_recursion/ruby/02_greet.rb create mode 100644 03_recursion/ruby/03_factorial.rb diff --git a/03_recursion/ruby/01_countdown.rb b/03_recursion/ruby/01_countdown.rb new file mode 100644 index 0000000..d28b3b1 --- /dev/null +++ b/03_recursion/ruby/01_countdown.rb @@ -0,0 +1,12 @@ +def countdown(i) + puts i + # base case + if i <= 0 + return + # recursive case + else + countdown(i - 1) + end +end + +countdown(5) diff --git a/03_recursion/ruby/02_greet.rb b/03_recursion/ruby/02_greet.rb new file mode 100644 index 0000000..200d4e1 --- /dev/null +++ b/03_recursion/ruby/02_greet.rb @@ -0,0 +1,16 @@ +def greet2(name) + puts "how are you, #{name}?" +end + +def bye + print "ok bye!" +end + +def greet(name) + puts "hello, #{name}!" + greet2(name) + puts "getting ready to say bye..." + bye +end + +greet("adit") diff --git a/03_recursion/ruby/03_factorial.rb b/03_recursion/ruby/03_factorial.rb new file mode 100644 index 0000000..a5c0a99 --- /dev/null +++ b/03_recursion/ruby/03_factorial.rb @@ -0,0 +1,9 @@ +def fact(x) + if x == 1 + 1 + else + x * fact(x - 1) + end +end + +puts fact(5) From 2d84d265405f46b6efe75f659ee7d0e536461c9d Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:21:21 +0100 Subject: [PATCH 04/11] remove trailing whitespace --- 01_introduction_to_algorithms/ruby/01_binary_search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_introduction_to_algorithms/ruby/01_binary_search.rb b/01_introduction_to_algorithms/ruby/01_binary_search.rb index 33762ad..7e873bb 100644 --- a/01_introduction_to_algorithms/ruby/01_binary_search.rb +++ b/01_introduction_to_algorithms/ruby/01_binary_search.rb @@ -14,7 +14,7 @@ def binary_search(list, item) low = mid + 1 end end - + return nil end From 0aa3c5efc7f092463390df4a87b64aec092674bb Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:23:16 +0100 Subject: [PATCH 05/11] use snake_case for method and variable names --- 02_selection_sort/ruby/01_selection_sort.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/02_selection_sort/ruby/01_selection_sort.rb b/02_selection_sort/ruby/01_selection_sort.rb index 463d251..f9172b7 100644 --- a/02_selection_sort/ruby/01_selection_sort.rb +++ b/02_selection_sort/ruby/01_selection_sort.rb @@ -1,5 +1,5 @@ # Finds the smallest value in an array -def findSmallest(arr) +def find_smallest(arr) # Stores the smallest value smallest = arr[0] # Stores the index of the smallest value @@ -15,15 +15,15 @@ def findSmallest(arr) end # Sort array -def selectionSort(arr) - newArr = [] - (0...(arr.length)).each do |i| +def selection_sort(arr) + new_arr = [] + arr.length.times do # Finds the smallest element in the array and adds it to the new array - smallest = findSmallest(arr) - newArr.push(arr.delete_at(smallest)) + smallest = find_smallest(arr) + new_arr.push(arr.delete_at(smallest)) end - newArr + new_arr end # 'p obj' is the same as 'puts obj.inspect' -p selectionSort([5, 3, 6, 2, 10]) +p selection_sort([5, 3, 6, 2, 10]) From 33d432bdaf421d78af2bb688ad7bb62219e6c5ad Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:34:16 +0100 Subject: [PATCH 06/11] code for chapter 4 in ruby --- 04_quicksort/ruby/01_loop_sum.rb | 10 ++++++++++ 04_quicksort/ruby/02_recursive_sum.rb | 4 ++++ 04_quicksort/ruby/03_recursive_count.rb | 4 ++++ 04_quicksort/ruby/04_recursive_max.rb | 9 +++++++++ 04_quicksort/ruby/05_quicksort.rb | 18 ++++++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 04_quicksort/ruby/01_loop_sum.rb create mode 100644 04_quicksort/ruby/02_recursive_sum.rb create mode 100644 04_quicksort/ruby/03_recursive_count.rb create mode 100644 04_quicksort/ruby/04_recursive_max.rb create mode 100644 04_quicksort/ruby/05_quicksort.rb diff --git a/04_quicksort/ruby/01_loop_sum.rb b/04_quicksort/ruby/01_loop_sum.rb new file mode 100644 index 0000000..27866af --- /dev/null +++ b/04_quicksort/ruby/01_loop_sum.rb @@ -0,0 +1,10 @@ +def sum(arr) + total = 0 + arr.each do |x| + total += x + end + + total +end + +puts sum([1, 2, 3, 4]) diff --git a/04_quicksort/ruby/02_recursive_sum.rb b/04_quicksort/ruby/02_recursive_sum.rb new file mode 100644 index 0000000..926b7ff --- /dev/null +++ b/04_quicksort/ruby/02_recursive_sum.rb @@ -0,0 +1,4 @@ +def sum(list) + return 0 if list.empty? + list[0] + sum(list[1..-1]) +end diff --git a/04_quicksort/ruby/03_recursive_count.rb b/04_quicksort/ruby/03_recursive_count.rb new file mode 100644 index 0000000..d159353 --- /dev/null +++ b/04_quicksort/ruby/03_recursive_count.rb @@ -0,0 +1,4 @@ +def count(list) + return 0 if list.empty? + 1 + count(list[1..-1]) +end diff --git a/04_quicksort/ruby/04_recursive_max.rb b/04_quicksort/ruby/04_recursive_max.rb new file mode 100644 index 0000000..59e7d94 --- /dev/null +++ b/04_quicksort/ruby/04_recursive_max.rb @@ -0,0 +1,9 @@ +def max(list) + if list.length == 2 + # condition ? then : else + list[0] > list[1] ? list[0] : list[1] + else + sub_max = max(list[1..-1]) + list[0] > sub_max ? list[0] : sub_max + end +end diff --git a/04_quicksort/ruby/05_quicksort.rb b/04_quicksort/ruby/05_quicksort.rb new file mode 100644 index 0000000..0fb614f --- /dev/null +++ b/04_quicksort/ruby/05_quicksort.rb @@ -0,0 +1,18 @@ +def quicksort(array) + if array.length < 2 + # base case, arrays with 0 or 1 element are already "sorted" + array + else + # recursive case + pivot = array[0] + # sub-array of all the elements less than the pivot + rest = array[1..-1] + less = rest.select { |i| i <= pivot } + # sub-array of all the elements greater than the pivot + greater = rest.select { |i| i > pivot } + + quicksort(less) + [pivot] + quicksort(greater) + end +end + +print quicksort([10, 5, 2, 3]) From 4794bad93af02030fa3710ba80c3b0783ce26611 Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:43:19 +0100 Subject: [PATCH 07/11] code for chapter 5 in ruby --- 05_hash_tables/ruby/01_price_of_groceries.rb | 8 ++++++++ 05_hash_tables/ruby/02_check_voter.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 05_hash_tables/ruby/01_price_of_groceries.rb create mode 100644 05_hash_tables/ruby/02_check_voter.rb diff --git a/05_hash_tables/ruby/01_price_of_groceries.rb b/05_hash_tables/ruby/01_price_of_groceries.rb new file mode 100644 index 0000000..9763b25 --- /dev/null +++ b/05_hash_tables/ruby/01_price_of_groceries.rb @@ -0,0 +1,8 @@ +# {} is the same as Hash.new +book = {} +# an apple costs 67 cents +book["apple"] = 0.67 +# milk costs $1.49 +book["milk"] = 1.49 +book["avocado"] = 1.49 +puts book diff --git a/05_hash_tables/ruby/02_check_voter.rb b/05_hash_tables/ruby/02_check_voter.rb new file mode 100644 index 0000000..0832e52 --- /dev/null +++ b/05_hash_tables/ruby/02_check_voter.rb @@ -0,0 +1,16 @@ +# in order to be accessible from within the check_voter function +# voted has to be an instance variable (or global / class) +@voted = {} + +def check_voter(name) + if @voted.key?(name) && @voted[name] + puts "kick them out!" + else + @voted[name] = true + puts "let them vote!" + end +end + +check_voter("tom") +check_voter("mike") +check_voter("mike") From da6478b78d6fdb0b878e058cf18e190ebdcb0ee7 Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:53:40 +0100 Subject: [PATCH 08/11] code for chapter 6 in ruby --- .../ruby/01_breadth-first_search.rb | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 06_breadth-first_search/ruby/01_breadth-first_search.rb diff --git a/06_breadth-first_search/ruby/01_breadth-first_search.rb b/06_breadth-first_search/ruby/01_breadth-first_search.rb new file mode 100644 index 0000000..cb21b9f --- /dev/null +++ b/06_breadth-first_search/ruby/01_breadth-first_search.rb @@ -0,0 +1,40 @@ +def person_is_seller(name) + name[-1] == "m" +end + +@graph = {} + +# %w(string1 string2 ...) is a shorter way to define arrays of strings +@graph["you"] = %w(alice bob claire) +@graph["bob"] = %w(anuj peggy) +@graph["alice"] = %w(peggy) +@graph["claire"] = %w(thom jonny) +@graph["anuj"] = [] +@graph["peggy"] = [] +@graph["thom"] = [] +@graph["jonny"] = [] + +def search(name) + search_queue = [] + search_queue += @graph[name] + # This array is how you keep track of which people you've searched before. + searched = [] + + until search_queue.empty? + person = search_queue.shift + # Only search this person if you haven't already searched them. + next if searched.member?(person) + if person_is_seller(person) + puts "#{person} is a mango seller!" + return true + else + search_queue += @graph[person] + # Marks this person as searched + searched.push(person) + end + end + + false +end + +search("you") From 738f9cac228c2c547a547932588673920a75a8ef Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 16:07:24 +0100 Subject: [PATCH 09/11] code for chapter 7 in ruby --- .../ruby/01_dijkstras_algorithm.rb | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb diff --git a/07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb b/07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb new file mode 100644 index 0000000..bb9001f --- /dev/null +++ b/07_dijkstras_algorithm/ruby/01_dijkstras_algorithm.rb @@ -0,0 +1,69 @@ +# the graph +graph = {} +graph["start"] = {} +graph["start"]["a"] = 6 +graph["start"]["b"] = 2 + +graph["a"] = {} +graph["a"]["fin"] = 1 + +graph["b"] = {} +graph["b"]["a"] = 3 +graph["b"]["fin"] = 5 + +graph["fin"] = {} + +# the costs table +costs = {} +costs["a"] = 6 +costs["b"] = 2 +costs["fin"] = Float::INFINITY + +# the parents table +parents = {} +parents["a"] = "start" +parents["b"] = "start" +parents["fin"] = nil + +@processed = [] + +def find_lowest_cost_node(costs) + lowest_cost = Float::INFINITY + lowest_cost_node = nil + # Go through each node. + costs.each do |node, cost| + # If it's the lowest cost so far and hasn't been processed yet... + if cost < lowest_cost && !@processed.member?(node) + # ... set it as the new lowest-cost node. + lowest_cost = cost + lowest_cost_node = node + end + end + lowest_cost_node +end + +# Find the lowest-cost node that you haven't processed yet. +node = find_lowest_cost_node(costs) +# If you've processed all the nodes, this while loop is done. +until node.nil? + cost = costs[node] + # Go through all the neighbors of this node. + neighbors = graph[node] + neighbors.keys.each do |n| + new_cost = cost + neighbors[n] + # If it's cheaper to get to this neighbor by going through this node... + if costs[n] > new_cost + # ... update the cost for this node. + costs[n] = new_cost + # This node becomes the new parent for this neighbor. + parents[n] = node + end + end + # Mark the node as processed. + @processed << node + # Find the next node to process, and loop. + node = find_lowest_cost_node(costs) +end + +puts "Cost from the start to each node:" +puts costs From 58b026e7b89fdaa7504c5aae76be46131ed1c5f2 Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 16:15:47 +0100 Subject: [PATCH 10/11] code for chapter 8 in ruby --- 08_greedy_algorithms/ruby/01_set_covering.rb | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 08_greedy_algorithms/ruby/01_set_covering.rb diff --git a/08_greedy_algorithms/ruby/01_set_covering.rb b/08_greedy_algorithms/ruby/01_set_covering.rb new file mode 100644 index 0000000..7bf982b --- /dev/null +++ b/08_greedy_algorithms/ruby/01_set_covering.rb @@ -0,0 +1,31 @@ +require "set" + +# You pass an array in, and it gets converted to a Set.new. +states_needed = Set.new(%w(mt wa or id nv ut ca az)) + +stations = {} +stations["kone"] = Set.new(%w(id nv ut)) +stations["ktwo"] = Set.new(%w(wa id mt)) +stations["kthree"] = Set.new(%w(or nv ca)) +stations["kfour"] = Set.new(%w(nv ut)) +stations["kfive"] = Set.new(%w(ca az)) + +final_stations = Set.new + +until states_needed.empty? + best_station = nil + states_covered = Set.new + + stations.each do |station, states| + covered = states_needed & states + if covered.length > states_covered.length + best_station = station + states_covered = covered + end + end + + states_needed -= states_covered + final_stations << best_station +end + +p final_stations From 7a2d6d8d77ca0dbf4142b39daa07091026566c67 Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 16:17:37 +0100 Subject: [PATCH 11/11] code for chapter 9 in ruby --- .../ruby/01_longest_common_subsequence.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 09_dynamic_programming/ruby/01_longest_common_subsequence.rb diff --git a/09_dynamic_programming/ruby/01_longest_common_subsequence.rb b/09_dynamic_programming/ruby/01_longest_common_subsequence.rb new file mode 100644 index 0000000..624023e --- /dev/null +++ b/09_dynamic_programming/ruby/01_longest_common_subsequence.rb @@ -0,0 +1,7 @@ +if word_a[i] == word_b[j] + # The letters match. + cell[i][j] = cell[i-1][j-1] + 1 +else + # The letters don't match. + cell[i][j] = [cell[i-1][j], cell[i][j-1]].max +end