Skip to content

Instantly share code, notes, and snippets.

View juanfurattini's full-sized avatar

Juan Manuel Furattini juanfurattini

View GitHub Profile
@juanfurattini
juanfurattini / array_flattener.rb
Last active July 26, 2018 06:22
Flattens an Array without using the built-in Array#flatten method (For testing it requires spec)
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
@juanfurattini
juanfurattini / codility_missing_number.rb
Last active July 26, 2018 05:20
Solution for Codility's MissingInteger exercise with a score of 100% (Ruby)
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
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
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
def solution(a)
a.map(&:abs).uniq.size
end
def solution(a, b, k)
return ((b - a) / k) + 1 if a % k == 0
(b / k - ((a / k) + 1)) + 1
end
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
SELECT SUM(v) FROM elements
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?
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