Git Product home page Git Product logo

ruby-data-structures-lesson's Introduction

Data Structure Review - Full Lecture

Motivation / Why Should You Care?

  • Sometimes it takes a bit of repetition for a concept to click. Coding is like any new skill you might pick up -- the more you do it, the better you get at it. Last unit we covered arrays and hashes. Let's review what we learned.

Lesson Plan

  • Difference between an array and a hash -
    • Think of an array as a list of items. We access information in an array from an index, starting with 0.
    • A hash is more specific way of labeling information. A hash is made up of key-value pairs. Keys have to be unique. Access information in a hash through the key.
  • Syntax for arrays and hashes *Array:
      snax = ["chips", "oreos", "celery"]
      snax[0] #returns "chips"
      snax.delete("celery") #removes celery
      snax << "popcorn" #adds popcorn to the end of the array)]
      snax.push("trail mix") #adds trail mix to the end of the array
      snax.last #returns trail mix
      snax.first #returns chips
      snax[3] = "Cotton Candy" #replaces trail mix with cotton candy
    • Hashes
      todo = {
        :homework => "Algebra problems",
        :chores => "make my bed",
        :shopping => "buy new shoes"
      }
    
      todo[:homework] #returns Algebra Problems
      todo.delete(:homework) #removes that key-value pair from the hash
      todo[:travel] = "Go to Spain" #adds that key value pair to the hash
      todo[:chores] = "laundry" #changes the value of the :chores key to laundry
  • Iterating over arrays and hashes
    • Arrays:
      snax = ["chips", "oreos", "celery"]
      snax.each do |snack|
        "I love #{snack}"
      end
    • Hashes:
      todo = {
        :homework => "Algebra problems",
        :chores => "make my bed",
        :shopping => "buy new shoes"
      }
    
      todo.each do |category, task|
        "I have so much to do! #{category}: #{task}"
      end
    
      todo.each_key do |category|
        "I have to do #{category}"
      end
    
      todo.each_value do |task|
        "I have to #{task}!!!"
      end
  • Create array with the different ages in the class. Then calculate:
    • The sum of all the ages
      total = 0
      ages = [15, 16, 17, 19]
      ages.each do |age|
        total += age
      end
      total
    • The average age
      ages = [15, 16, 17, 19]
      sum = 0.0
      ages.each do |age|
        sum += age
      end
      puts sum/ages.length
    • The oldest age
      ages = [15, 16, 17, 19]
      biggest_age = ages[0]
      ages.each do |age|
        if biggest_age < age
          biggest_age = age
        end
      end
      puts biggest_age
  • Turn ages array into a hash with students' names as keys. Then calcuate:
    • The sum of all the ages
      ages = { :beth => 15,:sarah => 16,:jeff => 17,:carrie => 19}
      array = ages.values
      sum = 0
      array.each do |number|
        sum += number
      end
      puts sum
    • The average age
      ages = { :beth => 15,:sarah => 16,:jeff => 17,:carrie => 19}
      array = ages.values
      sum = 0.0
      ages.each_value do |age|
        sum += age
      end
      puts sum/array.length
    • The youngest
      youngest_student = ages.sort_by {|keys,values| values}[0]
    • Organize the students alphabetically.
      alphabetical_student = ages.keys.sort
  • Write a method that takes in any array of numbers as an argument and gives back the average?
def find_average(array)
  sum = 0.0
  array.each do |number|
    sum += number
  end
  puts sum / array.length
end
  • Nested hashes and arrays
    • How do we pull individual items out if this is the case?
school_subjects = [
  ["algebra", "geometry", "trigonometry"],
  ["physics", "biology", "chemistry"],
  ["European history", "world civilizations", "U.S. history"]
]
  • Algebra"
school_subjects.first.first
school_subjects[0][0]
  • World Civilizations:
school_subjects[2][1]
  • Convert school_subjects to a hash.
 school_subjects = {
  :math => ["algebra", "geometry", "trigonometry"],
  :science => ["physics", "biology", "chemistry"],
  :social_studies => ["European history", "world civilizations", "U.S. history"]
}
  • Now how do I access "biology"?
  school_subjects[science][1]

ruby-data-structures-lesson's People

Watchers

James Cloos avatar

Forkers

mbigman

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.