ruby 用于DBC阶段0辅导

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于DBC阶段0辅导相关的知识,希望对你有一定的参考价值。

# Requirements
#
# Create a new list
# Add an item with a quantity to the list
# Remove an item from the list
# Update quantities for items in your list
# Print the list (Consider how to make it look nice!)


# Pseudocode
#
# DEFINE 'new_list' which takes no arguments:
#   CREATE and RETURN an empty hash
#
# DEFINE 'add', which takes three arguments, item (a string) and quantity (a string), and list (a hash):
#   IF item is on list
#     PRINT message that item is already on list
#   ELSE
#     ADD key item to list with value qantity
#
# DEFINE 'remove', which takes two arguments, an item (string), and a list (hash):
#   IF item is on list
#     DELETE the element of list with key of item
#   ELSE
#     PRINT message that item isn't actually on the list
#
# DEFINE 'update', which takes three arguments, an item (string), a quantity (a string) and a list(hash):
#   IF item is on list
#     SET list at item's value to quantity
#   ELSE
#     PRINT message that the item isn't there to update
#
# DEFINE print_list, which takes one argument, a list (hash)
#   ITERATE over each element of list, for each item, quantity
#     PRINT item in uppercase, hyphen, quantity



# Initial solution
#
def new_list
  Hash.new
end

def add(item, quantity, list)
  if list[item]
    puts "#{item.upcase} is already on your list! Use command `update` to change the quantity."
  else
    list[item] = quantity
  end
end

def remove(item, list)
  if list[item]
    list.delete(item)
  else
     puts "Whoops! #{item.upcase} wasn't even on your list!"
  end
end

def update(item, quantity, list)
  if list[item]
    list[item] = quantity
  else
    puts "Whoops! #{item.upcase} wasn't on your list! Use `add` to add your item."
  end
end

def print(list)
  list.each do |item, quantity|
    puts "#{item.upcase} - #{quantity}"
  end
end


# Tests
my_list = new_list
p my_list == {}

add("Carrots", "10", my_list)
p my_list == {"Carrots" => "10"}
add("Carrots", "20", my_list)
p my_list == {"Carrots" => "10"}
add("Apples", "15", my_list)
p my_list == {"Carrots" => "10", "Apples" => "15"}

remove("Oranges", my_list)
p my_list == {"Carrots" => "10", "Apples" => "15"}
remove("Apples", my_list)
p my_list == {"Carrots" => "10"}

update("Carrots", "20", my_list)
p my_list == {"Carrots" => "20"}
update("Oranges", "20", my_list)
p my_list == {"Carrots" => "20"}


# Refactored solution w/ commandline input
#
def new_list
  {}
end

def add(item, quantity, list)
  return list[item] = quantity unless list[item]
  puts "#{item.upcase} is already on your list! Use command `update` to change the quantity."
end

def remove(item, list)
  list.delete(item) { |item| puts "Whoops! #{item.upcase} wasn't on your list!" }
end

def update(item, quantity, list)
  return list[item] = quantity if list[item]
  puts "Whoops! #{item.upcase} wasn't on your list! Use `add` to add your item."
end

def print(list)
  list.each { |item, quantity| puts "#{item.upcase} - #{quantity}" }
end

def help
  puts <<-helpdocs
    The following commands are available. Separate input with a single space, e.g.:
      add 10 chips

    add <number> <item>           ADD an item to your list:
    remove <item>                 REMOVE an item from your list
    update <item> <quantity>      UPDATE an item's quantity on your list

    help                          show HELP documentation
    quit                          exit the program
  helpdocs
end

def run
  puts "Hello! Lets get ready to shop!"
  list = new_list
  puts "Type 'help' to see a list of available commands."
  input = ""

  until input[0] == "quit"
    puts
    puts "What would you like to do?"
    input = gets.chomp.split(" ")
    puts

    case input[0]
    when 'add' then add(input[2], input[1], list)
    when 'remove' then remove(input[1], list)
    when 'update' then update(input[1], input[2], list)
    when 'help' then help; next
    else puts "Try again..."
    end

    puts; puts "*******************"
    puts "This is your list:"
    print(list)
    puts
  end
end

run

以上是关于ruby 用于DBC阶段0辅导的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

Ruby:那个看美剧不用字幕的女孩

ruby 阶段0评估 - carolineartz

ruby 阶段0评估 - carolineartz