Git Product home page Git Product logo

programming-univbasics-nds-nested-arrays-iteration-lab-part-3-nyc01-seng-ft-051120's Introduction

Nested Data Structures: Reducing Data

For the final lab on traversing Array of Arrays, we're going to apply the double while loop to find a single value.

Traverse Array of Arrays to Produce a Single Value

Another variant on traversing nested Arrays is traversing the matrix and accumulating all the values. Let's imagine that someone created a grid representing a guessing game. In each cell of the grid, the games makers have put some amount of cash. Let's sum up all the possible values to determine how much money is in the grid.

guessing_game_grid = [
 [1, 2, 1, 7, 3],
 [2, 100, 15, 4, 18],
 [15, 16, 99, 1, 2, 11]
]

total = 0
row_index = 0
while row_index < guessing_game_grid.count do
  element_index = 0
  while element_index < guessing_game_grid[row_index].count do
    total += guessing_game_grid[row_index][element_index]
    element_index += 1
  end
  row_index += 1
end
total #=> 297

This pattern is known as reduction because you're going to reduce a nested data structure to a single value. As you learn Ruby's Enumerable methods, remembering this name will help a lot!

Instructions

In this lab, you'll be working with a mix of data types - nested arrays with strings and numbers. We need to extract out the strings from this data, ignoring the numbers.

Write a method, join_nested_strings, that can take in an array of arrays of integers and strings. This method should iterate over each nested array, combining all the strings into a single string value.

For example, given the code below:

mixed_data = [
  ["The", 4, "quick"],
  [-1, "brown", "fox", 30],
  ["studied", 101, 233, "Ruby"]
]

join_nested_strings(mixed_data)

We should expect the following returned:

"The quick brown fox studied Ruby"

Note: Don't worry if your resulting string has an extra space at the end - the tests will ignore it.

Conclusion

We've seen examples of methods like .join that combine arrays of strings, but in this situation, we need something more customizable. We were able to take a set of 'dirty' data and reduce it down to a single value, something that .join would not be able to accomplish on its own.

You may have noticed that when we're discussing nested data structures - a lot of the concepts we are introducing are things we've seen in regular arrays. At the core of our implementations, we're performing operations that we're familiar with - using conditional statements, combining values, pushing data into arrays, etc... We're incorporating these basic tasks with loops to produce custom data tools.

programming-univbasics-nds-nested-arrays-iteration-lab-part-3-nyc01-seng-ft-051120's People

Contributors

maxwellbenton avatar annjohn avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin 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.