Skip to content

csc123kazerouni/lab-arrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab 3 — Processing Data in Arrays

If you can read this, press

Ctrl/Cmd + Shift + V

to see these instructions formatted properly.

Objective

The objective in this lab is to practice processing data in arrays. Specifically, you will get practice with:

  • Working with tabular data represented as an array of arrays
  • Writing functions that process arrays, and make use of array operations like filter, map and reduce.
  • The assignment will also introduce you to new array functions that we haven't talked about in class yet (like find). These are "cousins" of the functions you already know, and they work in a similar way.

In this lab you are given a small dataset and asked to answer some questions about it. You will write functions to answer each question.

Note: The later questions in this lab are meant to be somewhat challenging. I am expecting there to be questions during lab periods! Please use me or your neighbours as resources as you work through this lab!

The data

This section describes the data you'll be working with. Read it carefully—it will make your life easier for the tasks below. The data depicts the percentage of schools in each county in California that offer computer science classes. It is a way of looking at who has access to CS education in California.

This is real data from around 2021, provided by the nonprofit organization CSforCA.org.

The data is given in ./data.ts. Take a look.

It contains a single array of arrays called schoolsWithCS. Each inner array is a fixed-length pair containing a string and a number, in that order. This is a convenient way to represent relatively simple tabular data.

The declaration in ./data.ts looks like this:

export const schoolsWithCS: [string, number][] = [
  [ 'Alameda', 0.534653 ],
  [ 'Amador', 0.333333 ],
  [ 'Butte', 0.321429 ],
  [ 'Calaveras', 0.444444 ],
  [ 'Colusa', 0 ],
  ...
];

It says that schoolsWithCS is an array that contains several elements, and each element is itself an array with two elements: a string and a number.

You can think of these as rows in a table containing two columns:

County Percentage of schools with CS classes
Alameda 0.534653
Amador 0.333333
Butte 0.321429
Calaveras 0.444444
Colusa 0

This is saying that about 53% of schools in Alameda county offered any CS classes, 33% of schools in Amador county offered CS classes, and 0% of schools in Colusa county offered CS classes.

Part 1: Understanding the data

We will start with some simple tasks to get you used to working with our array-of-arrays representation.

Task 1.1: Accessing elements

Write a function called percentageOfNthCounty that answers the question "What percentage of schools in the Nth county have CS classes?"

The function should take two parameters:

  • data: the array of arrays
  • n: a number

And it should return the percentage of schools with CS classes in the nth county.

Here are two example test cases to show how it should work:

console.assert(percentageOfNthCounty(schoolsWithCS, 0) === 0.534653); // Alameda
console.assert(percentageOfNthCounty(schoolsWithCS, 38) === 0.434783); // San Luis Obispo

Remember to follow the Design Recipe. Additionally, discuss the following with your neighbour:

What should your function return if n is negative, or greater than the number of counties? Account for those cases in your function.

Part 2: Processing the data

Let's use some of the higher-order array functions we've learned to process the data.

If you reach this part before we talk about the filter function in class, feel free to try it out by reading the course notes on filter. Or you can pause here and come back after we cover it in class.

Task 2.1: Counties with high access to CS education

Write a function called highAccessCounties that answers the question:

How many counties have at least 50% of their schools offering CS classes?

Your function should take one parameter:

  • data: the array of arrays

And it should return a number: the number of counties in which at least 50% of their schools offer CS classes.

You will use the filter function to solve this problem.

Task 2.2: Percentage of schools offering CS in a specific county

Write a function called percentageByCounty that answers the question:

What percentage of schools in a specific county offer CS classes?

Your function should take two parameters:

  • data: The array of arrays
  • county: The name of the county whose value you want to look up

And it should return a number: the percentage of schools with CS classes in the specified county.

There are a couple of ways in which you could approach this.

You could use filter to whittle down the data based on county name. filter would give you back an array containing the rows that match the county name. But since you know that each county name is unique in this data, the resulting array is basically guaranteed to have 0 or 1 element. It's a bit awkward to use filter in this scenario!

Instead, you should use the find function. It is similar to filter: it takes a predicate as its input, and checks each element of the array to see if it satisfies the predicate. The main difference is:

  • filter gives back all the rows that satisfy the predicate. If nothing satisfies the predicate, it returns an empty array.
  • find gives back the first row that satisfies the predicate. If nothing satisfies the predicate, it returns undefined.

Here are two "happy path" test cases to show how it should work. What other cases should you test?

console.assert(percentageByCounty(schoolsWithCS, 'Alameda') === 0.534653);
console.assert(percentageByCounty(schoolsWithCS, 'San Luis Obispo') === 0.434783);

Task 2.3: Summarizing access to CS education

Suppose you are working for the California State Government. You are trying to figure out where California stands in terms of providing access to high school CS education in the state. To do that, we need to know on average how many schools in each county are offering CS classes.

Write a function called averageAccessToCS that answers the question:

What is the average percentage of schools with CS classes across all counties?

Your function should take one parameter:

  • data: the array of arrays

And it should return a number: the average percentage of schools with CS classes across all counties.

I strongly recommend outlining a solution plan before you start coding. For example, how would you solve this problem on paper?

Hints:

  • You'll need to find the sum of all the percentages, and divide by the number of counties. What array function can you use to find the sum?
  • In order to find the sum, you'll need an array of just the percentages. What array function can you use to get that?

Following the Design Recipe, write your function in index.ts, and don't forget to include tests.

Task 2.4: Counties with low access to CS education

Now that we can compute the average percentage of schools offering CS, we need to now figure out which counties are being underserved in terms of how common CS education is.

Write a function called countiesBelowAverage that answers the question:

Which counties have a lower percentage of schools with CS classes than the average across all counties?

Your function should take one parameter:

  • data: the array of arrays

And it should return an array of strings, i.e., string[]: the names of the counties with a lower-than-average percentage of schools with CS classes than the average.

Hints:

  • You will use your averageAccessToCS function.
  • You will use filter to get the rows whose values are below average, and then you will use map to get only the county names from each row.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published