chore: split solutions by language

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-06-02 17:19:02 +02:00
parent 078993071a
commit 333866d1bc
Signed by: mfocko
GPG key ID: 7C47D46246790496
131 changed files with 4 additions and 4 deletions

View file

@ -0,0 +1,31 @@
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer}
def search(nums, target)
left = 0
right = nums.size
while left < right do
mid = (left + right).div(2)
if nums[mid] == target then
return mid
elsif nums[mid] < target then
left = mid + 1
else
right = mid
end
end
return -1
end
RSpec.describe "search" do
it "nums = [-1,0,3,5,9,12], target = 9" do
expect(search([-1,0,3,5,9,12], 9)).to eq(4)
end
it "nums = [-1,0,3,5,9,12], target = 2" do
expect(search([-1,0,3,5,9,12], 2)).to eq(-1)
end
end

View file

@ -0,0 +1,27 @@
# @param {Integer} n
# @return {Integer}
def climb_stairs(n)
possible_ways = [0, 1, 2]
k = 3
while possible_ways.size <= n do
possible_ways.push(possible_ways[k - 1] + possible_ways[k - 2])
k += 1
end
return possible_ways[n]
end
RSpec.describe "climb_stairs" do
it "1 is 1" do
expect(climb_stairs(1)).to eq(1)
end
it "2 is 2" do
expect(climb_stairs(2)).to eq(2)
end
it "3 is 3" do
expect(climb_stairs(3)).to eq(3)
end
end

View file

@ -0,0 +1,27 @@
# @param {Integer[]} nums
# @return {Boolean}
def contains_duplicate(nums)
encountered = Set.new
nums.each { |x|
if encountered.add?(x) == nil then
return true
end
}
return false
end
RSpec.describe "contains_duplicate" do
it "nums = [1,2,3,1] contains" do
expect(contains_duplicate([1,2,3,1])).to be true
end
it "nums = [1,2,3,4] doesn't contain" do
expect(contains_duplicate([1,2,3,4])).to be false
end
it "nums = [1,1,1,3,3,4,3,2,4,2] contains" do
expect(contains_duplicate([1,1,1,3,3,4,3,2,4,2])).to be true
end
end

View file

@ -0,0 +1,39 @@
# @param {Integer} n
# @return {Integer}
def fib(n)
if n == 0 then
return 0
end
prev, current = 0, 1
(2..n).each {
prev, current = current, prev + current
}
return current
end
RSpec.describe "fib of " do
it "0 is 0" do
expect(fib(0)).to eq(0)
end
it "1 is 1" do
expect(fib(1)).to eq(1)
end
it "2 is 1" do
expect(fib(2)).to eq(1)
end
it "3 is 2" do
expect(fib(3)).to eq(2)
end
it "4 is 3" do
expect(fib(4)).to eq(3)
end
it "5 is 5" do
expect(fib(5)).to eq(5)
end
end

View file

@ -0,0 +1,46 @@
# The is_bad_version API is already defined for you.
# @param {Integer} version
# @return {boolean} whether the version is bad
# def is_bad_version(version):
# @param {Integer} n
# @return {Integer}
def first_bad_version(n)
left = 1
right = n
while left < right do
mid = (left + right).div(2)
if is_bad_version(mid) then
right = mid
else
left = mid + 1
end
end
return left
end
RSpec.describe "first_bad_version" do
it "n = 5, bad = 4" do
def is_bad_version(n)
return n >= 4
end
expect(first_bad_version(5)).to eq(4)
end
it "n = 1, bad = 1" do
def is_bad_version(n)
return n >= 1
end
expect(first_bad_version(1)).to eq(1)
end
it "n = 1000000, bad = 76192" do
def is_bad_version(n)
return n >= 76192
end
expect(first_bad_version(1000000)).to eq(76192)
end
end

View file

@ -0,0 +1,36 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @param {Integer} val
# @return {TreeNode}
def insert_into_bst(root, val)
new_node = TreeNode.new(val)
if root == nil then
return new_node
end
node = root
while (val < node.val && node.left != nil) || (val >= node.val && node.right != nil) do
if val < node.val then
node = node.left
else
node = node.right
end
end
# insert the node
if val < node.val then
node.left = new_node
else
node.right = new_node
end
return root
end

View file

@ -0,0 +1,27 @@
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end
# @param {ListNode} head
# @return {ListNode}
require 'set'
def detectCycle(head)
def detectCycleRec(node, seen)
if node == nil then
return node
elsif seen.add?(node) == nil then
return node
end
return detectCycleRec(node.next, seen)
end
detectCycleRec(head, Set.new)
end

View file

