ruby 用于重构面包店挑战的辅导/ GPS资源。可能的解决方案(高级)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于重构面包店挑战的辅导/ GPS资源。可能的解决方案(高级)相关的知识,希望对你有一定的参考价值。

# U2.W5: Bakery Challenge GPS

# U2.W5: The Bakery Challenge (GPS 2.1)

# Your Names
# 1)
# 2)

def bakery_num(num_of_people, fav_food)
  #Release 2
  my_list = {"pie" => 8, "cake" => 6, "cookie" => 1}
  pie_qty = 0
  cake_qty = 0
  cookie_qty = 0

  has_fave = false

  # Release 3
  my_list.each_key do |k|
  if k == fav_food
  has_fave = true
  fav_food = k
  end
  end

  # Release 4
  if has_fave == false
  raise ArgumentError.new("You can't make that food")
  else

  # Release 5
  fav_food_qty = my_list.values_at(fav_food)[0]

  # Release 6
  if num_of_people % fav_food_qty == 0
    num_of_food = num_of_people / fav_food_qty
    return "You need to make #{num_of_food} #{fav_food}(s)."
  else num_of_people % fav_food_qty != 0

  # Release 7
    while num_of_people > 0

  # Release 8
    if num_of_people / my_list["pie"] > 0
    pie_qty = num_of_people / my_list["pie"]
    num_of_people = num_of_people % my_list["pie"]
    elsif num_of_people / my_list["cake"] > 0
    cake_qty = num_of_people / my_list["cake"]
    num_of_people = num_of_people % my_list["cake"]
    else
    cookie_qty = num_of_people
    num_of_people = 0
    end
    end
    return "You need to make #{pie_qty} pie(s), #{cake_qty} cake(s), and #{cookie_qty} cookie(s)."
    end
  end
end


#-----------------------------------------------------------------------------------------------------
# Release 1: DRIVER TEST CODE
# DO NOT MODIFY ANYTHING BELOW THIS LINE (except in the section at the bottom)
# These are the tests to ensure it's working.
# These should all print true if the method is working properly (except for the last one).
p bakery_num(24, "cake") == "You need to make 4 cake(s)."
p bakery_num(41, "pie") == "You need to make 5 pie(s), 0 cake(s), and 1 cookie(s)."
p bakery_num(24, "cookie") == "You need to make 24 cookie(s)."
p bakery_num(4, "pie") == "You need to make 0 pie(s), 0 cake(s), and 4 cookie(s)."
p bakery_num(130, "pie") == "You need to make 16 pie(s), 0 cake(s), and 2 cookie(s)."
# p bakery_num(3, "apples") # this will raise an ArgumentError

# You SHOULD change this driver test code. Why? Because it doesn't make sense.
p bakery_num(41, "cake") == "You need to make 5 pie(s), 0 cake(s), and 1 cookie(s)." # WHAAAAAT? I thought I said I wanted cake!


#  Reflection



TREAT_SERVINGS = {"pie" => 8, "cake" => 6, "cookie" => 1}

def bakery_num(servings, favorite_food)
  raise ArgumentError.new("You can't make that food") unless TREAT_SERVINGS.key?(favorite_food)

  TREAT_SERVINGS.keys.each_with_object(order_quantity = {}) { |food, order|  order[food] = 0 }
  
  if servings % TREAT_SERVINGS[favorite_food] == 0
    return "You need to make #{servings/TREAT_SERVINGS[favorite_food]} #{favorite_food}(s)."
  end

  foods = TREAT_SERVINGS.keys.drop_while { |food| food != favorite_food }

  foods.take_while do |food|
    order_quantity[food] = servings / TREAT_SERVINGS[food]
    servings %= TREAT_SERVINGS[food]
  end
  
  reversed_arrays = order_quantity.to_a.map(&:reverse)
  formatter = reversed_arrays.map { |quantity, food| [quantity, "#{food}(s)"].join(' ') }
  "You need to make #{formatter[0..-2].join(", ")}, and #{formatter.last}."
end





#-----------------------------------------------------------------------------------------------------
# DRIVER TEST CODE

