A retail chain issues loyalty reward codes that customers scan at checkout. Each submitted record has three fields: a code string, a department name, and an active flag. A record is considered VALID only if all of the following hold: (1) the code is a non-empty string containing only uppercase English letters ('A'-'Z'), digits ('0'-'9'), and underscores ('_'); (2) the department is exactly one of the four recognized departments -- ELECTRONICS, GROCERY, PHARMACY, APPAREL -- matched case-sensitively, with no other department accepted; and (3) the active flag equals 1. Given n submitted records, output the codes of all VALID records, ordered first by department priority (ELECTRONICS before GROCERY before PHARMACY before APPAREL) and, within the same department, by the code string in ascending lexicographic (ASCII) order.
Line 1: a single integer n, the number of submitted records. Each of the next n lines contains three space-separated tokens: code department active, where code and department contain no embedded whitespace, and active is either 0 or 1.
Print each valid code on its own line, in the order described above. If no record is valid, produce no output.
Example 1
Input
5 SAVE10 ELECTRONICS 1 milk20 GROCERY 1 RX_50 PHARMACY 1 JACKET5 APPAREL 0 BOGO_1 TOYS 1
Expected
SAVE10 RX_50
Explanation
SAVE10 is valid: it uses only uppercase letters and digits, ELECTRONICS is a recognized department, and the flag is active. milk20 is rejected because it contains lowercase letters. RX_50 is valid: uppercase letters, digits, and an underscore, PHARMACY is recognized, and it's active. JACKET5 has active flag 0, so it's rejected even though its code and department would otherwise qualify. BOGO_1 is rejected because TOYS is not one of the four recognized departments. The two valid codes are printed in department-priority order: ELECTRONICS (SAVE10) before PHARMACY (RX_50).
Example 2
Input
3 abc123 ELECTRONICS 1 CODE#1 GROCERY 1 GOOD1 HOME 1
Expected
(empty)Explanation
abc123 contains lowercase letters, so it fails the character rule. CODE#1 contains the character '#', which is not allowed. GOOD1's department, HOME, is not one of the four recognized departments. None of the three records qualify as valid, so the output is empty.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →