Collections Lab

Lab Objectives

  • Practice common iteration patterns: transform, find, and build.
  • Convert strings into a collection.
  • Define method as it relates to programming.

Practice

Each student should fork this REPL. In your small group, each person will review one of the three example code blocks. Take 5 minutes to review your code block, then take turns describing what is happening in each example. Be sure to describe things line by line - “On line 1 ___ happens, then on line 2 _____…”. And, discuss what each variable holds at each point during the code block.

After reviewing and sharing about your scenarios, Read the Common Iterations Patterns section, and discussed the included questions.

Common Iteration Patterns

Many of the tasks that involve collections of information fall into three patterns: Transform, Find, and Build.

We transform a collection by changing every element in a collection and storing those changed (transformed) elements in a new collection. An example could be transforming a list of strings into their corresponding lengths: {"Minnesota", "Iowa", "Missouri", "Arkansas", "Louisiana"} becomes {9, 4, 8, 8, 9}. When we transform a collection, we perform the same manipulation on each element, and end with the same number of elements that we started with.

Sometimes, we need to find elements in a collection that meet some criteria (we are trying to find a subset of the collection). For example, we might want to identify usernames that include specific characters, like spaces. When we use the find pattern, we typically end up with a collection that is smaller than the original collection; the resulting collection may also be empty.

There are also times when we want to use a collection to build a new thing. For example, we might want to use a List that represents a grocery list, and turn it into a Dictionary: {"Milk", "Bread", "Egg", "Egg", "Egg"} becomes { {"Milk", 1}, {"Bread", 1}, {"Egg", 3} }

Questions to answer with your group:

  • For each of the examples above, decide which pattern the example falls under.
  • Brainstorm one or two similarities in the code for each pattern.
  • Brainstorm a difference in the code for each pattern.

Choose one person in your group who will share out highlights from your discussion!

Keep an Eye on Slack We will be coming back together to discuss what you all have learned.

Manipulating Strings as Collections

As developers, we don’t always get data in exactly the format we would like it to be in; in fact, we nearly always have to do some data manipulation in order to complete the task at hand. The most common form of data on the web is String, and we often need to organize that string information into collections. This is such a common operation, that most languages include methods to help with this task.

Read this article about the String method, .Split().

In your notebook, answer the following:

  • What is the length of the array haiku in the following example:
    string[] haiku = "A world of dew, and within every dewdrop, a world of struggle".Split(", ");
    
  • Describe the difference between the two examples below:
    string[] users = "Mike, Alice, Bob".Split(',');
    
    string[] users = "Mike, Alice, Bob".Split(", ");
    

Lesson Search Results

Showing top 10 results