Решение на Трета задача от Станимира Христова

Обратно към всички решения

Към профила на Станимира Христова

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 19 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
module Promotions
def get_one_free(quantity,name)
old_price=@cart[name][0]
new_price=old_price
all_products=@cart[name][1]
free_products=all_products/quantity[0]
if (free_products!=0)
new_price=(all_products-free_products)*old_price
end
return new_price
end
def package(pack,name)
package=pack[0].to_a
quantity=package[0]
percentage=package[1]*0.01
products_in_cart=@cart[name][1]
if(products_in_cart<quantity)
return @cart[name][0]*@cart[name][1]
else return (products_in_cart/quantity)*@cart[name][0]*percentage
end
end
def treshold(promotion,name)
discount=promotion[0].to_a
quantity=discount[0]
percent=discount[1]*0.01
all_products=@cart[name][1]
if(all_products<quantity)
return @cart[name][0]*@cart[name][1]
else return quantity*@cart[name][0]+(all_products-quantity)*@cart[name][0]*percent
end
end
end
module Coupons
def percent(discount)
sum=0
@cart.values.each do |value|
sum+=value[0]
end
return sum=sum-sum*discount*0.01
end
def amount(discount)
sum=0
@cart.values.each do |value|
sum+=value[0]
end
if (sum>discount)
return sum-discount
else return 0
end
end
end
class Inventory
include Promotions
include Coupons
$storage={}
$coupons={}
$sum_with_coupon=0
def register (name,price,promotions={})
@price=price.to_d
if(name.lenght>40 | @price<0.01 | @price>999.9 |storage.has_key?(name))
raise "Invalid parameters passed."
else storage[name]=Array(@price,promotions)
end
end
def new_cart()
@cart={}
end
def add(add_product_name,quantity)
price_and_quantity = []
if(storage[add_product_name].nil? | quantity<=0)
raise "Ivalid parameters passed."
else price_and_quantity[0]=storage[add_product_name][0]
price_and_quantity[1]= quantity
if(@cart.has_key?(add_product_name))
@cart[add_product_name][1]+=quantity
else @cart[add_product_name] = price_and_quantity
end
end
end
def total()
total_sum=0
@cart.each do |key,value|
apply_promotion(key)
total_sum+=value[0]
end
return total_sum-sum_with_coupon
end
def apply_promotion(product_name)
promotion={}
if(storage[product_name][1].nil?)
@cart[product_name][0]=@cart[product_name][0]*@cart[product_name][1]
else promotion=storage[product_name][1]
end
set_promotion(promotion,product_name)
end
def set_promotion(promotion,product_name)
if(promotion.keys.to_s == 'get_one_free')
@cart[product_name][0]= get_one_free(promotion.values,product_name)
else if(promotion.keys.to_s=='package')
@cart[product_name][0]=package(promotion.values,product_name)
else @cart[product_name][0]=treshold(promotion.values,product_name)
end
end
end
def register_coupon(coupon_name,coupon)
coupons[coupon_name]=coupon
end
def use(coupon_name)
coupon=coupons[coupon_name].to_a
use_coupon=coupon[0]
discount=coupon[1]
if(coupon=='percent')
sum_with_coupon=percent(discount)
else sum_with_coupon=amount(discount)
end
end
end

Лог от изпълнението

FFFFFFFFFFFFFFFFFFF

