简体   繁体   中英

Turning an array into a hash, then, adding a key => value and increment when key is present

After trying again, I think this will need an each method with a do..end. Below you will see the whole test spec to give you a breakdown of what 'cart' translates to and I have included my code so far along with the errors.

The cart starts as an array of individual items. Translate it into a hash that includes the counts for each item with the consolidate_cart method.

describe "Grocer" do
  let(:items) do
    [
      {"AVOCADO" => {:price => 3.00, :clearance => true}},
      {"KALE" => {:price => 3.00, :clearance => false}},
      {"BLACK_BEANS" => {:price => 2.50, :clearance => false}},
      {"ALMONDS" => {:price => 9.00, :clearance => false}},
      {"TEMPEH" => {:price => 3.00, :clearance => true}},
      {"CHEESE" => {:price => 6.50, :clearance => false}},
      {"BEER" => {:price => 13.00, :clearance => false}},
      {"PEANUTBUTTER" => {:price => 3.00, :clearance => true}},
      {"BEETS" => {:price => 2.50, :clearance => false}},
      {"SOY MILK" => {:price => 4.50, :clearance => true}}
    ]
  end

  let(:coupons) do
    [
      {:item => "AVOCADO", :num => 2, :cost => 5.00},
      {:item => "BEER", :num => 2, :cost => 20.00},
      {:item => "CHEESE", :num => 3, :cost => 15.00}
    ]
  end

  describe "#consolidate_cart" do
    it "adds a count of one to each item when there are no duplicates" do
      cart = [find_item('TEMPEH'), find_item('PEANUTBUTTER'), find_item('ALMONDS')]
      result = consolidate_cart(cart)
      result.each do |item, attributes|
        expect(attributes.keys).to include(:count)
        expect(attributes[:count]).to eq(1)
      end
    end

    it "increments count when there are multiple items" do
      avocado = find_item('AVOCADO')
      cart = [avocado, avocado, find_item('KALE')]

      result = consolidate_cart(cart)
      expect(result["AVOCADO"][:price]).to eq(3.00)
      expect(result["AVOCADO"][:clearance]).to eq(true)
      expect(result["AVOCADO"][:count]).to eq(2)

      expect(result["KALE"][:price]).to eq(3.00)
      expect(result["KALE"][:clearance]).to eq(false)
      expect(result["KALE"][:count]).to eq(1)
    end
  end

Here is what I have done so far:

def consolidate_cart(cart)

  newCart = {}

  cart.each do |item| 

    if cart[item.keys[0]]
        cart[item.keys[0]][:count] += 1;
    else
      cart[item[0]]
      #need to use values method to get the price and clearence added.
      binding.pry
    end
    newCart[cart]
  end
end

I am getting the following error:

  1) Grocer #consolidate_cart adds a count of one to each item when there are no duplicates
     Failure/Error: result = consolidate_cart(cart)

     TypeError:
       no implicit conversion of String into Integer
     # ./grocer.rb:7:in `block in consolidate_cart'
     # ./grocer.rb:5:in `each'
     # ./grocer.rb:5:in `consolidate_cart'
     # ./spec/grocer_spec.rb:28:in `block (3 levels) in <top (required)>''

I really could use an explanation/help. I can't get my pry to work either to really look at each piece of ode individually.

def consolidate_cart(cart) # cart is an array of hashes newCart = {}

cart.each do |k|

if newCart[k.keys[0]]
  newCart[k.keys[0]][:count] += 1
else
  newCart[k.keys[0]] = k[k.keys[0]]
  newCart[k.keys[0]][:count] = 1
end

end newCart end

I know this is slightly old but I hope this helps someone else out.

It looks like you and I are working on the same thing. This was my solution and it passed the rspec tests.

def consolidate_cart(cart)
 new_cart = {}
 i = 0 
 while cart.length > i do 
   if new_cart.keys.include?(cart[i].keys[0]) == false 
     new_cart.merge!(cart[i])
     new_cart[cart[i].keys[0]][:count] = 1
     i += 1 
   else 
     new_cart[cart[i].keys[0]][:count] += 1
     i += 1 
   end
end
   p new_cart
end 

I decided to use .include? as it returns a boolean value, making the if/else statement really straightforward. The .include? method searches for a match in keys. If there is not a match, send that entire hash to new_cart. If there is a match, increment :count by 1. You'll want to use a loop so that you can catch all elements inside of the starting array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM