Skip to content

Commit

Permalink
Use divmod rather than % followed by /
Browse files Browse the repository at this point in the history
Teeeny speed improvement.
  • Loading branch information
offby1 committed Jan 5, 2013
1 parent 678422e commit dc6f8e7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ruby/bag.rb
Expand Up @@ -8,7 +8,7 @@ def <=>(anOther)

attr_reader :product

def initialize (str)
def initialize(str)
@product = 1
str.downcase().each_byte do |b|
if (b >= ?a.ord and b <= ?z.ord)
Expand All @@ -34,13 +34,13 @@ def eql?(other)
end

def -(other)
if(@product % other.product != 0)
nil
else
p = Bag.new("")
p.product = @product / other.product
p
end
q, r = @product.divmod(other.product)
return nil if r > 0

p = Bag.new("")
p.product = q

p
end

protected
Expand Down

0 comments on commit dc6f8e7

Please sign in to comment.