Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. def count_zeros arr
  2. run = true
  3. count = 0
  4. temp = ""
  5. while run
  6. if arr[count].nil?
  7. temp = ""
  8. run = false
  9. elsif !(arr[count] == "1")
  10. temp += arr[count]
  11. else
  12. run = false
  13. end
  14. count += 1
  15. end
  16.  
  17. temp.size
  18. end
  19.  
  20. def binary_gap num
  21. result = []
  22. temp = num.to_s(2).split('')
  23. temp.each_with_index do |x, i|
  24. if x == "1"
  25. result << count_zeros(temp[i+1..-1])
  26. end
  27. end
  28. result.max
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement