Given a payment reference string reference, validate whether it follows all of the rules for a valid transaction reference.
A valid transaction reference must satisfy all of the following conditions:
-
It has a length between 8 and 20 characters (inclusive).
-
It contains only alphanumeric characters (letters
a
-
z
,
A
-
Z
, and digits
0
-
9
).
-
It does not contain a run of 3 or more consecutive identical characters. For example,
"aaa"
or
"111"
make a reference invalid, but
"aa"
or
"11"
are acceptable.
Return True if the reference is valid, or False otherwise.
Function Signature
def is_valid_reference(reference: str) -> bool:
Rules
-
Characters are compared exactly, so the run check is case-sensitive:
"aAa"
is not a run of identical characters, while
"AAA"
is.
-
Any character outside
a
-
z
,
A
-
Z
and
0
-
9
(a space, a hyphen, an underscore, punctuation) makes the reference invalid.
-
A reference that breaks several rules at once is simply invalid. The function returns one boolean.
Constraints
-
8 <= len(reference) <= 100
-
reference
consists of printable ASCII characters.
Examples
Input: reference = "AB12CD34EF"
Output: True
The string has length 10, which is between 8 and 20. Every character is a letter or a digit, and no character appears three times in a row.
Input: reference = "AAA12345BC"
Output: False
The reference starts with "AAA", a run of 3 consecutive identical characters, which breaks the repeated-run rule.
Input: reference = "AB12CD34EF56GH78IJ90K"
Output: False
Every character is a letter or a digit and no two neighbors are equal, but the length is 21, one more than the limit of 20.