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 second_max_number(array) | |
raise 'Argument array must be an enumerable' unless array.is_a?(Enumerable) | |
raise 'Argument array must have at least 2 elements' if array.size < 2 | |
max, second_max = nil | |
array.each do |current| | |
if max.nil? || (!current.nil? && current > max) | |
if second_max.nil? || (!max.nil? && max > second_max) | |
second_max = max | |
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
/* | |
Cell compete: | |
Eight houses, represented as a cells, are arranged as a straight line. | |
Each days every cells competes with adjacent cells. An integer value 1 | |
represents an active cell and a value of 0 represent an inactive cell. | |
if the neigbour on both the sides of the cell are both active or inactive, | |
the cell become inactive in the next day, otherwise the become active. | |
The two cells on each or have a single adjacent cell, so asume that | |
the onoccupied space in the opposite side is an inactive cell. even after | |
updating the cell state, consider its previus state when updating the state |
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 |
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
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
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
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) | |
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) | |
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) | |
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 |
NewerOlder