AP Computer Science Principles · Data
Using programs with data
1Programs as data tools
Programs process data far faster and more reliably than people can by hand. Common tasks:
- Filtering: selecting records that meet criteria.
- Transforming: changing every element (e.g. converting Celsius to Fahrenheit, doubling prices).
- Aggregating: combining values (sum, average, count, maximum).
- Sorting and searching.
- Combining data from several sources.
- Visualising: producing charts and maps.
Spreadsheets, databases and custom programs all do this.
Check yourself
Calculating the average of every student's score is an example of:
Show the answer
Aggregating data
Combine many values into one.
2Lists as data sets
In programs, data sets are often stored in lists. Algorithms traverse the list to process each element.
Worked example (AP pseudocode): count temperatures above 30.
count ← 0
FOR EACH t IN temps
{
IF (t > 30)
{
count ← count + 1
}
}
DISPLAY (count)
For temps = [28, 31, 35, 29, 33], the result is 3.
Traversing the list: count goes up for each value above 30
Check yourself
For temps = [30, 31, 25], how many values are counted by the condition t > 30?
Show the answer
1
Only 31.
3Interactive and iterative analysis
Data analysis is often iterative: an initial chart raises a new question, which leads to new filtering or a new visualisation.
a shop owner's data.
- A chart shows sales fell in March.
- Filter March sales by product: one product sold far less.
- Combine with stock data: the product was out of stock for two weeks.
- Insight: the fall was caused by supply, not demand.
Programs allow users to explore data interactively, adjusting filters and seeing results instantly.
Check yourself
What makes data analysis iterative?
Show the answer
Each result can lead to new questions and further processing
Repeated refinement.
4Scalability and limits
Programs make it possible to analyse data sets with millions of records, but:
- some processing takes too long even for computers, so parallel and distributed systems are used;
- programs only find patterns in the data they are given: missing or biased data gives misleading results;
- visualisations can mislead if axes or scales are chosen poorly.
Check yourself
A program finds that most customers buy online, but the data came only from the online store. The problem is:
Show the answer
The data is biased and cannot represent all customers
Sampling bias.
5Going deeper: filtering, transforming and combining, with examples
Programs turn data into information far faster, and at far larger scale, than people can by hand.
Tools: spreadsheets, databases, text-analysis and search tools, data-visualization software, and programs you write yourself.
The core operations
Filtering: keeping only the items that meet a condition.
- From a table of all customers, keep those in one city.
- From sensor readings, keep those above a threshold.
Sorting: arranging by some value, to find the largest, smallest or most recent.
Transforming: applying the same change to every item.
- Convert all temperatures from Fahrenheit to Celsius.
- Double every value; change all names to capitals.
Combining or comparing data sets: merging records from two sources (matching on a shared field), or setting one against another.
Aggregating: reducing many values to one: count, sum, average, maximum.
Classifying and clustering: putting items into known categories, or finding natural groups of similar items.
Searching: finding items that match a pattern or keyword.
Visualizing: charts, graphs and maps.
Worked example (filter and count in pseudocode): count how many scores are at least 70.
count ← 0
FOR EACH score IN scores
{
IF (score ≥ 70)
{
count ← count + 1
}
}
DISPLAY(count)
Worked example (filter into a new list): keep only the long words.
longWords ← []
FOR EACH word IN words
{
IF (LENGTH(word) > 6)
{
APPEND(longWords, word)
}
}
Worked example (transform): convert each price from dollars to cents.
cents ← []
FOR EACH price IN prices
{
APPEND(cents, price * 100)
}
Worked example (combine operations): a table of films has columns for title, year, genre and rating. To find the highest-rated comedies since 2015:
- Filter to genre = comedy.
- Filter to year ≥ 2015.
- Sort by rating, highest first.
- Read the top rows.
The order of the two filters does not change the result; sorting first would also work but would sort rows that are then discarded, which is wasted effort on a large table.
Spreadsheet-style questions: you may be shown a table and asked which sequence of steps answers a question, or which question can be answered.
Worked example (what can the data answer?): a table records, for each library loan, the book's title, genre, the date borrowed and the borrower's age group.
- "Which genre is borrowed most in summer?" Yes: filter by date, count by genre.
- "Which age group borrows most mystery books?" Yes.
- "Which books do readers enjoy most?" No: borrowing is recorded, not enjoyment.
- "How long is each book kept?" No: no return date is stored.
Always check that the fields needed are actually present.
An interactive, iterative process: analysis rarely goes straight to an answer. You look at the data, try a filter or a chart, notice something, ask a new question and refine. Programs make each round quick.
Gaining insight: combining and visualizing can show what no single record does: an outbreak of illness mapped by location; a trend in prices over years; groups of customers with similar habits.
Patterns found by machines: with very large data sets, programs can find patterns people would never spot, and use them to predict (recommendations, spam filters, weather forecasts). The predictions are only as good as the data: biased or unrepresentative data give biased results.
Cautions
- A pattern may be coincidence.
- Correlation does not show cause.
- Filtering can itself mislead if it removes relevant cases.
- Combined data sets may reveal personal information that neither source exposes alone.
Common mistakes
- Confusing filtering (choosing rows) with sorting (ordering rows).
- Confusing transforming (changing every value) with filtering.
- Claiming the data answer a question for which no field exists.
- Modifying a list while looping over it, in place of building a new one.
Check yourself
A program creates a new list containing only the temperatures above 30 from a list of readings. What is this operation called?
Show the answer
Filtering
Items are kept or discarded according to a condition.
6For top marks
- Name the operation precisely: filter, transform (map), aggregate, sort, combine.
- Trace a list-processing algorithm accurately, step by step.
- Explain how a program's results depend on the quality and completeness of the input data.
- Link visualisation choices to clear communication.
"To find the busiest hour, the program filters records for one day, counts records in each hour (aggregate) and displays the hour with the highest count."
Check yourself
Converting every price in a list from dollars to euros is:
Show the answer
Transforming the data
Changes each element.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera