hash最小值过滤算法
程序员文章站
2022-05-01 09:21:22
...
[["数量","包装","价格"],["数量","包装","价格"],["数量","包装","价格"]]
要求取出相同包装数量的最低价格结果集
例子:[[10,"kg",200],[10,"kg",300],[20,"kg",100],[20,"kg",200],[30,"kg",200]]
StockChemicalPrice 模型下有一个字断记录报价方案:
{levels: "5,10,100", prices: "100,200,300", fees: "0,0,10"}
解析报价方案,取同包装最低报价方案
要求取出相同包装数量的最低价格结果集
例子:[[10,"kg",200],[10,"kg",300],[20,"kg",100],[20,"kg",200],[30,"kg",200]]
results = [[10,"kg",200],[10,"kg",300],[20,"kg",100],[20,"kg",200],[30,"kg",200]] result_hash = {} results.each do |result| result_hash[result[0]] = result unless result_hash[package_unit].present? && result_hash[result[0]][0] > result[0] end results = result_hash.values
StockChemicalPrice 模型下有一个字断记录报价方案:
{levels: "5,10,100", prices: "100,200,300", fees: "0,0,10"}
解析报价方案,取同包装最低报价方案
def get_stock_hash(source_entity_type) return_result = [] purity_stock = StockChemicalPrice.standard_package.where(active: 1, stock_chemical_id: self.id, source_entity_type: source_entity_type).group_by(&:purity) purity_stock.each do |purity,stocks| stocks.group_by(&:package_unit).each do |package_unit,stock| results = [] stock.each do |data| purity = purity package_unit = package_unit package_unit_int = data.package_unit_before_type_cast stock_id = data.id levels = data.hash_rules[:levels].split(',') prices = data.hash_rules[:prices].split(',') levels.each_with_index do |l,i| results << [stock_id,"#{l}#{package_unit}",purity,package_unit,prices[i],l,package_unit_int] end if levels.uniq != levels result_hash = {} results.each do |result| result_hash[result[4]] = result unless result_hash[package_unit].present? && result_hash[result[4]][4] > result[4] end results = result_hash.values end end return_result += results end end return return_result end
上一篇: 检查数组是否包含某个值的方法
下一篇: 各云服务平台的架构图