【问题标题】:how to get the remainder and calculate the price in ruby?如何获得余数并计算红宝石价格?
【发布时间】:2020-08-02 10:12:00
【问题描述】:

我应该如何用 Ruby 编写这个?

  • 一本书是 35.00 美元
  • 4 本书一册,售价 112.00 美元(因此,如果有人购买 9 本书,则需要 259 美元)。

这是我可以写的:

book_price = 35.00 * quantity

if quantity == 4  
    book_price = 112.00
elsif quantity > 4
    book_price = (quantity / 4) * 112.00
    book_price += (quantity % 4) * 35.00
end

puts "you spend a total of $#{book_price} on books. 

【问题讨论】:

  • 你应该选择更好的变量名。我假设user_input 实际上是quantity。在任何语言中,您都可以将书籍的数量除以 4,然后将结果乘以 112。将总书籍数量除以 4 后的余数乘以 35,然后将这两个操作相加。 total_price = (user_input/4)*112.00 + (user_input % 4)*35.00.
  • 您好,您能简单介绍一下吗?您是否要达到flat discount 并且user_input 是否意味着数量?
  • 欢迎来到 SO!这有一种家庭作业的味道:“How do I ask and answer homework questions?”。您的代码没有做什么您希望它做的事情?请参阅“How to Ask”、“Stack Overflow question checklist”和“MCVE”及其所有链接页面,了解有关如何改进您的问题的更多信息。
  • 在您当前的实现中,没有理由使用 if 语句。即使quantity 等于4,ifelse 部分也会计算正确的价格。

标签: ruby if-statement module conditional-statements


【解决方案1】:

您可以使用Numeric#divmod 查找 4 本书捆绑和剩余单本书的数量,然后使用标准算术计算捆绑的价格、剩余的价格,最后计算总价。

将您的计算分解为单独的表达式,让您有机会使用变量为这些子表达式提供不言自明的名称,并让您的代码“讲故事”。

测试您的代码也是一个好主意。

def book_price(quantity)
  single_book_price =  35
  bundle_price      = 112

  bundle_size       =   4

  bundles_quantity, leftover_quantity = quantity.divmod(bundle_size)

  bundles_price   = bundles_quantity  * bundle_price
  leftovers_price = leftover_quantity * single_book_price

  bundles_price + leftovers_price
end

require 'test/unit'
class BookPriceTest < Test::Unit::TestCase
  data do
    [
      [0, 0],
      [1, 35],
      [3, 105],
      [4, 112],
      [8, 224],
      [5, 147],
      [7, 217],
      [9, 259],
  ].each_with_object({}) do |(quantity, price), data_set|
      data_set["#{quantity} books should cost #{price}"] = [quantity, price]
    end
  end
  def test_that_the_price_for_QUANTITY_books_is_PRICE
    quantity, price = data
    assert_equal price, book_price(quantity)
  end
end

运行此命令:

Loaded suite ./book_price
Started
........
Finished in 0.00079 seconds.
-------------------------------------------------------------------------------------
8 tests, 8 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
-------------------------------------------------------------------------------------
10126.58 tests/s, 10126.58 assertions/s

【讨论】:

    【解决方案2】:

    你可以这样做

    def price(num_books)
      full_price = 35.00
      sum = (num_books / 4) * 112.00 # bulk discounted price per 4
      sum += (num_books % 4) * full_price
    end
    
    price(9)
    =>259.0
    price(3)
    =>105
    price(4)
    =>112
    

    【讨论】:

    • 非常感谢。不知道为什么数量为 9 时我得到 287 美元(我不想写函数)
    • book_price = 35.00 * quantity if quantity == 4 book_price = 112.00 elsif quantity > 4 book_price = (quantity / 4) * 112.00 book_price += (quantity % 4) * 35.00 end puts "你花了图书总计 $#{book_price}。
    • 考虑使用divmodzipsum
    【解决方案3】:

    我们可以使用三元运算符和余数零逻辑来得到这个。

    quantity = 4
    discount = quantity/4 * 112
    rem = quantity%4
    
    book_price = (rem == 0 ? discount : discount + rem * 35)
    puts "you spend a total of $#{book_price} on books."
    
    you spend a total of $112 on books.
      => nil
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      • 2015-07-03
      • 2011-01-09
      • 1970-01-01
      • 2015-07-16
      • 2012-05-16
      • 1970-01-01
      相关资源
      最近更新 更多