Skip to content

Instantly share code, notes, and snippets.

@adamloo85
Last active December 28, 2015 17:49
Show Gist options
  • Save adamloo85/7538933 to your computer and use it in GitHub Desktop.
Save adamloo85/7538933 to your computer and use it in GitHub Desktop.

Revisions

  1. adamloo85 revised this gist Nov 19, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pivot.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #README
    #Run by typing "ruby (filename)". Drive code is already included.
    #This script adds a method "find_pivot" to the array class which finds the pivot point of any array
    #and returns the index of that point if it exists or -1 otherwise.

  2. adamloo85 revised this gist Nov 19, 2013. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion pivot.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    #README
    #This script adds a method "find_pivot" to the array class which finds the pivot point of any array
    #and returns the index of that point if it exists or -1 otherwise.

    class Array
    def find_pivot
    raise NoMethodError, 'Wrong type of input!' if self.class != Array
    @@ -24,4 +28,5 @@ def find_pivot
    end
    end

    [1,4,6,3,2].find_pivot
    p "[1,4,6,3,2].find_pivot yields " + "#{[1,4,6,3,2].find_pivot}."
    p "[1,4,6,7,3,2].find_pivot yields " + "#{[1,4,6,7,3,2].find_pivot}."
  3. adamloo85 revised this gist Nov 19, 2013. 2 changed files with 16 additions and 0 deletions.
    File renamed without changes.
    16 changes: 16 additions & 0 deletions pivot_tests.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    require_relative 'pivot.rb'
    require "test/unit"

    class FindPivot < Test::Unit::TestCase
    def test_structure
    assert_equal Fixnum, [1,2,3,4,5].find_pivot.class
    assert_raise NoMethodError do
    "adam".find_pivot
    end
    end

    def test_logic
    assert_equal 2, [1,4,6,3,2].find_pivot
    assert_equal -1, [1,4,6,7,3,2].find_pivot
    end
    end
  4. adamloo85 revised this gist Nov 19, 2013. 1 changed file with 23 additions and 52 deletions.
    75 changes: 23 additions & 52 deletions pivot_finder.rb
    Original file line number Diff line number Diff line change
    @@ -1,56 +1,27 @@
    require 'rubygems'
    require 'shoulda-context'

    def pivot_finder(array)
    raise NoMethodError, 'Wrong type of input!' if array.class != Array

    low = 0
    high = array.length - 1
    mid = (low + high) / 2
    searches = 0

    until searches == (array.length / 2)
    searches += 1
    left_sum = array[low..mid - 1].inject {|sum, i| sum + i}
    right_sum = array[mid + 1..high].inject{|sum, i| sum + i}

    if left_sum == right_sum
    return mid
    elsif right_sum > left_sum
    mid += 1
    elsif left_sum > right_sum
    mid -= 1
    end
    end
    return -1
    end

    ## Unit Tests
    class FindPivot < Test::Unit::TestCase
    context "method structure" do
    should "return a Fixnum" do
    assert_equal Fixnum, pivot_finder([1,2,3,4,5]).class
    end

    should "raise error with wrong arguments" do
    assert_raise NoMethodError do
    pivot_finder("adam")
    class Array
    def find_pivot
    raise NoMethodError, 'Wrong type of input!' if self.class != Array

    low = 0
    high = self.length - 1
    mid = (low + high) / 2
    searches = 0

    until searches == (self.length / 2)
    searches += 1
    left_sum = self[low..mid - 1].inject {|sum, i| sum + i}
    right_sum = self[mid + 1..high].inject{|sum, i| sum + i}

    if left_sum == right_sum
    return mid
    elsif right_sum > left_sum
    mid += 1
    elsif left_sum > right_sum
    mid -= 1
    end
    end
    return -1
    end
    end

    context "method logic" do
    setup do
    @has_pivot = [1,4,6,3,2]
    @no_pivot = [1,4,6,7,3,2]
    end

    should "return index of pivot point if there is one" do
    assert_equal 2, pivot_finder(@has_pivot)
    end

    should "return -1 if there is no pivot point" do
    assert_equal -1, pivot_finder(@no_pivot)
    end
    end
    end
    [1,4,6,3,2].find_pivot
  5. adamloo85 created this gist Nov 19, 2013.
    56 changes: 56 additions & 0 deletions pivot_finder.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    require 'rubygems'
    require 'shoulda-context'

    def pivot_finder(array)
    raise NoMethodError, 'Wrong type of input!' if array.class != Array

    low = 0
    high = array.length - 1
    mid = (low + high) / 2
    searches = 0

    until searches == (array.length / 2)
    searches += 1
    left_sum = array[low..mid - 1].inject {|sum, i| sum + i}
    right_sum = array[mid + 1..high].inject{|sum, i| sum + i}

    if left_sum == right_sum
    return mid
    elsif right_sum > left_sum
    mid += 1
    elsif left_sum > right_sum
    mid -= 1
    end
    end
    return -1
    end

    ## Unit Tests
    class FindPivot < Test::Unit::TestCase
    context "method structure" do
    should "return a Fixnum" do
    assert_equal Fixnum, pivot_finder([1,2,3,4,5]).class
    end

    should "raise error with wrong arguments" do
    assert_raise NoMethodError do
    pivot_finder("adam")
    end
    end
    end

    context "method logic" do
    setup do
    @has_pivot = [1,4,6,3,2]
    @no_pivot = [1,4,6,7,3,2]
    end

    should "return index of pivot point if there is one" do
    assert_equal 2, pivot_finder(@has_pivot)
    end

    should "return -1 if there is no pivot point" do
    assert_equal -1, pivot_finder(@no_pivot)
    end
    end
    end