Failures:

  1) Inventory with no discounts can tell the total price of all products
     Failure/Error: inventory.register 'The Best of Coltrane CD', '1.99'
     NoMethodError:
       undefined method `lenght' for "The Best of Coltrane CD":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:39:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Inventory with no discounts has some constraints on prices and counts
     Failure/Error: inventory.register 'Existing', '1.00'
     NoMethodError:
       undefined method `lenght' for "Existing":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:48:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Inventory with no discounts can print an invoice
     Failure/Error: inventory.register 'Green Tea',    '0.79'
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:65:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Inventory with a 'buy X, get one free' promotion grants every nth item for free
     Failure/Error: inventory.register 'Gum', '1.00', get_one_free: 4
     NoMethodError:
       undefined method `lenght' for "Gum":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:89:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Inventory with a 'buy X, get one free' promotion grants 2 items free, when 8 purchased and every 3rd is free
     Failure/Error: inventory.register 'Gum', '1.00', get_one_free: 3
     NoMethodError:
       undefined method `lenght' for "Gum":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:97:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Inventory with a 'buy X, get one free' promotion shows the discount in the invoice
     Failure/Error: inventory.register 'Green Tea', '1.00', get_one_free: 3
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:105:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Inventory with a '% off for every n' promotion gives % off for every group of n
     Failure/Error: inventory.register 'Sandwich', '1.00', package: {4 => 20}
     NoMethodError:
       undefined method `lenght' for "Sandwich":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:128:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Inventory with a '% off for every n' promotion does not discount for extra items, that don't fit in a group
     Failure/Error: inventory.register 'Sandwich', '0.50', package: {4 => 10}
     NoMethodError:
       undefined method `lenght' for "Sandwich":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:138:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Inventory with a '% off for every n' promotion shows the discount in the invoice
     Failure/Error: inventory.register 'Green Tea', '1.00', package: {4 => 10}
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:148:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) Inventory with a '% off of every item after the nth' promotion gives a discount for every item after the nth
     Failure/Error: inventory.register 'Coke', '2.00', threshold: {5 => 10}
     NoMethodError:
       undefined method `lenght' for "Coke":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:171:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) Inventory with a '% off of every item after the nth' promotion does not give a discount if there are no more than n items in the cart
     Failure/Error: inventory.register 'Coke', '1.00', threshold: {10 => 20}
     NoMethodError:
       undefined method `lenght' for "Coke":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:178:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Inventory with a '% off of every item after the nth' promotion shows the discount in the ivnoice
     Failure/Error: inventory.register 'Green Tea', '1.00', threshold: {10 => 10}
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:191:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Inventory with a '% off' coupon gives % off of the total
     Failure/Error: inventory.register 'Tea', '1.00'
     NoMethodError:
       undefined method `lenght' for "Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:214:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Inventory with a '% off' coupon applies the coupon discount after product promotions
     Failure/Error: inventory.register 'Tea', '1.00', get_one_free: 6
     NoMethodError:
       undefined method `lenght' for "Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:224:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Inventory with a '% off' coupon shows the discount in the invoice
     Failure/Error: inventory.register 'Green Tea', '1.00'
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:234:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Inventory with an 'amount off' coupon subtracts the amount form the total
     Failure/Error: inventory.register 'Tea', '1.00'
     NoMethodError:
       undefined method `lenght' for "Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:255:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Inventory with an 'amount off' coupon does not result in a negative total
     Failure/Error: inventory.register 'Tea', '1.00'
     NoMethodError:
       undefined method `lenght' for "Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:265:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Inventory with an 'amount off' coupon shows the discount in the invoice
     Failure/Error: inventory.register 'Green Tea', '1.00'
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:275:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) Inventory with multiple discounts can print an invoice
     Failure/Error: inventory.register 'Green Tea',    '2.79', get_one_free: 2
     NoMethodError:
       undefined method `lenght' for "Green Tea":String
     # /tmp/d20111115-5847-1oebcj0/solution.rb:74:in `register'
     # /tmp/d20111115-5847-1oebcj0/spec.rb:296:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.55266 seconds
19 examples, 19 failures

Failed examples:

rspec /tmp/d20111115-5847-1oebcj0/spec.rb:38 # Inventory with no discounts can tell the total price of all products
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:47 # Inventory with no discounts has some constraints on prices and counts
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:64 # Inventory with no discounts can print an invoice
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:88 # Inventory with a 'buy X, get one free' promotion grants every nth item for free
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:96 # Inventory with a 'buy X, get one free' promotion grants 2 items free, when 8 purchased and every 3rd is free
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:104 # Inventory with a 'buy X, get one free' promotion shows the discount in the invoice
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:127 # Inventory with a '% off for every n' promotion gives % off for every group of n
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:137 # Inventory with a '% off for every n' promotion does not discount for extra items, that don't fit in a group
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:147 # Inventory with a '% off for every n' promotion shows the discount in the invoice
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:170 # Inventory with a '% off of every item after the nth' promotion gives a discount for every item after the nth
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:177 # Inventory with a '% off of every item after the nth' promotion does not give a discount if there are no more than n items in the cart
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:190 # Inventory with a '% off of every item after the nth' promotion shows the discount in the ivnoice
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:213 # Inventory with a '% off' coupon gives % off of the total
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:223 # Inventory with a '% off' coupon applies the coupon discount after product promotions
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:233 # Inventory with a '% off' coupon shows the discount in the invoice
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:254 # Inventory with an 'amount off' coupon subtracts the amount form the total
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:264 # Inventory with an 'amount off' coupon does not result in a negative total
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:274 # Inventory with an 'amount off' coupon shows the discount in the invoice
rspec /tmp/d20111115-5847-1oebcj0/spec.rb:295 # Inventory with multiple discounts can print an invoice