p bakery_num(24, "cake") == "You need to make 4 cake(s)."
p bakery_num(41, "pie") == "You need to make 5 pie(s), 0 cake(s), and 1 cookie(s)."
p bakery_num(24, "cookie") == "You need to make 24 cookie(s)."
p bakery_num(4, "pie") == "You need to make 0 pie(s), 0 cake(s), and 4 cookie(s)."
p bakery_num(130, "pie") == "You need to make 16 pie(s), 0 cake(s), and 2 cookie(s)."
# p bakery_num(3, "apples") # this will raise an ArgumentError

p bakery_num(41, "cake") == "You need to make 0 pie(s), 6 cake(s), and 5 cookie(s)." 
p bakery_num(46, "pie") == "You need to make 5 pie(s), 1 cake(s), and 0 cookie(s)." 
p bakery_num(47, "pie") == "You need to make 5 pie(s), 1 cake(s), and 1 cookie(s)."





#clerly there is way too much going on for a single method, but in an effort to keep with the challenge guidelines, here it stands
TREAT_SERVINGS = {"pie" => 8, "cake" => 6, "cookie" => 1} #making this a constant, in order from most servings to least (that is important)

def bakery_num(servings, favorite_food)
  raise ArgumentError.new("You can't make that food") unless TREAT_SERVINGS.key?(favorite_food)

  #instead of initializing them individually, we cna put it into a hash and easily associate with the servings hash
  order_quantity = {"pie" => 0, "cake" => 0, "cookie" => 0}
  fave_food_servings = TREAT_SERVINGS[favorite_food]

  if servings % fave_food_servings == 0
    return "You need to make #{servings/fave_food_servings} #{favorite_food}(s)."
  end

  # we know that we'll only have to fill the order with foods that have serving sizes of 
  # equal to or less than favorite_food. so, if the constant TREAT_SERVINGS is always in 
  # order by most servings : least_servings, this should return
  # an array of the treats that will potentially be part of the order
  foods = TREAT_SERVINGS.keys.drop_while { |food| food != favorite_food }

  # take_while will continue iterating until it evaluates to false or nil
  # so the last line in the block is asking if servings still have to be allocated
  foods.take_while do |food|
    order_quantity[food] = servings / TREAT_SERVINGS[food]
    servings %= TREAT_SERVINGS[food]
  end

  return "You need to make #{order_quantity["pie"]} pie(s), #{order_quantity["cake"]} cake(s), and #{order_quantity["cookie"]} cookie(s)."
end



#-----------------------------------------------------------------------------------------------------
# DRIVER TEST CODE

p bakery_num(24, "cake") == "You need to make 4 cake(s)."
p bakery_num(41, "pie") == "You need to make 5 pie(s), 0 cake(s), and 1 cookie(s)."
p bakery_num(24, "cookie") == "You need to make 24 cookie(s)."
p bakery_num(4, "pie") == "You need to make 0 pie(s), 0 cake(s), and 4 cookie(s)."
p bakery_num(130, "pie") == "You need to make 16 pie(s), 0 cake(s), and 2 cookie(s)."
# p bakery_num(3, "apples") # this will raise an ArgumentError

p bakery_num(41, "cake") == "You need to make 0 pie(s), 6 cake(s), and 5 cookie(s)." 
p bakery_num(46, "pie") == "You need to make 5 pie(s), 1 cake(s), and 0 cookie(s)." 
p bakery_num(47, "pie") == "You need to make 5 pie(s), 1 cake(s), and 1 cookie(s)."



以上是关于ruby 用于重构面包店挑战的辅导/ GPS资源。可能的解决方案(高级)的主要内容,如果未能解决你的问题,请参考以下文章

ruby 图像模糊挑战的重构代码(包括曼哈顿距离)

ruby 为学生提供辅导资源DBC P0准备

ruby 为学生提供辅导资源DBC P0准备

ruby 用于DBC阶段0辅导

css 根据@wernull的想法重构WPD面包屑吧

满意给高分,现有一gps模块,一51单片机。想做成能显示所在地经纬度的东西只需要知道经纬度数据即可求辅导