@ -0,0 +1,29 @@
# @param {Integer[]} nums
# @return {Integer}
def max_sub_array(nums)
if nums.empty? then
return nil
end
sum, running_sum = nums.first, nums.first
nums.drop(1).each { |x|
running_sum = [running_sum + x, x].max
sum = [sum, running_sum].max
}
return sum
end
RSpec.describe "max_sub_array of " do
it "[-2,1,-3,4,-1,2,1,-5,4] is 6" do
expect(max_sub_array([-2,1,-3,4,-1,2,1,-5,4])).to eq(6)
end
it "[1] is 1" do
expect(max_sub_array([1])).to eq(1)
end
it "[5,4,-1,7,8] is 23" do
expect(max_sub_array([5,4,-1,7,8])).to eq(23)
end
end

View file

@ -0,0 +1,59 @@
# @param {Integer[]} nums1
# @param {Integer} m
# @param {Integer[]} nums2
# @param {Integer} n
# @return {Void} Do not return anything, modify nums1 in-place instead.
def merge(nums1, m, nums2, n)
# shift numbers to right
i, k = m - 1, nums1.size - 1
while i >= 0 do
nums1[k] = nums1[i]
i -= 1
k -= 1
end
# merge them
i, j, k = nums1.size - m, 0, 0
while i < nums1.size && j < n do
if nums1[i] < nums2[j] then
nums1[k] = nums1[i]
i += 1
else
nums1[k] = nums2[j]
j += 1
end
k += 1
end
while j < n do
nums1[k] = nums2[j]
j += 1
k += 1
end
end
RSpec.describe "merge" do
it "nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 ~> [1,2,2,3,5,6]" do
nums1, m = [1,2,3,0,0,0], 3
nums2, n = [2,5,6], 3
merge(nums1, m, nums2, n)
expect(nums1).to eq([1,2,2,3,5,6])
end
it "nums1 = [0], m = 0, nums2 = [1], n = 1 ~> [1]" do
nums1, m = [1], 1
nums2, n = [], 0
merge(nums1, m, nums2, n)
expect(nums1).to eq([1])
end
it "[3, 3] for 6 is [0, 1]" do
nums1, m = [0], 0
nums2, n = [1], 1
merge(nums1, m, nums2, n)
expect(nums1).to eq([1])
end
end

View file

@ -0,0 +1,28 @@
# @param {Integer[]} cost
# @return {Integer}
def min_cost_climbing_stairs(cost)
def get(cost, k)
if k < 0 || k >= cost.size then
return 0
end
return cost[k]
end
i = cost.size - 1
while i >= 0 do
cost[i] += [get(cost, i + 1), get(cost, i + 2)].min
i -= 1
end
return cost.take(2).min
end
RSpec.describe "min_cost_climbing_stairs" do
it "of [10,15,20] is 15" do
expect(min_cost_climbing_stairs([10,15,20])).to eq(15)
end
it "of [1,100,1,1,1,100,1,1,100,1] is 6" do
expect(min_cost_climbing_stairs([1,100,1,1,1,100,1,1,100,1])).to eq(6)
end
end

View file

@ -0,0 +1,68 @@
# @param {Integer[]} nums
# @param {Integer} k
# @return {Void} Do not return anything, modify nums in-place instead.
def rotate(nums, k)
def reverse(nums, from, to)
while from < to do
nums[from], nums[to] = nums[to], nums[from]
from += 1
to -= 1
end
end
k = k % nums.size
reverse(nums, 0, nums.size - 1)
reverse(nums, 0, k - 1)
reverse(nums, k, nums.size - 1)
end
RSpec.describe "rotate" do
it "nums = [1,2,3,4,5,6,7], k = 0" do
nums = [1,2,3,4,5,6,7]
k = 0
rotate(nums, k)
expect(nums).to eq([1,2,3,4,5,6,7])
end
it "nums = [1,2,3,4,5,6,7], k = 1" do
nums = [1,2,3,4,5,6,7]
k = 1
rotate(nums, k)
expect(nums).to eq([7,1,2,3,4,5,6])
end
it "nums = [1,2,3,4,5,6,7], k = 2" do
nums = [1,2,3,4,5,6,7]
k = 2
rotate(nums, k)
expect(nums).to eq([6,7,1,2,3,4,5])
end
it "nums = [1,2,3,4,5,6,7], k = 3" do
nums = [1,2,3,4,5,6,7]
k = 3
rotate(nums, k)
expect(nums).to eq([5,6,7,1,2,3,4])
end
it "nums = [-1,-100,3,99], k = 1" do
nums = [-1,-100,3,99]
k = 1
rotate(nums, k)
expect(nums).to eq([99,-1,-100,3])
end
it "nums = [-1,-100,3,99], k = 2" do
nums = [-1,-100,3,99]
k = 2
rotate(nums, k)
expect(nums).to eq([3,99,-1,-100])
end
end

View file