История (1 версия и 0 коментара)

Станимира обнови решението на 06.11.2011 15:42 (преди около 13 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+module Promotions
+ def get_one_free(quantity,name)
+ old_price=@cart[name][0]
+ new_price=old_price
+ all_products=@cart[name][1]
+ free_products=all_products/quantity[0]
+ if (free_products!=0)
+ new_price=(all_products-free_products)*old_price
+ end
+ return new_price
+ end
+
+ def package(pack,name)
+ package=pack[0].to_a
+ quantity=package[0]
+ percentage=package[1]*0.01
+ products_in_cart=@cart[name][1]
+ if(products_in_cart<quantity)
+ return @cart[name][0]*@cart[name][1]
+ else return (products_in_cart/quantity)*@cart[name][0]*percentage
+ end
+ end
+
+ def treshold(promotion,name)
+ discount=promotion[0].to_a
+ quantity=discount[0]
+ percent=discount[1]*0.01
+ all_products=@cart[name][1]
+ if(all_products<quantity)
+ return @cart[name][0]*@cart[name][1]
+ else return quantity*@cart[name][0]+(all_products-quantity)*@cart[name][0]*percent
+ end
+ end
+
+end
+
+
+module Coupons
+
+ def percent(discount)
+ sum=0
+ @cart.values.each do |value|
+ sum+=value[0]
+ end
+ return sum=sum-sum*discount*0.01
+ end
+
+ def amount(discount)
+ sum=0
+ @cart.values.each do |value|
+ sum+=value[0]
+ end
+ if (sum>discount)
+ return sum-discount
+ else return 0
+ end
+ end
+end
+
+class Inventory
+
+ include Promotions
+ include Coupons
+
+ $storage={}
+ $coupons={}
+ $sum_with_coupon=0
+
+ def register (name,price,promotions={})
+ @price=price.to_d
+ if(name.lenght>40 | @price<0.01 | @price>999.9 |storage.has_key?(name))
+ raise "Invalid parameters passed."
+ else storage[name]=Array(@price,promotions)
+ end
+ end
+
+ def new_cart()
+ @cart={}
+ end
+
+ def add(add_product_name,quantity)
+ price_and_quantity = []
+ if(storage[add_product_name].nil? | quantity<=0)
+ raise "Ivalid parameters passed."
+ else price_and_quantity[0]=storage[add_product_name][0]
+ price_and_quantity[1]= quantity
+ if(@cart.has_key?(add_product_name))
+ @cart[add_product_name][1]+=quantity
+ else @cart[add_product_name] = price_and_quantity
+ end
+ end
+ end
+
+ def total()
+ total_sum=0
+ @cart.each do |key,value|
+ apply_promotion(key)
+ total_sum+=value[0]
+ end
+ return total_sum-sum_with_coupon
+ end
+
+ def apply_promotion(product_name)
+ promotion={}
+ if(storage[product_name][1].nil?)
+ @cart[product_name][0]=@cart[product_name][0]*@cart[product_name][1]
+ else promotion=storage[product_name][1]
+ end
+ set_promotion(promotion,product_name)
+ end
+
+ def set_promotion(promotion,product_name)
+ if(promotion.keys.to_s == 'get_one_free')
+ @cart[product_name][0]= get_one_free(promotion.values,product_name)
+ else if(promotion.keys.to_s=='package')
+ @cart[product_name][0]=package(promotion.values,product_name)
+ else @cart[product_name][0]=treshold(promotion.values,product_name)
+ end
+ end
+ end
+
+ def register_coupon(coupon_name,coupon)
+ coupons[coupon_name]=coupon
+ end
+
+ def use(coupon_name)
+ coupon=coupons[coupon_name].to_a
+ use_coupon=coupon[0]
+ discount=coupon[1]
+ if(coupon=='percent')
+ sum_with_coupon=percent(discount)
+ else sum_with_coupon=amount(discount)
+ end
+ end
+end