Last active
June 6, 2016 14:08
-
-
Save shime/83aa7cd7f8c12bd2db2dd62eebf2533a to your computer and use it in GitHub Desktop.
my codility solutions
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) | |
counter = [0] * a.length | |
a.each do |element| | |
if ! (1 <= element && element <= a.length) | |
return 0 | |
else | |
if counter[element-1] != 0 | |
return 0 | |
else | |
counter[element-1] = 1 | |
end | |
end | |
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) | |
sumright = a[1..-1].reduce(:+) | |
sumleft = a[0] | |
min = (sumright - sumleft).abs | |
a[1..-2].each do |element| | |
sumleft += element | |
sumright -= element | |
if ((sumright - sumleft).abs < min) | |
min = (sumright - sumleft).abs | |
end | |
end | |
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(n) | |
max_gap = 0 | |
current_gap = 0 | |
a = n.to_s(2).split('').each do |number| | |
if number == "1" | |
if current_gap > max_gap | |
max_gap = current_gap | |
end | |
current_gap = 0 | |
elsif number == "0" | |
current_gap += 1 | |
end | |
end | |
max_gap | |
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(x, y, d) | |
(y - x) % d == 0 ? (y - x) / d : | |
((y - x) / d) + 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, k) | |
if k == 0 | |
return a | |
else | |
result = [] | |
a.each_with_index do |value, index| | |
result.push(a[index - 1]) | |
end | |
solution(result, k - 1) | |
end | |
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(x, a) | |
cache = [] | |
i = 0 | |
a.each do |element| | |
if !cache[element] | |
cache[element] = true | |
x -= 1 | |
end | |
if (x == 0) | |
return i | |
end | |
i += 1 | |
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) | |
n = a.length + 1 | |
total = (n * (n + 1)) / 2 | |
total - (a.inject(:+) || 0) | |
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.inject(0) {|sum, element| sum = sum ^ element } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment