Giter Site home page Giter Site logo

programming-univbasics-4-array-concept-review-lab-staff's Introduction

Using Loops and Arrays to Find Data

Learning Goals

  • Work with arrays and loops to extract specific values
  • Use loops to compare values in an array

Introduction

We've stepped away from the idea of programming as conversation in these last few lessons. But now that you understand what an Array is and how it mirrors real life, you might realize that you've been working with Arrays all the time!

  • Do any of the children have a runny nose?
  • Do all of the children have a runny nose?
  • "Is there a doctor in the house?"
  • Do all of the dogs have their rabies vaccination?
  • Who is the fastest runner?
  • What is lowest temperature Maui will see during my vacation?

Given an Array with the appropriate data, you now have the ability to loop through it and return the critical bit of information.

We're going to practice working with arrays and loops in a bit more depth. We will be using them to implement three common patterns:

  • Looping through an array to find the position of a value
  • Finding a maximum value in an array
  • Finding a minimum value in an array

Instructions

For this lab's tests, assume all arrays are sets of positive integers. The methods and parameter names are provided for you in lib/array_methods.rb.

ASIDE: For every test in the "Array" of tests, make sure you can pass it so you can move to the next lesson ;)

Write the implementation for each of these methods that solves the tests.

Finding Where an Element is Located on an Array

The first method, find_element_index(array, value_to_find), takes in two parameters, an array of integers and a value to find. This method should return the index of the value that was passed in. If the value is not found, this method should return nil.

To implement this method, you will need loop over the provided array and compare each value in the array to the value provided. Remember though, that this is not the value that needs to be returned

Finding a Maximum Value

The second method, find_max_value(array), takes in an array of integers. This methods should return whichever integer in the array has the highest value. So, for instance, if we ran the following:

find_max_value([1,2,1,3,4,3,5,4,3,2,1])

We should expect to receive 5 in return. Remember when writing your implementation, you will need to check every number in a given array

Finding a Minimum Value

Similar to the previous, find_min_value(array), takes in an array of integers. This method should return whichever integer in the array has the lowest value.

find_min_value([10,5,3,7,19,1,3,10])

We should expect to receive 1 in return.

Conclusion

Learning to master using a collection to answer a question is usually accomplished by looping through a collection data type (either an Array or Hash). You might notice some similar coding between these various solutions, that suggests that there might be an awesome way to DRY out this code (and you'll see it very soon!).

While coding these implementations might feel repetitive after a while, they are worthwhile to practice. They ensure that you can reason clearly about how to use collection data to answer questions and they're commonly used in interviews. From there, it is only a few small conceptual steps to highly complex algorithms.

Video Walkthrough

Try to solve this by yourself and give it your best, but if you get stuck checkout this video walkthrough:

Video Walkthrough

<iframe width="640" height="480" src="https://www.youtube.com/embed/eEIP3wliWcY" frameborder="0" allowfullscreen></iframe>

Resources

programming-univbasics-4-array-concept-review-lab-staff's People

Contributors

maxwellbenton avatar sgharms avatar donnadieu avatar drakeltheryuujin avatar

Watchers

Lore Dirick 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.