AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/2/2-4)
Unit 2 · Topic 2.4
2.4 Using Programs with Data
Programs let you work with far more data than you could ever handle by hand. This topic covers the main ways programs process data (filtering, transforming, combining and visualizing) and how people use programs again and again to dig insight out of a data set.
Key terms
- filtering data
- transforming data
- combining data
- data visualization
- patterns and trends
Why use a program?
A spreadsheet of 30 rows you could sort by hand. A file of 3 million rows you can't. Programs, including spreadsheets, databases and code you write yourself, can search, sort and summarize huge data sets quickly and without getting tired.
Search tools help you find specific information fast. Filtering systems help you find what matters and spot patterns. Spreadsheets help organize data and find trends with sorting, formulas and charts.
Four ways to process data
Most data work is some mix of these:
- Transforming: changing every element in the same way, like converting every temperature from Fahrenheit to Celsius, or adding a parent's email to every student record.
- Filtering: keeping only the elements that meet a condition, like only the orders over $50 or only the students who signed up for robotics.
- Combining or comparing: bringing values together into a result, like adding up a column, finding the average, or finding the student with the most volunteer hours.
- Visualizing: showing data as a chart, graph, map or table so patterns jump out.
Filtering in code
A filter goes through a list and keeps only the items that pass a test. This segment keeps the temperatures of 80 or more:
temps ← [68, 75, 81, 59, 90]
hot ← []
FOR EACH t IN temps
{
IF (t ≥ 80)
{
APPEND(hot, t)
}
}
DISPLAY(LENGTH(hot))
It displays 2, because only 81 and 90 pass the test. The new list hot holds [81, 90], and the original list is unchanged.
Transforming and combining in code
A transformation applies the same change to every element. This segment builds a list of Celsius temperatures from Fahrenheit ones:
tempsF ← [68, 86, 59]
tempsC ← []
FOR EACH f IN tempsF
{
APPEND(tempsC, (f - 32) * 5 / 9)
}
Afterward tempsC holds 20, 30 and 15, in the same order as the originals.
Combining reduces a list to a single answer, like a total, an average or a maximum. Those patterns are covered in detail in 3.9 and 3.10.
Gaining insight is a loop
Working with data is iterative and interactive. You filter, look at the result, notice something, ask a new question, then transform or chart the data a different way. Each answer leads to the next question.
Programs can also clean data, combine data from several sources, cluster data (group similar items together, like customers with similar buying habits) and classify data (sort items into categories, like spam or not spam). Patterns often show up only after the data has been transformed or visualized, such as when daily numbers are turned into a weekly trend line.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Combining: average and maximum
What does this code segment display?
sales ← [120, 95, 140, 110] total ← 0 best ← sales[1] FOR EACH s IN sales { total ← total + s IF (s > best) { best ← s } } DISPLAY(total / LENGTH(sales)) DISPLAY(best)Show the solutionHide the solution
- Step 1: Start: total = 0, best = sales[1] = 120.
- Step 2: s = 120: total = 120; 120 > 120 is false, so best stays 120.
- Step 3: s = 95: total = 215; 95 > 120 is false.
- Step 4: s = 140: total = 355; 140 > 120 is true, so best = 140.
- Step 5: s = 110: total = 465; 110 > 140 is false.
- Step 6: The average is 465 / 4 = 116.25 (on the exam, / is ordinary division). Then it displays best, which is 140.
Answer: 116.25 140
- Example 2
Choosing a process
A school has a list of every student's record, including grade level and number of absences. The principal wants to know how many 9th graders have more than 10 absences. Describe the steps a program would use.
Show the solutionHide the solution
- Step 1: Filter the records to keep only students whose grade level is 9.
- Step 2: Filter that result again to keep only students with more than 10 absences. (Or do both checks at once with AND.)
- Step 3: Combine by counting the records that remain.
Answer: Filter for grade 9, filter for more than 10 absences, then count the remaining records.
Common mistakes
- Mixing up filtering and transforming. Filtering keeps some items unchanged; transforming changes every item and keeps them all.
- Assuming a filter changes the original list. In the example, the matching values are copied into a new list.
- Forgetting that a visualization can mislead if its scale is chosen badly. Check the axes before drawing conclusions.
On the exam
- Questions often describe a data set and a goal, then ask which sequence of steps (filter, sort, count, chart) would achieve it. Make sure each step uses data that's actually available.
- Code-tracing questions may show a FOR EACH loop that filters or totals a list. Track each variable after every pass.
Connected topics
Videos
Check yourself
4 questions on 2.4 Using Programs with Data. Pick an answer to see if you got it, and why.
A teacher has a list of every student's quiz score and wants a list of only the scores below 70, so she can offer extra help. Which data process does this describe?
A store has a list of prices in dollars and creates a new list where every price is converted to euros. Which data process is this?
The list temps contains [72, 85, 90, 78, 81]. What is displayed when the following code segment is run?
result ← []
FOR EACH t IN temps
{
IF (t > 80)
{
APPEND(result, t)
}
}
DISPLAY(result)
The list nums contains [4, 7, 1] and the list doubled is empty. What is displayed when the following code segment is run?
FOR EACH n IN nums
{
APPEND(doubled, n * 2)
}
DISPLAY(LENGTH(doubled))
DISPLAY(doubled[2])
0 of 4 answered