Skip to content

Instantly share code, notes, and snippets.

@rantler
Forked from davingee/gist:8278b9f6c66f0c53f5e9
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rantler/40379dc4d5817bff283b to your computer and use it in GitHub Desktop.
Save rantler/40379dc4d5817bff283b to your computer and use it in GitHub Desktop.
Fork of SumOfEach with "equals5" solution
# gem install awesome_print
# gem install benchmark
# gem install pry
require 'awesome_print'
require 'pry'
require 'benchmark'
class SumOfEach
attr_accessor :number_to_equal, :array, :answers, :array_count, :to_remove, :answered
def initialize( args = { } )
self.number_to_equal = args.fetch( :number_to_equal, 30 )
self.array = ( args.fetch( :from, -50 )..args.fetch( :to, 50 ) ).to_a
self.array_count = array.count
self.answers = { }
self.to_remove = []
self.answered = false
end
def self.find( args = { } )
number_to_equal = args.fetch(:number_to_equal, 5000)
from = args.fetch(:from, -5000)
to = args.fetch(:to, 5000)
sum_of_each = SumOfEach.new( number_to_equal: number_to_equal, from: from, to: to )
Benchmark.bm do |x|
x.report('equals1') {
sum_of_each.equals1
sum_of_each.answers[ :count ] = sum_of_each.answers.count
}
end
ap sum_of_each.answers[ :count ]
sum_of_each = SumOfEach.new( number_to_equal: number_to_equal, from: from, to: to )
Benchmark.bm do |x|
x.report('equals2') {
sum_of_each.equals2
sum_of_each.answers[ :count ] = sum_of_each.answers.count
}
end
ap sum_of_each.answers[ :count ]
# ap sum_of_each.answers
# sum_of_each = SumOfEach.new( number_to_equal: number_to_equal, from: from, to: to )
# Benchmark.bm do |x|
# x.report('equals3') {
# sum_of_each.equals3
# sum_of_each.answers[ :count ] = sum_of_each.answers.count
# }
# end
# ap sum_of_each.answers[ :count ]
sum_of_each = SumOfEach.new( number_to_equal: number_to_equal, from: from, to: to )
Benchmark.bm do |x|
x.report('equals4') {
sum_of_each.equals4
sum_of_each.answers[ :count ] = sum_of_each.answers.count
}
end
ap sum_of_each.answers[ :count ]
sum_of_each = SumOfEach.new( number_to_equal: number_to_equal, from: from, to: to )
Benchmark.bm do |x|
x.report('equals5') {
sum_of_each.equals5
sum_of_each.answers[ :count ] = sum_of_each.answers.count
}
end
ap sum_of_each.answers[ :count ]
end
def equals1
remove_numbers_from_array_logic
( array - to_remove ).map.with_index do | n, i |
answers[ "#{ n }_plus_#{ number_to_equal - n }" ] = number_to_equal if array[ i, array_count].include?( number_to_equal - n )
end
end
def equals2
remove_numbers_from_array_logic
( array - to_remove ).map.with_index{ | n, i | array[ i, array_count ].each{ | n2 | answers[ "#{ n }_plus_#{ n2 }" ] = number_to_equal if n + n2 == number_to_equal } }
end
def equals3
array.each_with_index do | number, index |
array.each do |number_two|
answers[ "#{ number }_plus_#{ number_two }" ] = number_to_equal if sum_equals_number_to_check?( number_two, number ) && not_already_awnsered?( number, number_two )
end
end
end
def equals4 # 50000
remove_numbers_from_array_logic
( array - to_remove ).map.with_index{ | n, i |
array[ i, array_count ].map{ | n2 |
answers[ number_to_equal ] = [ n, n2 ] if n + n2 == number_to_equal
}
}
end
def equals5
found = {}
array.each do |number|
if found[number_to_equal - number + 1] == true
answers["#{number}_plus_#{number_to_equal - number}"] = [number, number_to_equal - number]
end
found[number + 1] = true
end
end
# Same as equals5 but runs all by itself without needing anything else
def equals6
numbers = [-5, 15, 1, 4, 45, 6, 10, 0, 8]
target = 10
found = {}
matches = []
numbers.each do |number|
if found[target - number + 1] == true
matches << [number, target - number]
end
found[number + 1] = true
end
puts matches
end
private
def not_already_awnsered?( number, number_two )
true if answers[ "#{ number }_plus_#{ number_two }" ].nil? && answers[ "#{ number_two }_plus_#{ number }" ].nil?
end
def sum_equals_number_to_check?( number_two, number )
number + number_two == number_to_equal
end
def remove_numbers_from_array_logic
array.reverse.each do |n|
if n + n > number_to_equal
self.to_remove << n
else
break
end
end
array.each do |n|
if n + n > number_to_equal
self.to_remove << n
else
break
end
end
# there are more one can remove; how?
# array.each do |n|
#
# end
#
# ( 1..10 ).to_a.each do | devisor |
# divisable_number = number_to_equal / devisor.to_f
# if devisor.to_i != devisor
# a = array.select { |x| x%devisor == 0 }
#
# array.collection{ |n| n}
# end
# end
# remove numbers from array logic ?? if number_to_equal is 10 number_to_equal / 9 .remove from array
end
end
SumOfEach.find
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment