---
title: "Using Programs with Data | FiveStudy"
description: "How programs transform, filter, combine, and visualize data, why data is cleaned first, and how running programs iteratively reveals patterns."
url: https://fivestudy.com/guides/ap-csp-2-4-using-programs-with-data
---

[AP Computer Science Principles](https://fivestudy.com/ap-computer-science-principles) · Study guide

# Using Programs with Data

[Figure: FiveStudy]

FiveStudy

AP® Computer Science Principles · Big Idea 2 · Data

AP® Computer Science Principles · Big Idea 2 · Data

# Using Programs with Data

How programs transform, filter, combine, and visualize data to answer questions, and how running them again and again turns information into insight.

1. Programs turn data into information 2. Transform 3. Filter 4. Combine and compare 5. Visualize 6. Clean first 7. Running programs again and again 8. Worked examples 9. Common mistakes

## 1 Programs turn data into information

Programs can process data to get information out of it. A bus company's file of trips is just rows; a program can turn it into “the red line averages 12 minutes.” Nearly every data job is built from four kinds of steps: **transforming** every item, **filtering** to keep only some items, **combining or comparing** items to get one result, and **visualizing** the result.

The trips are stored in two lists. Item 1 of `routes` goes with item 1 of `minutes`, and so on:

```
routes ← ["red", "blue", "red", "green", "blue", "red", "green", "red", "blue", "green", "red", "blue"]
minutes ← [12, 9, 11, 15, 8, 13, 14, 10, 10, 13, 14, 9]
```

**Figure 1.** Filter, combine, and visualize: a program turns 12 rows into one number, and a chart makes the comparison across routes easy to see.

The same steps exist in a **spreadsheet**: a formula filled down a column transforms, the filter menu filters, and SUM, AVERAGE, and MAX combine. Spreadsheets are programs that help people organize data and find trends in it.

## 2 Transform

**Transforming** a data set changes every item in the same way: doubling every number in a list, converting every temperature from °F to °C, or adding a parent's email to every student record. Here every trip time is changed from minutes to seconds:

```
seconds ← []
FOR EACH m IN minutes
{
  APPEND(seconds, m * 60)
}
DISPLAY(seconds)
```

Output: `[720, 540, 660, 900, 480, 780, 840, 600, 600, 780, 840, 540]`. The list has the same number of items as before; each one was changed.

## 3 Filter

**Filtering** keeps only the items that pass a test: only the positive numbers, only the students who signed up for band, only the red-line trips. Filtering systems are important tools for finding information and spotting patterns, because they let you look at just the part of the data a question is about.

```
redTimes ← []
i ← 1
REPEAT LENGTH(routes) TIMES
{
  IF (routes[i] = "red")
  {
    APPEND(redTimes, minutes[i])
  }
  i ← i + 1
}
DISPLAY(redTimes)
```

Output: `[12, 11, 13, 10, 14]`. Filtering is different from **searching**. A search tool finds where something is (“which rows mention route red?”) and is useful for finding information quickly. A filter hands back a smaller data set that later steps can work on.

## 4 Combine and compare

**Combining or comparing** takes many items and produces one result: adding up a list, finding an average, or finding the student with the highest GPA. This segment works on the filtered red-line list:

```
total ← 0
longest ← redTimes[1]
FOR EACH t IN redTimes
{
  total ← total + t
  IF (t > longest)
  {
    longest ← t
  }
}
DISPLAY(total / LENGTH(redTimes))
DISPLAY(longest)
```

Output: `12` and `14`. The red line averages 12 minutes, and its longest trip took 14 minutes.

## 5 Visualize

**Visualizing** shows data as a chart, graph, or other picture. Tables, diagrams, text, and charts are all ways to share what the data showed. Each suits a different reader:

| Form | The same result |
| --- | --- |
| Text | “Green is the slowest line at 14 minutes on average, blue the fastest at 9; red is in between at 12.” |
| Table | red 12 · blue 9 · green 14 |
| Chart | the bar chart in Figure 1: the slowest line stands out at a glance |

## 6 Clean first

A program computes with whatever it is given. One broken temperature sensor reported 999 °C in a week of readings:

```
total ← 0
FOR EACH t IN temps
{
  total ← total + t
}
DISPLAY(total / LENGTH(temps))
```

That displays about 161.6, a meaningless average. Programmers use programs to **clean and filter** data before combining it. This version skips any reading that can't be a real air temperature:

```
total ← 0
count ← 0
FOR EACH t IN temps
{
  IF (t < 60)
  {
    total ← total + t
    count ← count + 1
  }
}
DISPLAY(total / count)
```

Output: `22`. Cleaning and filtering first is how the program gives an answer worth trusting.

## 7 Running programs again and again

People rarely get insight from one run of a program. They use programs **iteratively and interactively**: run the program, look at the result, ask a sharper question, change the program, and run it again.

A city stores the start time of each of 36 trips as minutes after midnight, such as 395 for 6:35. Read as a list of numbers, nothing stands out. A program **transforms** every start time into an hour and counts trips per hour:

```
counts ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
FOR EACH t IN starts
{
  hour ← (t - t MOD 60) / 60
  counts[hour + 1] ← counts[hour + 1] + 1
}
DISPLAY(counts)
```

**Figure 2.** Listed as start times, the trips look like a jumble of numbers. Changed into hours and counted, a morning peak and an evening peak appear.

Now a pattern appears: trips crowd into two rush periods, 7:00 to 9:00 and 16:00 to 18:00, and every other hour has at most one trip. **Patterns can appear when data is transformed**, and changing information from one form to another can lead to insight. The next run might ask a sharper question: which routes are crowded during the morning peak?

Programs also gain insight by **combining data sources**, **clustering**, and **classifying**:

| Process | What it does | Example |
| --- | --- | --- |
| Clustering | finds groups in the data that nobody named in advance | delivery distances fall into 3 groups: about 1.2 to 2.1 km, 9.6 to 11 km, 24.5 to 26 km |
| Classifying | puts each item into a group that already has a name | sorting emails into “spam” or “not spam” using examples already labeled |
| Combining sources | joins data from different places about the same things | joining trip times with weather records by date to see whether rain slows buses |

The key idea

Programs extract information by transforming every item, filtering to the items that matter, combining or comparing to get a result, and visualizing it. Clean the data first. Insight comes from running programs iteratively: transform, cluster, classify, or combine sources, look at what appears, and ask a better question.

## 8 Worked examples

Worked Example 1 Name the process

### Four requests

Name the process: (a) add 5 bonus points to every quiz score; (b) keep only the students with more than 3 absences; (c) find the highest score in the class; (d) draw a line graph of attendance by week.

1. 1

   (a) The same change to every item: transform.

2. 2

   (b) Keep only items that pass a test: filter.

3. 3

   (c) Many items to one result: combine or compare.

4. 4

   (d) A picture of the data: visualize.

**Answer:** (a) transform, (b) filter, (c) compare, (d) visualize

Worked Example 2 Trace a filter

### Keep the positives

The list `nums` is `[4, -2, 7, 0, -5, 3]`. What does this display?

```
positives ← []
FOR EACH n IN nums
{
  IF (n > 0)
  {
    APPEND(positives, n)
  }
}
DISPLAY(positives)
```

1. 1

   Check each item: 4 > 0 yes, −2 no, 7 yes, 0 no, −5 no, 3 yes.

2. 2

   0 is not greater than 0, so it is left out.

**Answer:** `[4, 7, 3]`

Worked Example 3 Spot a cleaning problem

### The hot week that wasn't

A program averages the temperatures [21, 23, 22, 999, 24, 20, 22] and reports about 162 degrees. What went wrong, and what should the program do?

1. 1

   999 can't be a real air temperature: it is invalid data from a broken sensor.

2. 2

   Averaging it with the real readings drags the result far too high.

3. 3

   Filter out impossible readings first; the average of the other six is 22.

**Answer:** Clean before combining. The real average is 22 degrees.

Worked Example 4 Clustering or classifying?

### Two programs

(a) A store's program finds that its customers fall into three shopping patterns nobody had named before. (b) A school's program sorts each new library book into an existing genre using books already labeled. Which is clustering and which is classifying?

1. 1

   (a) The groups were discovered from the data: clustering.

2. 2

   (b) The groups already had names, and each book is assigned to one: classifying.

**Answer:** (a) clustering, (b) classifying

Worked Example 5 Use a program iteratively

### What to run next

A first run shows trips crowd into 7:00 to 9:00 and 16:00 to 18:00. The city wants to decide where to add buses. What should the next run do?

1. 1

   The first run answered “when?” but not “where?”.

2. 2

   Filter to trips starting in those rush hours, then count trips per route.

3. 3

   Look at that result, and decide whether a still sharper question is needed.

**Answer:** Filter to the peak hours and count by route. Each run is shaped by what the last one showed.

## 9 Common mistakes

Common Mistake 1 · Mixing up filtering and transforming

A filter changes how many items there are and leaves each one alone. A transform keeps the count and changes every item.

Common Mistake 2 · Averaging before cleaning

One invalid value can wreck an average. Filter out missing and impossible values first.

Common Mistake 3 · Expecting one run to give the answer

Insight comes from looking at a result and asking a better question. Plan for more than one run.

Common Mistake 4 · Mixing up clustering and classifying

Clustering discovers groups; classifying puts items into groups that already have names.

Describe how a program gets information from data like this

“The program filters the trips to keep only the red line, then combines the times by averaging them, giving 12 minutes. A bar chart of the average for each route shows that green is the slowest.”

Name each process in the order the program uses it, and say what it produced.

FiveStudy AP® Computer Science Principles · Using Programs with Data