@ -0,0 +1,35 @@
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer}
def search_insert(nums, target)
left = 0
right = nums.size
while left < right do
mid = (left + right).div(2)
if nums[mid] == target then
return mid
elsif nums[mid] < target then
left = mid + 1
else
right = mid
end
end
return left
end
RSpec.describe "search_insert" do
it "nums = [1,3,5,6], target = 5" do
expect(search_insert([1,3,5,6], 5)).to eq(2)
end
it "nums = [1,3,5,6], target = 2" do
expect(search_insert([1,3,5,6], 2)).to eq(1)
end
it "nums = [1,3,5,6], target = 7" do
expect(search_insert([1,3,5,6], 7)).to eq(4)
end
end

View file

@ -0,0 +1,57 @@
# @param {Integer[]} nums
# @return {Integer[]}
def sorted_squares(nums)
result = []
split = nums.find_index { |x| x >= 0 }
i, j = nil, nil
if split == nil then
i, j = nums.size - 1, nums.size
else
i, j = split - 1, split
end
while i >= 0 and j < nums.size do
i_s = nums[i] * nums[i]
j_s = nums[j] * nums[j]
if i_s < j_s then
result.push(i_s)
i -= 1
else
result.push(j_s)
j += 1
end
end
while i >= 0 do
result.push(nums[i] * nums[i])
i -= 1
end
while j < nums.size do
result.push(nums[j] * nums[j])
j += 1
end
return result
end
RSpec.describe "rotate" do
it "nums = [-4,-1,0,3,10]" do
expect(sorted_squares([-4,-1,0,3,10])).to eq([0,1,9,16,100])
end
it "nums = [-7,-3,2,3,11]" do
expect(sorted_squares([-7,-3,2,3,11])).to eq([4,9,9,49,121])
end
it "nums = [-7,-3,0,0,0,0,2,3,11]" do
expect(sorted_squares([-7,-3,0,0,0,0,2,3,11])).to eq([0,0,0,0,4,9,9,49,121])
end
it "nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]" do
expect(sorted_squares([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])).to eq([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])
end
end

43
problems/rb/tribonacci.rb Normal file
View file

@ -0,0 +1,43 @@
# @param {Integer} n
# @return {Integer}
def tribonacci(n)
sequence = [0, 1, 1]
if n < 3 then
return sequence[n]
end
(3..n).each {
sequence = sequence.drop(1) + [sequence.sum]
}
return sequence.last
end
RSpec.describe "tribonacci of " do
it "0 is 0" do
expect(tribonacci(0)).to eq(0)
end
it "1 is 1" do
expect(tribonacci(1)).to eq(1)
end
it "2 is 1" do
expect(tribonacci(2)).to eq(1)
end
it "3 is 2" do
expect(tribonacci(3)).to eq(2)
end
it "4 is 4" do
expect(tribonacci(4)).to eq(4)
end
it "5 is 7" do
expect(tribonacci(5)).to eq(7)
end
it "25 is 1389537" do
expect(tribonacci(25)).to eq(1389537)
end
end

30
problems/rb/two-sum.rb Normal file
View file

@ -0,0 +1,30 @@
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer[]}
def two_sum(nums, target)
encountered = Hash.new
nums.each_index { |i|
complement = encountered.assoc(target - nums[i])
if complement != nil then
return [complement[1], i]
else
encountered[nums[i]] = i
end
}
end
RSpec.describe "two_sum from" do
it "[2, 7, 11, 15] for 9 is [0, 1]" do
expect(two_sum([2, 7, 11, 15], 9)).to eq([0, 1])
end
it "[3, 2, 4] for 6 is [1, 2]" do
expect(two_sum([3, 2, 4], 6)).to eq([1, 2])
end
it "[3, 3] for 6 is [0, 1]" do
expect(two_sum([3, 3], 6)).to eq([0, 1])
end
end

View file

@ -0,0 +1,49 @@
# @param {String} pattern
# @param {String} s
# @return {Boolean}
def word_pattern(pattern, s)
words = s.split
expected_words = Hash.new
expected_patterns = Hash.new
pattern.chars.zip(words).each { |pat, word|
expected_word = expected_words[pat]
expected_pattern = expected_patterns[word]
if expected_pattern == nil && expected_word == nil then
expected_patterns[word] = pat
expected_words[pat] = word
elsif word != expected_word || pat != expected_pattern then
return false
end
}
return pattern.size == words.size
end
RSpec.describe "word_pattern" do
it "finds pattern" do
expect(word_pattern("abba", "dog cat cat dog")).to be true
end
it "finds one odd word" do
expect(word_pattern("abba", "dog cat cat fish")).to be false
end
it "finds that are not the same words" do
expect(word_pattern("aaaa", "dog cat cat dog")).to be false
end
it "handles bijection correctly" do
expect(word_pattern("abba", "dog dog dog dog")).to be false
end
it "handles correctly different length of input" do
expect(word_pattern("aaa", "aa aa aa aa")).to be false
end
it "handles correctly different length of pattern" do
expect(word_pattern("aaaaa", "aa aa aa aa")).to be false
end
end