Giter VIP home page Giter VIP logo

apples-and-holidays's Introduction

Iterating Over Hashes

Objectives

  1. Iterate over nested, or multidimensional, hashes.

The Holiday Suppliers

Instructions

You have a bunch of decorations for various holidays organized by season.

holiday_supplies = {
  :winter => {
    :christmas => ["Lights", "Wreath"],
    :new_years => ["Party Hats"]
  },
  :summer => {
    :fourth_of_july => ["Fireworks", "BBQ"]
  },
  :fall => {
    :thanksgiving => ["Turkey"]
  },
  :spring => {
    :memorial_day => ["BBQ"]
  }
}

Write your methods in lib/holiday.rb; use the comments in each method as guides.

  • Write a method that returns the second supply for the Fourth of July. For example:
def second_supply_for_fourth_of_july(holiday_supplies)
  holiday_supplies[:summer][:fourth_of_july][1]
end
  • Write a method that adds a supply to both Winter holidays.

  • Write a method that adds a supply to Memorial Day.

  • Write a method that adds a new holiday and its associated supplies to any season.

  • Write a method to collect all Winter supplies from all the winter holidays. For example:

winter_supplies(holiday_supplies) #=> ["Lights", "Wreath", etc]
  • Write a method that uses a loop to list out all the supplies you have for each holiday and the season. Use string manipulation to get your output to match what the test is expecting.

Here are a few helpful tips:

  • Our hash keys are symbols. We need to convert them into strings. Use the .to_s method on a symbol to convert it into a string.
  • Look closely at the output string that the test is expecting. You'll notice that it expects holiday names, like "New Years", to have both words capitalized. Ruby has a .capitalize method that you can call on a string. But, note:
    • .capitalize returns the capitalized string but doesn't change the original string. So, when you call on that same string in the future, it isn't capitalized! You can capitalize a string for now and evermore by using the bang operator (!).
    • You'll need to capitalize both words in a given holiday's name. If you call "new years".capitalize!, it will return "New years". In order to capitalize both words, you'll need to .split the string into an array and iterate over that array to .capitalize! each word in it. Then, you'll need to .join the array back into a string.
    • If you're unfamiliar with the methods mentioned above, look them up in the Ruby documentation.

Example of expected output:

Winter:
  Christmas: Lights, Wreath
  New Years: Party Hats
  • Write a method to collect all holidays with "BBQ" in the supply array. The method should behave as seen below:
holidays_with_bbqs(holiday_supplies)
#=> [:fourth_of_july, :memorial_day]

Reminder: This is a challenging lab, so remember to use Pry, Google, and the Flatiron School community to help you get the tests passing.

Resources

apples-and-holidays's People

Contributors

ahimmelstoss avatar annjohn avatar arelenglish avatar aviflombaum avatar benjagross avatar bhollan avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar ga-be avatar gilmoursa avatar ihollander avatar irmiller22 avatar jmburges avatar kthffmn avatar lizbur10 avatar loganhasson avatar matthewkrey avatar maxwellbenton avatar msuzoagu avatar ngevan avatar pletcher avatar roseweixel avatar sammarcus avatar sarogers avatar sophiedebenedetto avatar timothylevi avatar tsiege avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apples-and-holidays's Issues

I could not solve this lab

@SophieDeBenedetto

I feel like I just got thrown into the deep end of the pool. Here's my fork: https://github.com/markedwardmurray/apples-and-holidays-000

There are still 3 failures:

I don't understand why using the .collect method is returning an empty array on that test.

Why is valentine's day not getting added?

I'm not practiced enough with string interpolation at this point to solve displaying the nested hash according to the expectation. The instructions were not clear that I could have chosen to puts everything individually. Don't provide two options to solve a challenge: pick one and be clear about the expectation. If you want to keep the second option, turn it into an advanced.

Were I a student at home, this lab may have caused me to give up on Learn.

Learn IDE issues

Is something being done about the error that is coming up sometimes when opening the learn IDE? I have seen comments from other students as well that this is consistently reoccurring. It either shows the error below or does not reconnect Learn IDE.
This is putting people behind on coursework and is making things difficult during an already rigorous program.
capture (1)

First RSpec test doesn't actually check for hardcoded solutions

The test describes itself like so: "returns the string 'BBQ' without hardcoding it".

But the test doesn't contain any code that checks for a hardcoded solution:

describe "#second_supply_for_fourth_of_july" do
  it "returns the string 'BBQ' without hardcoding it" do
    expect(second_supply_for_fourth_of_july(holiday_supplies)).to eq("BBQ")
  end
