medium +25 pts

Match Credit Card Pattern

Validate credit card numbers: 16 digits in groups of 4, no repeated digit 4+ times.

Write a function `is_valid_credit_card(card: str) -> bool` that returns `True` if the string `card` is a valid credit card number according to the following rules: - It must be exactly 16 digits long. - It may optionally have hyphens separating the digits into groups of exactly 4 digits (i.e., `DDDD-DDDD-DDDD-DDDD`). If hyphens are used, they must be present between every group; otherwise, the number is just 16 consecutive digits. - The string must NOT contain any other characters (only digits, and optionally hyphens at the correct positions). - It must NOT have 4 or more consecutive repeated digits anywhere in the 16-digit sequence (e.g., `1111` is invalid, but `111` is allowed). Your function should use a regular expression to check the format, and you may also use additional logic for the consecutive-digit rule. The string must match the whole pattern; no extra leading/trailing characters. Implement `is_valid_credit_card` so that it returns a boolean.

Constraints

The input is a string containing only ASCII characters. Its length is at most 100. Time complexity should be O(N) where N is the length of the string.

Example

>>> is_valid_credit_card('4253625879615786')
True
>>> is_valid_credit_card('5122-2368-7954-3214')
True
>>> is_valid_credit_card('42536258796157867')
False
>>> is_valid_credit_card('4424444424442444')
False
>>> is_valid_credit_card('5122-2368-7954-3214-')
False
>>> is_valid_credit_card('5122-2368-7954-321')
False
25 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `re.fullmatch` to ensure the whole string matches either the hyphenated or unhyphenated pattern.
To allow both formats, use a pattern that accepts exactly 16 digits or groups of 4 separated by hyphens.
After removing hyphens, verify there are exactly 16 digits.
For the consecutive digit rule, use a regex like `(\d)\1{3}` on the digit-only string.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.