lib/tribe_challenge/tribe_bundler.rb
def bundle(count, code)
    count = count.to_i

    # we want the bundle counts in descending order, this is important for the
    # behaviour of this method so be careful
    counts = TribeBundles.bundle_counts(code).sort.reverse

    result = { code: code, count: count, bundles: {} }
    counts.each do |value|
      modulus = count % value
      next unless modulus == 0 || counts.include?(modulus)

      num = count / value
      count = count - (value * num)
      result[:bundles][value] = num
    end

    result[:leftover] = count

    return result
  end
##
# Do the logic for finding the right bundles for a given count.
#
# @example bundle(10, :img)
#
# @param [Integer] count, the total count of items
# @param [Symbol] code, the symbol representing the bundle required.
# 
# @return [Hash], the bundles that could be found (and any leftovers) to be
# combined for the given count and code
##
Do the logic for finding the right bundles for a given count.
Do the logic for finding the right bundles for a given count .
def bundle(count, code)
    count = count.to_i
    counts = TribeBundles.bundle_counts(code).sort.reverse
    result = { code: code, count: count, bundles: {} }
    counts.each do |value|
      modulus = count % value
      next unless modulus == 0 || counts.include?(modulus)
      num = count / value
      count = count - (value * num)
      result[:bundles][value] = num
    end
    result[:leftover] = count
    return result
  end
def bundle ( count , code ) count = count . to_i counts = TribeBundles . bundle_counts ( code ) . sort . reverse result = { code : code , count : count , bundles : { } } counts . each do | value | modulus = count % value next unless modulus == 0 || counts . include? ( modulus ) num = count / value count = count - ( value * num ) result [ :bundles ] [ value ] = num end result [ :leftover ] = count return result end
Do the logic for finding the right bundles for a given count.
Do the logic for finding the right bundles for a given count .
# we want the bundle counts in descending order, this is important for the # behaviour of this method so be careful
, the bundles that could be found (and any leftovers) to be combined for the given count and code
the bundles that could be found ( and any leftovers ) to be combined for the given count and code
Hash
count
code
code, the symbol representing the bundle required.
code the symbol representing the bundle required .
example
None