end

Apple file

spec/spec_helper.rb file line 1: require_relative '../lib/apple'

but there is no '../lib/apple'.

tests only run if line is commented-out (or that file is created, obviously).

arguments incorrect?

Hi there.

I was struggling with a lot of this lesson evidently due to the fact that some of the pre-filled arguments for the last few methods in this lab didn't match up to what was provided in the solution. Since I'm fairly new to this I didn't realize that I can just alter those arguments to best fit the solutions needed. I tend to take things too literally so its probably my fault for not realizing I could make those sorts of changes, but it might help some of us newbs if that was mentioned in the hint section.

Tests use too much iteration and aren't clear about expectations

These tests can be updated. The setup and expectations should be optimized for flow and understanding, not DRY.

  # Question 4
  # Write a method that adds a new holiday and its associated supplies to any season
  describe "#add_new_holiday_with_supplies" do
    let(:columbus_supplies) { ["Flags", "Parade Floats", "Candle Sticks"] }
    let(:v_day_supplies)    { ["Cupid Cut-Out", "Candy Hearts"] }
    let(:add_columbus_day)  { add_new_holiday_with_supplies(holiday_supplies, :fall, :columbus_day, columbus_supplies) }
    let(:add_v_day)         { add_new_holiday_with_supplies(holiday_supplies, :winter, :valentines_day, v_day_supplies) }

    it "returns an updated version of the original hash" do
      [add_columbus_day, add_v_day].each do |result|
        expect(result.class).to eq(Hash)
      end
    end

    it "still has exactly four seasons" do
      [add_columbus_day, add_v_day].each do |result|
        expect(result.keys.size).to eq(4)
      end
    end

    it "has two total holidays in fall when holiday is added to fall" do
      num_of_keys_in_fall = add_columbus_day[:fall].keys.size
      expect(num_of_keys_in_fall).to eq(2)
    end

    it "has three total holidays in winter when holiday is added to winter" do
      num_of_keys_in_winter = add_v_day[:winter].keys.size
      expect(num_of_keys_in_winter).to eq(3)
    end

    it "adds an extra holiday to the hash in the correct season
       where the key is the holiday name and
       the value is the array of supplies" do
      expect(add_columbus_day[:fall][:columbus_day]).to eq(columbus_supplies)
      expect(add_v_day[:winter][:valentines_day]).to eq(v_day_supplies)
    end
  end

Should be re-written as:

  # Question 4
  # Write a method that adds a new holiday and its associated supplies to any season
  describe "#add_new_holiday_with_supplies" do
    it "modifies the original hash by adding supplies of a new holiday to a season" do
      columbus_day_supplies = ["Flags", "Parade Floats", "Candle Sticks"]

      add_new_holiday_with_supplies(holiday_supplies, :fall, :columbus_day, columbus_day_supplies)

      expect(holiday_supplies[:fall].keys).to include(:columbus_day)
      expect(holiday_supplies[:fall][:columbus_day]).to match_array(columbus_day_supplies)

      valentines_day_supplies = ["Cupid Cut-Out", "Candy Hearts"]
      add_new_holiday_with_supplies(holiday_supplies, :winter, :valentines_day, valentines_day_supplies)

      expect(holiday_supplies[:fall].keys).to include(:valentines_day)
      expect(holiday_supplies[:fall][:columbus_day]).to match_array(valentines_day_supplies)
    end
  end

I think that's way clearer. Thoughts?

Typos in solution

holiday_supplies in both of these methods should be holiday_hash.

def all_winter_holiday_supplies(holiday_hash)
  holiday_supplies[:winter].map do |holiday, supplies|
    supplies
  end.flatten
end

def all_supplies_in_holidays(holiday_hash)
  holiday_supplies.each do |season, holidays|
    puts "#{season.capitalize}:"
    holidays.each do |holiday, supplies|
      puts"  #{holiday.to_s.split('_').map {|w| w.capitalize }.join(' ') }: #{supplies.join(", ")}"
    end
  end
end

Apple still in specification

The specifications could not find an apple.rb file. Looking at the github page, it seemed to have been purposefully removed. However, in the specification file I had, it still had:

require_relative '../lib/apple'

Once I commented it out, everything worked fine.

apple.rb file

The apple.rb files and tests don't seem to belong here. I got the tests to pass, but there is no mention of the exercise in the description, plus it deals with array methods that are covered earlier in the course.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.