Skip to content

Instantly share code, notes, and snippets.

@rafaltrojanowski
Last active November 26, 2017 09:27

Revisions

  1. rafaltrojanowski revised this gist Nov 26, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion BinaryGaps
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    def solution(n)
    # write your code in Ruby 2.2
    binary_array = to_binary(n)

    i = 0
  2. rafaltrojanowski renamed this gist Nov 26, 2017. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions binary_gaps → BinaryGaps
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    # you can write to stdout for debugging purposes, e.g.
    # puts "this is a debug message"

    def solution(n)
    # write your code in Ruby 2.2
    binary_array = to_binary(n)
  3. rafaltrojanowski revised this gist Aug 15, 2017. No changes.
  4. rafaltrojanowski revised this gist Aug 15, 2017. No changes.
  5. rafaltrojanowski revised this gist Aug 15, 2017. No changes.
  6. rafaltrojanowski revised this gist Aug 15, 2017. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions binary_gaps
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,6 @@
    # you can write to stdout for debugging purposes, e.g.
    # puts "this is a debug message"


    # 561892, 74901729, 1376796946



    def solution(n)
    # write your code in Ruby 2.2
    binary_array = to_binary(n)
    @@ -41,13 +36,10 @@ def to_binary(n)
    arr = []

    while n > 0

    i = n % 2

    arr << i

    n = n / 2

    end

    arr
  7. rafaltrojanowski revised this gist Aug 15, 2017. 1 changed file with 6 additions and 13 deletions.
    19 changes: 6 additions & 13 deletions binary_gaps
    Original file line number Diff line number Diff line change
    @@ -8,39 +8,32 @@

    def solution(n)
    # write your code in Ruby 2.2

    binary_array = to_binary(n)

    puts binary_array.inspect


    i = 0
    j = 0

    counters = []
    max = 0
    counter = 0

    while i < binary_array.size - 1
    counter = 0

    if binary_array[i] == 1 && binary_array[i+1] == 0
    puts "I: #{i}"

    if binary_array[i] == 1 && binary_array[i+1] == 0
    j = i + 1

    while binary_array[j] == 0
    counter += 1
    puts "COUNTER: #{counter}"
    j += 1
    end

    counters << counter
    if counter > max
    max = counter
    end
    end

    i += 1
    end

    counters.max
    max
    end


  8. rafaltrojanowski revised this gist Aug 15, 2017. 1 changed file with 19 additions and 14 deletions.
    33 changes: 19 additions & 14 deletions binary_gaps
    Original file line number Diff line number Diff line change
    @@ -18,24 +18,29 @@ def solution(n)
    j = 0

    counters = []
    counter = 0

    while j < binary_array.size
    puts binary_array[j]
    while i < binary_array.size - 1
    counter = 0

    if binary_array[i] == 0
    i += 1
    j += 1
    else # 1
    j += 1
    while binary_array[j] == 0
    j +=1
    if binary_array[i] == 1 && binary_array[i+1] == 0
    puts "I: #{i}"

    j = i + 1

    while binary_array[j] == 0
    counter += 1
    puts "COUNTER: #{counter}"
    j += 1
    end
    i += 1
    puts "I: #{i}, J: #{j}"
    counters << j - i
    end

    counters << counter
    end

    i += 1
    end
    counters.max

    counters.max
    end


  9. rafaltrojanowski revised this gist Aug 15, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions binary_gaps
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    # you can write to stdout for debugging purposes, e.g.
    # puts "this is a debug message"


    # 561892, 74901729, 1376796946



    def solution(n)
    # write your code in Ruby 2.2

  10. rafaltrojanowski created this gist Aug 15, 2017.
    51 changes: 51 additions & 0 deletions binary_gaps
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # you can write to stdout for debugging purposes, e.g.
    # puts "this is a debug message"

    def solution(n)
    # write your code in Ruby 2.2

    binary_array = to_binary(n)

    puts binary_array.inspect


    i = 0
    j = 0

    counters = []

    while j < binary_array.size
    puts binary_array[j]

    if binary_array[i] == 0
    i += 1
    j += 1
    else # 1
    j += 1
    while binary_array[j] == 0
    j +=1
    end
    i += 1
    puts "I: #{i}, J: #{j}"
    counters << j - i
    end
    end
    counters.max
    end


    def to_binary(n)
    arr = []

    while n > 0

    i = n % 2

    arr << i

    n = n / 2

    end

    arr
    end