Git Product home page Git Product logo

collections_practice's Introduction

Collections Practice

Objectives

  1. Become proficient at manipulating arrays
  2. Practice using higher level Ruby enumerators like .collect and .sort

Instructions

The goal of this lab is to become proficient at manipulating arrays. Try experimenting with the built in Ruby methods as well as implementing your own logic to solve these. Write methods that solve each of the rspec tests.

Question 1: #sort_array_asc

Build a method sort_array_asc that takes in an array of integers and returns a copy of the array with the integers in ascending order.

Question 2: #sort_array_desc

Build a method sort_array_desc that takes in an array of integers and returns a copy of the array with the integers in descending order. Remember that .sort takes a block in which you can specify how you want your array sorted.

Question 3: #sort_array_char_count

Build a method sort_array_char_count that takes in an array of strings and returns a copy of the array with the strings ordered in ascending order by length. Remember that .sort takes a block in which you can specify how you want your array sorted.

Question 4: #swap_elements

Build a method swap_elements that takes in an array and swaps the second and third elements. Remember that array indices start at 0, so the second element has an index of 1 and the third element has an index of 2.

Advanced: Try building a method swap_elements_from_to that takes in three arguments, array, index, destination_index, that will allow you to specify the index of the element you would like to move to a new index. So:

swap_elements_from_to(["a", "b", "c"],0,2) #=> ["c", "b", "a"]
swap_elements_from_to(["a", "b", "c"],2,1) #=> ["a", "c", "b"]

Advanced #2: Try writing test coverage for it!

Question 5: #reverse_array

Build a method reverse_array that takes in an array of integers and returns a copy of the array with the elements in reverse order.

Question 6: #kesha_maker

Build a method called kesha_maker that takes in an array of strings and replaces the third character in each string with a $ ("dollar sign")—Ke$ha style. Use the .each method to iterate and build a new array to return at the end of your method, just like we did in the "My Each" lab.

Question 7: #find_a

Build a method find_a that returns all the strings in the array passed to it that start_with? (hint) the letter "a". You'll want to use a high level iterator for this that finds, selects, or detects elements based on a condition.

Question 8: #sum_array

Build a method sum_array that adds together all of the integers in the array and returns their sum.

Advanced: Try using the .inject method here.

Question 9: #add_s

Build a method that adds an "s" to each word in the array except for the second element in the array ("feet" is already plural).

Advanced: Iterators in Ruby are chainable, see if you can use .each_with_index in addition to .collect to solve this one in an elegant way. What happens if you write:

[1,2].each_with_index.collect{|element, index| }

collections_practice's People

Contributors

ahimmelstoss avatar alexgriff avatar annjohn avatar aviflombaum avatar bhollan avatar blake41 avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar gilmoursa avatar gj avatar ihollander avatar kjleitz avatar kthffmn avatar louisasm avatar markedwardmurray avatar maxwellbenton avatar msuzoagu avatar nancyko avatar sarogers avatar sophiedebenedetto avatar tsiege avatar

Watchers

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

collections_practice's Issues

#reverse_array README and spec unaligned

In the README (Question 5), it says: "Build a method reverse_array that takes in an array of integers...", but the test in the spec is as follows:

  describe '#reverse_array' do
    it 'reverse the order of an array' do
      expect(reverse_array(["blake", "ashley", "scott"])).to eq(["scott", "ashley", "blake"])
    end
  end

...which tests it against an array of strings, not integers. Shouldn't really affect anything, but the semantics are clearly off.

#sort_array_char_count passes test without modifying ruby's default #sort -- unclear example array from spec.

The method #sort_array_char_count is intended to teach students that they can modify default sorting behavior by passing a block to #sort or using #sort_by. The spec tests this functionality with the array ["dogs", "cat", "horses"]. Calling ruby's #sort with the default lexicographic ordering for Strings results in the same output as if the student sorted the array by length of string as intended. Both sortings output ["cat", "dogs", "horses"].

This can lead to confusion. Working with a student now whose code looks like this:

def sort_array_char_count(array)
  sorted_array = []
  array.each do |word|
    sorted_array.push(word.length)
  end.sort
end

This passes the spec even though it returns a lexicographic sort. I will have to explain to them that their sorted_array is never actually being returned (not to mention it is an array of integers). While this is a "teachable moment", the spec leads to a false positive and the student has not grasped the lesson.

Proposed fix: change array to ["dogs", "cat", "horses", "armadillo"]

Directions do not match the spec

the directions for bonus collections practice do not match the spec (ex: on learn, question # 3 is sort_array_char_count but the spec is looking for swap_elements) change in the curriculum?

sorting lab errors when trying to open IDE

// ♥ learn open collections_practice-online-web-pt-090819
Looking for lesson...
Forking lesson...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
Cloning lesson...
Cannot clone this lesson right now. Please try again.
[16:05:44] ~
// ♥ [16:05:45] ~
// ♥ learn open collections_practice-online-web-pt-090819
Looking for lesson...
Forking lesson...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
Cloning lesson...
Cannot clone this lesson right now. Please try again.
[16:05:54] ~
// ♥ [16:05:55] ~
// ♥ learn open collections_practice-online-web-pt-090819
Looking for lesson...
Forking lesson...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
Cloning lesson...
Cannot clone this lesson right now. Please try again.
[16:06:03] ~
// ♥ [16:06:04] ~
// ♥ learn open collections_practice-online-web-pt-090819
Looking for lesson...
Forking lesson...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...
There was a problem cloning this lesson. Retrying...
Cloning lesson...

Question 3 issues

User: Artem Eremin

Batch: 000

Bug Description: Page: https://learn.co/tracks/web-development-with-ruby-on-rails/ruby/a-deeper-dive-into-enumerables/bonus-collections-practice-1

Section: QUESTION 3: #SWAP_ELEMENTS Code: swap_elements_from_to(["a", "b", "c"],0,3) #=> ["c", "b", "a"]

Bug: actually, such call doesn't make sence, because if destination_index equals 3 then it's beyond the array, so this call should return nil or even raise an Error. Anyway, even if we call swap_elements_from_to(["a", "b", "c"],0,2) to be correct with indexes, it should return ["b", "c", "a"] instead of ["c", "b", "a"] provided in the comment

Questions 4 and 5 out of order

The lab shows question 4 being the swap method and question 5 being the reverse method but the rspec tests are reversed (4 is reverse and 5 is swap) so there will be confusion even though in the end all tests have to be run anyway.

You can't (easily) use #each_with_object to sum an array of integers

From the README:

QUESTION 8: #SUM_ARRAY
Build a method sum_array that adds together all of the integers in the array and returns their sum.
Advanced: Try using either the .inject method or the .each_with_object method here.

if you try to use #each_with_object to sum an array the return value will be 0 (or whatever the starting sum is) due to the way instances of the Fixnum class don't change. Each integer value is a different Fixnum object. #each_with_object` returns the original object passed in which will always be 0

(1..10).to_a.each_with_object(0) do |num, sum|
  sum += num
end
#= > 0

You could do something like

(1..10).to_a.each_with_object({sum: 0}) do |num, hash|
  hash[:sum] += num
end[:sum]
#= > 55

but that's sort of roundabout and confusing to students at this point in the curriculum

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.