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)

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.