From 3cca135e4e179e606f27f5507ce956124d6cd1e5 Mon Sep 17 00:00:00 2001 From: Onur ER Date: Thu, 17 Oct 2019 12:26:54 +0300 Subject: [PATCH 01/32] Add the third problem from project Euler Add the third problem from project Euler --- Project Euler/Problem 3/problem3_sol1.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Project Euler/Problem 3/problem3_sol1.rb diff --git a/Project Euler/Problem 3/problem3_sol1.rb b/Project Euler/Problem 3/problem3_sol1.rb new file mode 100644 index 0000000..30a8bca --- /dev/null +++ b/Project Euler/Problem 3/problem3_sol1.rb @@ -0,0 +1,19 @@ +# The prime factors of 13195 are 5, 7, 13 and 29. +# What is the largest prime factor of the number 600851475143 ? + +def solution(n) + prime = 1 + i = 2 + while i * i <= n + while (n % i).zero? + prime = i + n = n.fdiv i + end + i += 1 + end + prime = n if n > 1 + prime.to_i +end + +puts solution(600851475143) + From 2980dd73c123905dabd40071661dbacd7f04bbf2 Mon Sep 17 00:00:00 2001 From: Onur ER Date: Fri, 18 Oct 2019 15:33:19 +0300 Subject: [PATCH 02/32] Add the fourth problem from project Euler Add the fourth problem from project Euler --- Project Euler/Problem 4/problem4_sol1.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Project Euler/Problem 4/problem4_sol1.rb diff --git a/Project Euler/Problem 4/problem4_sol1.rb b/Project Euler/Problem 4/problem4_sol1.rb new file mode 100644 index 0000000..fb1ac24 --- /dev/null +++ b/Project Euler/Problem 4/problem4_sol1.rb @@ -0,0 +1,11 @@ +# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. +# Find the largest palindrome made from the product of two 3-digit numbers. + +answer = 0 +999.downto(99) do |i| + 999.downto(99) do |j| + t = (i * j) + answer = i * j if (t.to_s == t.to_s.reverse) && (t > answer) && (t > answer) + end +end +puts answer \ No newline at end of file From 86d482995335036ead05d0f3cd69c38b0dccc0e8 Mon Sep 17 00:00:00 2001 From: Onur ER Date: Fri, 18 Oct 2019 15:37:07 +0300 Subject: [PATCH 03/32] Add the fourth problem from project Euler Add the fourth problem from project Euler --- Project Euler/Problem 4/problem4_sol1.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Project Euler/Problem 4/problem4_sol1.rb diff --git a/Project Euler/Problem 4/problem4_sol1.rb b/Project Euler/Problem 4/problem4_sol1.rb new file mode 100644 index 0000000..fb1ac24 --- /dev/null +++ b/Project Euler/Problem 4/problem4_sol1.rb @@ -0,0 +1,11 @@ +# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. +# Find the largest palindrome made from the product of two 3-digit numbers. + +answer = 0 +999.downto(99) do |i| + 999.downto(99) do |j| + t = (i * j) + answer = i * j if (t.to_s == t.to_s.reverse) && (t > answer) && (t > answer) + end +end +puts answer \ No newline at end of file From 2f53abe79881392940bf80a227e6e693c2dc6741 Mon Sep 17 00:00:00 2001 From: vzvu3k6k Date: Tue, 27 Oct 2020 05:35:41 +0900 Subject: [PATCH 04/32] Add tests for Sorting/bogo_sort.rb --- sorting/bogo_sort.rb | 8 +++++--- sorting/bogo_sort_test.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 sorting/bogo_sort_test.rb diff --git a/sorting/bogo_sort.rb b/sorting/bogo_sort.rb index c447cd5..c792026 100644 --- a/sorting/bogo_sort.rb +++ b/sorting/bogo_sort.rb @@ -13,6 +13,8 @@ class Array end end -puts "Enter a list of numbers separated by space" -str = gets.chomp.split('') -puts str.bogosort.join('') +if $0 == __FILE__ + puts "Enter a list of numbers separated by space" + str = gets.chomp.split('') + puts str.bogosort.join('') +end diff --git a/sorting/bogo_sort_test.rb b/sorting/bogo_sort_test.rb new file mode 100644 index 0000000..3fc6e83 --- /dev/null +++ b/sorting/bogo_sort_test.rb @@ -0,0 +1,16 @@ +require 'minitest/autorun' +require_relative './bogo_sort' + +class TestBogoSort < Minitest::Test + def test_sorted_array + input = [1, 2, 3, 4, 5] + expected = input.dup + assert_equal expected, input.bogosort + end + + def test_reversed_array + input = [5, 4, 3, 2, 1] + expected = [1, 2, 3, 4, 5] + assert_equal expected, input.bogosort + end +end From f3dc923ccb368551b6207188f702746e6c3bd93b Mon Sep 17 00:00:00 2001 From: vzvu3k6k Date: Tue, 27 Oct 2020 05:41:54 +0900 Subject: [PATCH 05/32] Add Rakefile to run tests --- Rakefile | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Rakefile diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..d4dcdd1 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.test_files = FileList['**/*_test.rb'] +end + +task default: :test From 99b940ec8efda4c7abc0d1c630b106d82d562f3d Mon Sep 17 00:00:00 2001 From: vzvu3k6k Date: Tue, 27 Oct 2020 05:42:18 +0900 Subject: [PATCH 06/32] Run tests on CI --- .github/workflows/test.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..cfca6b2 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,12 @@ +name: test +on: [push, pull_request] +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - uses: actions/setup-ruby@master + with: + ruby-version: '2.7' + - name: Run tests + run: rake test From d6e44e6afaedf9ff9cd90021fc7642424a6d0ed0 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 14 Nov 2020 18:53:02 +0000 Subject: [PATCH 07/32] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..2fcb289 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -42,6 +42,7 @@ ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) + * [Bogo Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort_test.rb) * [Bubble Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort.rb) * [Bucket Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort.rb) * [Heap Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort.rb) From d24e95ae02dd2eed13501181cb3d69b350973959 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:18:06 -0800 Subject: [PATCH 08/32] Add brute force solution --- .../get_products_of_all_other_elements.rb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 data_structures/arrays/get_products_of_all_other_elements.rb diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb new file mode 100644 index 0000000..8bfe4a6 --- /dev/null +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -0,0 +1,23 @@ +# Arrays - Get Products of all other elements in Ruby + +# Algorithm challenge description: +# Given an array of integers, return a new array such that +# each element at index `i` of the new array is the product of +# all the numbers in the original array except the one at `i`. + +# 1. Brute force solution +def calculate_products_of_all_other_elements(nums) + products_other_elements = Array.new(nums.length, 1) + + nums.each_with_index do |num1, i| + nums.each_with_index do |num2, j| + if (i != j) + products_other_elements[i] = products_other_elements[i] * num2 + end + end + end + + products_other_elements +end + +puts(calculate_products_of_all_other_elements([1, 2, 3])) From ecde337d9bab5665169c161f88345e0230a25226 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:18:22 -0800 Subject: [PATCH 09/32] Add better solution - O(n) time and space --- .../get_products_of_all_other_elements.rb | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb index 8bfe4a6..b4a8a2f 100644 --- a/data_structures/arrays/get_products_of_all_other_elements.rb +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -21,3 +21,65 @@ def calculate_products_of_all_other_elements(nums) end puts(calculate_products_of_all_other_elements([1, 2, 3])) + +# 2. O(n) time and space +# Arrays - Get Products of all other elements in Ruby + +# Generates prefix products +def build_prefix_products(nums) + prefix_products = [] + + nums.each do |num| + if prefix_products.count > 0 + prefix_products << (prefix_products.last * num) + else + prefix_products << num + end + end + + return prefix_products +end + +# Generates suffix products +def build_suffix_products(nums) + suffix_products = [] + + nums.reverse.each do |num| + if suffix_products.count > 0 + suffix_products << (suffix_products.last * num) + else + suffix_products << num + end + end + + return suffix_products +end + +# Builds output +def output(prefix_products, suffix_products, nums) + result = [] + + nums.reverse.each_with_index do |num, index| + if index == 0 + result << suffix_products[index + 1] + elsif index == nums.length - 1 + result << prefix_products[index - 1] + else + result << (prefix_products[index - 1] * suffix_products[index + 1]) + end + end + + return result +end + +# Generate result from the product of prefixes and suffixes +def products(nums) + prefix_products = build_prefix_products(nums) + suffix_products = build_suffix_products(nums) + suffix_products = suffix_products.reverse + + return output(prefix_products, suffix_products, nums) +end + +puts(products([1, 2, 3])) +# => [6, 3, 2] From 13f9770de540fd3470623114358f6f6db99582e2 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:19:07 -0800 Subject: [PATCH 10/32] Add DIRECTORY.md entry --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..e2a7845 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -3,6 +3,8 @@ * [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb) ## Data Structures + * Arrays + * [Get Products of all other elements in Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * Binary Trees * [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb) * [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb) From dcfdf73a7f937e12470a585add4e6107ef942583 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:20:45 -0800 Subject: [PATCH 11/32] Separators --- data_structures/arrays/get_products_of_all_other_elements.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb index b4a8a2f..e22dd1a 100644 --- a/data_structures/arrays/get_products_of_all_other_elements.rb +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -5,7 +5,9 @@ # each element at index `i` of the new array is the product of # all the numbers in the original array except the one at `i`. +# # 1. Brute force solution +# def calculate_products_of_all_other_elements(nums) products_other_elements = Array.new(nums.length, 1) @@ -22,8 +24,10 @@ end puts(calculate_products_of_all_other_elements([1, 2, 3])) +# # 2. O(n) time and space # Arrays - Get Products of all other elements in Ruby +# # Generates prefix products def build_prefix_products(nums) From a285989e1cf2601c519bd338aab2d8b115b879b6 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:23:48 -0800 Subject: [PATCH 12/32] Rename var --- .../arrays/get_products_of_all_other_elements.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb index e22dd1a..0e7d97d 100644 --- a/data_structures/arrays/get_products_of_all_other_elements.rb +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -7,19 +7,19 @@ # # 1. Brute force solution -# +# def calculate_products_of_all_other_elements(nums) - products_other_elements = Array.new(nums.length, 1) + product_of_other_elements = Array.new(nums.length, 1) nums.each_with_index do |num1, i| nums.each_with_index do |num2, j| if (i != j) - products_other_elements[i] = products_other_elements[i] * num2 + product_of_other_elements[i] = product_of_other_elements[i] * num2 end end end - products_other_elements + product_of_other_elements end puts(calculate_products_of_all_other_elements([1, 2, 3])) From 6a64ce0c5ac381f79a7243b62341e90a05a0e7ca Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:24:18 -0800 Subject: [PATCH 13/32] Update DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index e2a7845..d0f0b0a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -4,7 +4,7 @@ ## Data Structures * Arrays - * [Get Products of all other elements in Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) + * [Get products of all other elements in Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * Binary Trees * [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb) * [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb) From 4586b9dcd91c78f7a8dc56c8d0b58f74c0abad7e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:06:24 -0800 Subject: [PATCH 14/32] Add Search algorithm: Recursive linear search --- searches/recursive_linear_search.rb | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 searches/recursive_linear_search.rb diff --git a/searches/recursive_linear_search.rb b/searches/recursive_linear_search.rb new file mode 100644 index 0000000..67065cd --- /dev/null +++ b/searches/recursive_linear_search.rb @@ -0,0 +1,30 @@ +=begin +A pure Ruby implementation of a recursive linear search algorithm +=end + +def rec_linear_search(sequence, low, high, target) + unless (high < sequence.length && low < sequence.length) + raise Exception("Invalid upper or lower bound!") + end + + if high < low + return -1 + end + if sequence[low] == target + return low + end + if sequence[high] == target + return high + end + + return rec_linear_search(sequence, low + 1, high - 1, target) +end + +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)) +# => 0 +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)) +# => 4 +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)) +# => 1 +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)) +# => -1 From c5898581f46a1309f80a3d42ee566e3688653d7d Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:08:48 -0800 Subject: [PATCH 15/32] Fix rubocop offenses --- searches/recursive_linear_search.rb | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/searches/recursive_linear_search.rb b/searches/recursive_linear_search.rb index 67065cd..0c80a8d 100644 --- a/searches/recursive_linear_search.rb +++ b/searches/recursive_linear_search.rb @@ -1,30 +1,25 @@ -=begin -A pure Ruby implementation of a recursive linear search algorithm -=end +# A pure Ruby implementation of a recursive linear search algorithm def rec_linear_search(sequence, low, high, target) - unless (high < sequence.length && low < sequence.length) - raise Exception("Invalid upper or lower bound!") - end + raise Exception('Invalid upper or lower bound!') unless high < sequence.length && low < sequence.length - if high < low - return -1 - end - if sequence[low] == target - return low - end - if sequence[high] == target - return high - end + return -1 if high < low - return rec_linear_search(sequence, low + 1, high - 1, target) + return low if sequence[low] == target + + return high if sequence[high] == target + + rec_linear_search(sequence, low + 1, high - 1, target) end puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)) # => 0 + puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)) # => 4 + puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)) # => 1 + puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)) # => -1 From 99fae7a9309a35434f56848b9b7e215feb9f407e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:11:46 -0800 Subject: [PATCH 16/32] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..27bfeaa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -39,6 +39,7 @@ * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) + * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) From 00a3e61cfec3cd817268634c8c9c53a0fc14d224 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:49:22 -0800 Subject: [PATCH 17/32] Add double linear search --- searches/double_linear_search.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 searches/double_linear_search.rb diff --git a/searches/double_linear_search.rb b/searches/double_linear_search.rb new file mode 100644 index 0000000..e8b63f3 --- /dev/null +++ b/searches/double_linear_search.rb @@ -0,0 +1,29 @@ +# Iterate through the array from both sides to find the index of search_item. + +def double_linear_search(array, search_item) + start_ind = 0 + end_ind = array.length - 1 + + while start_ind <= end_ind + if array[start_ind] == search_item + return start_ind + elsif array[end_ind] == search_item + return end_ind + else + start_ind += 1 + end_ind -= 1 + end + end + + # returns -1 if search_item is not found in array + return -1 +end + +puts(double_linear_search([1, 5, 5, 10], 1)) +# => 0 +puts(double_linear_search([1, 5, 5, 10], 5)) +# => 1 +puts(double_linear_search([1, 5, 5, 10], 100)) +# => -1 +puts(double_linear_search([1, 5, 5, 10], 10)) +# => 3 From e2a03f33c4e7ef662ede4323b4f33a886404b8b2 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:50:42 -0800 Subject: [PATCH 18/32] Pass rubocop --- searches/double_linear_search.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/searches/double_linear_search.rb b/searches/double_linear_search.rb index e8b63f3..e68b02f 100644 --- a/searches/double_linear_search.rb +++ b/searches/double_linear_search.rb @@ -5,18 +5,15 @@ def double_linear_search(array, search_item) end_ind = array.length - 1 while start_ind <= end_ind - if array[start_ind] == search_item - return start_ind - elsif array[end_ind] == search_item - return end_ind - else - start_ind += 1 - end_ind -= 1 - end + return start_ind if array[start_ind] == search_item + return end_ind if array[end_ind] == search_item + + start_ind += 1 + end_ind -= 1 end # returns -1 if search_item is not found in array - return -1 + -1 end puts(double_linear_search([1, 5, 5, 10], 1)) From 7afb2806d31fd0af962d06aa7906d0633870c825 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:51:23 -0800 Subject: [PATCH 19/32] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..b668d4f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -39,6 +39,7 @@ * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) + * [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb) ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) From 7289ef00552117c4e08131009475abedf5d7e944 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:53:04 -0800 Subject: [PATCH 20/32] spaces --- searches/double_linear_search.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/searches/double_linear_search.rb b/searches/double_linear_search.rb index e68b02f..86abbfe 100644 --- a/searches/double_linear_search.rb +++ b/searches/double_linear_search.rb @@ -18,9 +18,12 @@ end puts(double_linear_search([1, 5, 5, 10], 1)) # => 0 + puts(double_linear_search([1, 5, 5, 10], 5)) # => 1 + puts(double_linear_search([1, 5, 5, 10], 100)) # => -1 + puts(double_linear_search([1, 5, 5, 10], 10)) # => 3 From 4de3238d39098d6df18e9119ca619a2355fb9421 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 17:01:07 -0800 Subject: [PATCH 21/32] Add Recursive Double Linear Search --- searches/recursive_double_linear_search.rb | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 searches/recursive_double_linear_search.rb diff --git a/searches/recursive_double_linear_search.rb b/searches/recursive_double_linear_search.rb new file mode 100644 index 0000000..f38f4a3 --- /dev/null +++ b/searches/recursive_double_linear_search.rb @@ -0,0 +1,28 @@ +# Iterate through the array to find the index of key using recursion. + +def recursive_double_linear_search(data, key, left = 0, right = 0) + right &&= data.length - 1 + + return -1 if left > right + + return left if data[left] == key + + return right if data[right] == key + + recursive_double_linear_search(data, key, left + 1, right - 1) +end + +puts(recursive_double_linear_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 5)) +# => 5 + +puts(recursive_double_linear_search([1, 2, 4, 5, 3], 4)) +# => 2 + +puts(recursive_double_linear_search([1, 2, 4, 5, 3], 6)) +# => -1 + +puts(recursive_double_linear_search([5], 5)) +# => 0 + +puts(recursive_double_linear_search([], 1)) +# => -1 From c82082c3c43881200eb7768fdb4815155a431795 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 17:01:42 -0800 Subject: [PATCH 22/32] Add entry to DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..4a879d0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -39,6 +39,7 @@ * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) + * [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_double_linear_search.rb) ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) From 89ad4fdbbc2f284a15f718ca9a606e625d4d22fb Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 17:02:35 -0800 Subject: [PATCH 23/32] Kill spaces --- searches/recursive_double_linear_search.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/searches/recursive_double_linear_search.rb b/searches/recursive_double_linear_search.rb index f38f4a3..ade0f59 100644 --- a/searches/recursive_double_linear_search.rb +++ b/searches/recursive_double_linear_search.rb @@ -6,7 +6,6 @@ def recursive_double_linear_search(data, key, left = 0, right = 0) return -1 if left > right return left if data[left] == key - return right if data[right] == key recursive_double_linear_search(data, key, left + 1, right - 1) From 43ebe0c33edffaab11db21f7e8bfc1e9b7bc383a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 27 Dec 2020 11:13:10 -0800 Subject: [PATCH 24/32] Add Trie data structure: Search string --- data_structures/tries/trie.rb | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 data_structures/tries/trie.rb diff --git a/data_structures/tries/trie.rb b/data_structures/tries/trie.rb new file mode 100644 index 0000000..2a3cb2f --- /dev/null +++ b/data_structures/tries/trie.rb @@ -0,0 +1,75 @@ +# A Trie (prefix tree) is a kind of search tree used to provide quick lookup +# of words/patterns in a set of words. A basic Trie however has O(n^2) +# space complexity making it impractical in practice. +# It however provides O(max(search_string, length of longest word)) +# lookup time making it an optimal approach when space is not an issue. + +class Node + attr_reader :value, :next + attr_accessor :word + + def initialize(value) + @value = value + @word = false + @next = [] + end +end + +class Trie + def initialize + @root = Node.new('*') + end + + def insert_many(word) + letters = word.chars + base = @root + + letters.each do |letter| + base = insert(letter, base.next) + end + end + + def include?(word) + letters = word.chars + base = @root + + letters.all? do |letter| + base = find(letter, base.next) + end + end + + private + + # check if it already exists + # if not add character to node + def insert(character, trie) + found = trie.find do |n| + n.value == character + end + + add_node(character, trie) unless found + end + + def add_node(character, trie) + Node.new(character).tap do |new_node| + trie << new_node + end + end + + def find(character, trie) + trie.find do |n| + n.value == character + end + end +end + +trie = Trie.new +trie.insert_many('Dogs') +trie.insert_many('South') +trie.insert_many('Cape Town') + +puts trie.include?('Cape Town') +# => true + +puts trie.include?('not presented') +# => false From bfcba1016bcb51d7070184d40fc64458be112f5f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 27 Dec 2020 14:15:21 -0800 Subject: [PATCH 25/32] Update directory.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..7cbdff4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -11,6 +11,8 @@ * Linked Lists * [Double List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/double_list.rb) * [Single List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/single_list.rb) + * Tries + * [Trie](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/tries/trie.rb) ## Discrete Mathematics * [Euclidean Gcd](https://github.com/TheAlgorithms/Ruby/blob/master/discrete_mathematics/euclidean_gcd.rb) From 40b463589524c7b3c7c167565eb3b38f2f509def Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 28 Dec 2020 09:00:19 -0800 Subject: [PATCH 26/32] Update DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2fcb289..48513cb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -42,7 +42,6 @@ ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) - * [Bogo Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort_test.rb) * [Bubble Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort.rb) * [Bucket Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort.rb) * [Heap Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort.rb) From 082a02e7613545a63a253fc926e53129d3cb726b Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 28 Dec 2020 17:00:35 +0000 Subject: [PATCH 27/32] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..2fcb289 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -42,6 +42,7 @@ ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) + * [Bogo Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort_test.rb) * [Bubble Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort.rb) * [Bucket Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort.rb) * [Heap Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort.rb) From 90ac8a1a635a054392f3d355816c2372ddef2824 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 28 Dec 2020 22:36:08 +0000 Subject: [PATCH 28/32] updating DIRECTORY.md --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2fcb289..6824cfd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -19,6 +19,10 @@ ## Other * [Fisher Yates](https://github.com/TheAlgorithms/Ruby/blob/master/other/fisher_yates.rb) +## Project Euler + * Problem 4 + * [Problem4 Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/Project%20Euler/Problem%204/problem4_sol1.rb) + ## Project Euler * Problem 1 * [Problem1 Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_1/problem1_sol1.rb) From 46761fb6be7de8098d548a749a6a90842bebab59 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 28 Dec 2020 22:37:02 +0000 Subject: [PATCH 29/32] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6824cfd..cd07ffa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,6 +20,8 @@ * [Fisher Yates](https://github.com/TheAlgorithms/Ruby/blob/master/other/fisher_yates.rb) ## Project Euler + * Problem 3 + * [Problem3 Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/Project%20Euler/Problem%203/problem3_sol1.rb) * Problem 4 * [Problem4 Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/Project%20Euler/Problem%204/problem4_sol1.rb) From 610d9deab3040187a0c8afd0536579b95d738ae2 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 28 Dec 2020 22:37:24 +0000 Subject: [PATCH 30/32] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1fb6494..ed7e774 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -4,7 +4,7 @@ ## Data Structures * Arrays - * [Get products of all other elements in Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) + * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * Binary Trees * [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb) * [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb) From 529f715f7a3883f8ebfc8ad078b6cc8445ca966a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 28 Dec 2020 22:38:25 +0000 Subject: [PATCH 31/32] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 35c593e..dc8778a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -46,8 +46,8 @@ ## Searches * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) - * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) * [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb) + * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) ## Sorting From 2eea7df79766be736309fd423100927a4c53c353 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 28 Dec 2020 22:39:16 +0000 Subject: [PATCH 32/32] updating DIRECTORY.md --- DIRECTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9dae632..9fc5c68 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -46,10 +46,10 @@ ## Searches * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) - * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) - * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) * [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb) + * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) * [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_double_linear_search.rb) + * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb)