Skip to content

Instantly share code, notes, and snippets.

@shime shime/eighth.rb
Last active Jun 6, 2016

Embed
What would you like to do?
my codility solutions
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
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
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
def solution(x, y, d)
(y - x) % d == 0 ? (y - x) / d :
((y - x) / d) + 1
end
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
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
def solution(a)
n = a.length + 1
total = (n * (n + 1)) / 2
total - (a.inject(:+) || 0)
end
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
You can’t perform that action at this time.