THINK FIRST·CODE LATER

← All labs

k-anonymity checker with generalization

Problem

The instructor wants a table of study statistics. Before release, check its k-anonymity with respect to the quasi-identifiers age, gender, major.

Input: first line k; then lines age gender major hours (hours = weekly study hours, the sensitive value) until the end of input.

  1. Group the rows by (age, gender, major). Print Original: min group size m and, if m < k, Too small: ... listing each group smaller than k as age/gender/major(size) in order of first appearance, separated by , .
  2. Then generalize the age into 5-year ranges starting at multiples of 5 (17 → 15-19, 20 → 20-24) and group again. Print Generalized: min group size m, and the small groups in the same way (using the range as age).
  3. Finally, suppress the rows that are still in groups smaller than k: print Suppressed rows: n and Release: YES (k=K, rows r) if at least one row remains, otherwise Release: NO.

Input:

2
19 F CS 10
20 F CS 12
21 F CS 8
19 M CS 15
22 M CS 5
23 M MATH 9

Output:

Original: min group size 1
Too small: 19/F/CS(1), 20/F/CS(1), 21/F/CS(1), 19/M/CS(1), 22/M/CS(1), 23/M/MATH(1)
Generalized: min group size 1
Too small: 15-19/F/CS(1), 15-19/M/CS(1), 20-24/M/CS(1), 20-24/M/MATH(1)
Suppressed rows: 4
Release: YES (k=2, rows 2)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

s11-2-data-1.txt

2
19 F CS 10
20 F CS 12
21 F CS 8
19 M CS 15
22 M CS 5
23 M MATH 9
What your program must print for s11-2-data-1.txt
Original: min group size 1
Too small: 19/F/CS(1), 20/F/CS(1), 21/F/CS(1), 19/M/CS(1), 22/M/CS(1), 23/M/MATH(1)
Generalized: min group size 1
Too small: 15-19/F/CS(1), 15-19/M/CS(1), 20-24/M/CS(1), 20-24/M/MATH(1)
Suppressed rows: 4
Release: YES (k=2, rows 2)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < s11-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content s11-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.