Skip to content

Instantly share code, notes, and snippets.

@clarkngo
Created February 19, 2019 18:33
Show Gist options
  • Save clarkngo/5fb6bf6a0d7ce9903d40e3e5436f3140 to your computer and use it in GitHub Desktop.
Save clarkngo/5fb6bf6a0d7ce9903d40e3e5436f3140 to your computer and use it in GitHub Desktop.
plus minus plus plus
https://www.codewars.com/kata/plus-minus-plus-plus-dot-dot-dot-count/train/ruby
def catch_sign_change(arr)
count = 0
if arr == []
return count
end
if arr[0] >= 0
previous_sign = "positive"
else
previous_sign = "negative"
end
for i in 1...arr.length
if arr[i] >= 0
current_sign = "positive"
else
current_sign = "negative"
end
if current_sign != previous_sign
count = count + 1
end
previous_sign = current_sign
end
count
end
# other solution
def catch_sign_change(arr)
arr.each_cons(2).count { |x, y| (x >= 0) != (y >= 0) }
end
# other solution
def catch_sign_change(arr)
arr.each_cons(2).count{ |x, y| x.negative? != y.negative? }
end
@MetFerel
Copy link

Hello I'm Piter It's wery well I from Russian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment