Last active
December 28, 2015 17:49
-
-
Save adamloo85/7538933 to your computer and use it in GitHub Desktop.
Revisions
-
adamloo85 revised this gist
Nov 19, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
adamloo85 revised this gist
Nov 19, 2013 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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}." -
adamloo85 revised this gist
Nov 19, 2013 . 2 changed files with 16 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
adamloo85 revised this gist
Nov 19, 2013 . 1 changed file with 23 additions and 52 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,56 +1,27 @@ 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 [1,4,6,3,2].find_pivot -
adamloo85 created this gist
Nov 19, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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