-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added array extensions to make problem 1 more readable.
- Loading branch information
John Dzak
committed
Nov 10, 2008
1 parent
d907dad
commit aa06098
Showing
4 changed files
with
27 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Array | ||
def unique | ||
self & self | ||
end | ||
|
||
def sum | ||
self.inject{|sum, num| sum + num} | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'array' | ||
|
||
describe Array do | ||
it "should return only unique items" do | ||
[1,3,3,4,4].unique.should == [1,3,4] | ||
end | ||
it "should return the sum of the array" do | ||
[1,3,9].sum.should == 13 | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
multiples = 3.multiples(1..9) | ||
multiples.should == [3,6,9] | ||
end | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
require 'integer' | ||
require 'array' | ||
|
||
multiples_of_3 = 3.multiples(1..999) | ||
multiples_of_5 = 5.multiples(1..999) | ||
multiples_of_3.each { |num| multiples_of_3.delete(num) if multiples_of_5.include?(num) } | ||
|
||
sum = (multiples_of_3 + multiples_of_5).inject {|sum, multiple| sum + multiple } | ||
puts sum | ||
multiples = multiples_of_3 + multiples_of_5 | ||
unique_multiples = multiples.unique | ||
|
||
sum = unique_multiples.sum | ||
|
||
puts "The answer is #{sum}" | ||
|