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..7e873bb --- /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 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..f9172b7 --- /dev/null +++ b/02_selection_sort/ruby/01_selection_sort.rb @@ -0,0 +1,29 @@ +# Finds the smallest value in an array +def find_smallest(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 selection_sort(arr) + new_arr = [] + arr.length.times do + # Finds the smallest element in the array and adds it to the new array + smallest = find_smallest(arr) + new_arr.push(arr.delete_at(smallest)) + end + new_arr +end + +# 'p obj' is the same as 'puts obj.inspect' +p selection_sort([5, 3, 6, 2, 10]) 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) 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]) 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") 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") 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 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 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