Coder Social home page Coder Social logo

ruby-enumerables-practice-hash-collections-exercise's Introduction

Collections Practice

Learning Goals

  • Practice Iteration over arrays and hashes
  • Practice accessing and comparing data in arrays and hashes
  • Navigate and create nested data structures

Instructions

The goal of this lab is to become proficient manipulating the basic data structures we use in Ruby: arrays and hashes. Try experimenting with the built-in Ruby and Hash methods as well as implementing your own logic to solve these. Write methods that solve each of the RSpec tests. All solutions should be generic and work for any "case." These problems should get progressively more difficult.

begins_with_r

The begins_with_r method takes in an array of strings returns true if every string of the array starts with an r. Returns false otherwise.

Example:

array_of_words = [
  "apple",
  "rambutan",
  "banana",
  "raspberry"
]

r_words = [
  "red",
  "raisin",
  "rice"
]

begins_with_r(array_of_words) #=> false
begins_with_r(r_words) #=> true

contain_a

The contain_a method takes in returns all elements that contain the letter a. For example, given the following array:

array_of_produce = [
  "apple",
  "banana",
  "broccoli",
  "mesclun"
]


contains_a(array_of_produce)

We would get an array in return without some elements:

[
  "apple",
  "banana"
]

first_wa

The first_wa method returns the first element that begins with the letters wa.

For example:

array_of_words = [
  "paint",
  "garage",
  "housewares",
  "walk",
  "wait",
  "television"
]

first_wa(array_of_words)

Would return:

"walk"

Even though "housewares" has wa, it does not start with those letters, so it was not considered.

remove_non_strings

The remove_non_strings method takes in an array of different data types and returns a new array where anything that is not a string has been removed.

For example:

remove_non_strings(["I am a string", :symbol, 10, {}, "hey a string", []])

Should return:

["I am a string", "hey a string"]

count_elements

The count_elements method takes in an array of hashes, some duplicated. It then returns a new array of hashes that include a :count key indicating the number of original hashes that were passed in. So, for example, if we were to run the following:

count_elements([{:name => "peter"}, {:name => "peter"}, {:name => "ashley"}])

We would get in return:

[{:name => "peter", :count => 2}, {:name => "ashley", :count => 1}]

merge_data

The merge_data method takes in two arrays of the same length, both containing hashes. It returns one array, the same length as the original, containing merged hashes - the first hash in both arrays should be merged together to become the first hash in the returned array, the same with the second, and all subsequent hashes.

So, for example, if we passed in an array of hashes containing titles and an array of hashes containing names, we would get one array containing hashes, each with one title and one name:

merge_data([{:title => "Mayor"}, {:title => "Governor"}], [{:name => "Quimby"}, {:name => "Bailey"}])

We would get in return:

[{:title => "Mayor", :name => "Quimby"}, {:title => "Governor", :name => "Bailey"}]

find_cool

The find_cool method takes in an array of hashes and returns a new array containing only hashes that contain a key :temperature pointed to the value "cool". For example, running the following:

find_cool({:location => "Texas", :temperature => "hot"}, {:location => "Artic", :temperature => "cool"}, {:location => "New York", :temperature => "temperate"})

We would get in return:

[{:location => "Artic", :temperature => "cool"}]

organize_campuses

The organize_campuses method takes in a hash containing the names of some Flatiron School campuses as keys. These keys each point to a hash containing a :location pointing to the US state they are in. This method should create a new hash where instead of campus names, the US state value is the 'top-level' key. This key should point to an array containing each of the campus names that are located in that state.

For example, if we were to run:

campus_hash = {
  "Flatiron School San Francisco" => {
    :location => "CA"
  },
  "Access Labs" => {
    :location => "NY"
  },
  "Flatiron School Seattle" => {
    :location => "WA"
  },
  "Flatiron School Manhattan" => {
    :location => "NY"
  }
}

organize_campuses(campus_hash)

Will result in:

{
  "CA"=>["Flatiron School San Francisco"],
  "NY"=>["Access Labs", "Flatiron School Manhattan"],
  "WA"=>["Flatiron School Seattle"]
}

ruby-enumerables-practice-hash-collections-exercise's People

Contributors

ahimmelstoss avatar blake41 avatar fislabstest avatar fs-lms-test-bot avatar jd2rogers2 avatar kthffmn avatar maxwellbenton avatar sarogers avatar sgharms avatar

Watchers

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

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.