This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ArrayUtils | |
extend self | |
# Converts a nested array to a flatten array without using the built-in method Array#flatten | |
# It doesn't remove nil | |
# | |
# If the passed parameter is not an array, the same object is returned | |
# to_flat_array(nil) #=> nil | |
# to_flat_array({ a: 'something }) #=> {:a=>"something"} | |
# If the passed parameter is a nested array, a flatten array is returned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a) | |
val = 0 | |
a.uniq.sort.each do |e| | |
next if e <= 0 | |
return val.next if e > val.next | |
val = e | |
end | |
val.next | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a) | |
head = a[0] | |
tail = a[1...a.size].reduce(&:+) | |
min = calc_diff(head, tail) | |
(1...a.size).each do |idx| | |
head += a[idx] | |
tail -= a[idx] | |
new_diff = calc_diff(head, tail) | |
min = new_diff if new_diff < min | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a) | |
val = 0 | |
a.sort.each do |e| | |
return 0 if e <= 0 || e == val || e > val.next | |
val = e | |
end | |
1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a) | |
a.map(&:abs).uniq.size | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a, b, k) | |
return ((b - a) / k) + 1 if a % k == 0 | |
(b / k - ((a / k) + 1)) + 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a) | |
return 1 unless a.include?(1) | |
val = 0 | |
a.uniq.sort.each do |e| | |
next if e <= 0 | |
return val.next if e > val.next | |
val = e | |
end | |
val.next | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT SUM(v) FROM elements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prime' | |
def solution(n) | |
primes, powers = n.prime_division.transpose | |
exponents = powers.map { |i| (0..i).to_a } | |
divisors = exponents.shift.product(*exponents).map do |powers| | |
primes.zip(powers).map { |prime, power| prime ** power }.inject(:*) | |
end | |
result = divisors.sort.map { |div| [div, n / div] } | |
return 0 if result.nil? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a, k) | |
return a if a.empty? || k.zero? | |
k = k.modulo(a.size) if k > a.size | |
a.unshift(*a.pop(k)) | |
end |
OlderNewer