← 返回 snapchat 的题目列表Valid Set Cards
类型:qbank
Given cards with feature values in {1,2,3}, determine whether a selected group forms a valid set: for every feature, values are either all same or all different.
Requirements
Implement validation for a Set-card style rule.
Input shape:
cards = [
[1, 1, 2, 2],
[2, 3, 1, 1],
[3, 2, 3, 3]
]
A group is valid if, for each feature index independently:
All selected cards have the same value for that feature, or
All selected cards have different values for that feature.
You should:
Validate a candidate group of cards.
Clarify whether the group size is always 3 or can vary.
Clarify the number of features and allowed values.
Return a boolean, and be ready to extend to finding all valid groups.
Notes
For the usual 3-card, 3-value game, each feature is valid when the set of values has size 1 or 3. If a feature has exactly 2 distinct values, the group is invalid. This check is independent across feature positions.
A common follow-up is to find the third card that completes a set for two given cards. With values {1,2,3}, the required value for each feature is either the same value when the first two match, or the missing third value when they differ. Encoding values as 0,1,2 lets you compute the missing value with modular arithmetic.
Preparation
Implement is_valid_set(cards) for three cards and arbitrary feature length.
Add tests where all features differ, all features match, one feature has exactly two values, and cards have inconsistent lengths.
Practice the follow-up that finds all valid triples in a deck using a hash set.