Tuesday, April 23, 2024
HomeSoftware EngineeringThe best way to Validate a Regex PIN Code in Python

The best way to Validate a Regex PIN Code in Python


The problem

ATM machines enable 4 or 6 digit PIN codes and PIN codes can not comprise something however precisely 4 digits or precisely 6 digits.

If the operate is handed a legitimate PIN string, return true, else return false.

Examples

"1234"   -->  true
"12345"  -->  false
"a234"   -->  false

The answer in Python code

Possibility 1:

def validate_pin(pin):
    return len(pin) in (4, 6) and pin.isdigit()

Possibility 2:

import re

def validate_pin(pin):
    return bool(re.fullmatch("d{4}|d{6}", pin))

Possibility 3:

def validate_pin(pin):
    import re
    if len(pin) == 4 or len(pin) == 6: #not 4 or 6 digits
        if re.search('[^0-9]', pin) == None : #comprises non-digit chars
            return True
            
    return False

Check instances to validate our resolution

import  take a look at
from resolution import validate_pin

@take a look at.describe("Fastened Exams")
def fixed_tests():
    @take a look at.it("ought to return False for pins with size apart from 4 or 6")
    def basic_test_cases():    
        take a look at.assert_equals(validate_pin("1"),False, "Flawed output for '1'")
        take a look at.assert_equals(validate_pin("12"),False, "Flawed output for '12'")
        take a look at.assert_equals(validate_pin("123"),False, "Flawed output for '123'")
        take a look at.assert_equals(validate_pin("12345"),False, "Flawed output for '12345'")
        take a look at.assert_equals(validate_pin("1234567"),False, "Flawed output for '1234567'")
        take a look at.assert_equals(validate_pin("-1234"),False, "Flawed output for '-1234'")
        take a look at.assert_equals(validate_pin("-12345"),False, "Flawed output for '-12345'")
        take a look at.assert_equals(validate_pin("1.234"),False, "Flawed output for '1.234'")
        take a look at.assert_equals(validate_pin("00000000"),False, "Flawed output for '00000000'")
    
    @take a look at.it("ought to return False for pins which comprise characters apart from digits")
    def _():
        take a look at.assert_equals(validate_pin("a234"),False, "Flawed output for 'a234'")
        take a look at.assert_equals(validate_pin(".234"),False, "Flawed output for '.234'")
    
    @take a look at.it("ought to return True for legitimate pins")
    def _():
        take a look at.assert_equals(validate_pin("1234"),True, "Flawed output for '1234'")
        take a look at.assert_equals(validate_pin("0000"),True, "Flawed output for '0000'")
        take a look at.assert_equals(validate_pin("1111"),True, "Flawed output for '1111'")
        take a look at.assert_equals(validate_pin("123456"),True, "Flawed output for '123456'")
        take a look at.assert_equals(validate_pin("098765"),True, "Flawed output for '098765'")
        take a look at.assert_equals(validate_pin("000000"),True, "Flawed output for '000000'")
        take a look at.assert_equals(validate_pin("123456"),True, "Flawed output for '123456'")
        take a look at.assert_equals(validate_pin("090909"),True, "Flawed output for '090909'")
    
    @take a look at.it("ought to deal with edge instances")
    def _():
        checks = [
            '',
            '123',
            '12345',
            '1234567',
            '1234567890',
            '1234x',
            '123456x',
            '12.0',
            '1234.0',
            '123456.0',
            '123n',
            '1234n',
            '09876n',
            '098765n',
            '-111',
            '111-',
            '-44444',
            '44444-',
            '+111',
            '+88888',
            '+1111',
            '-2018',
            '+234567',
            '-234567',
            '123/',
            '456:',
            'xbe', # "three quarters" in Python 3, just some byte in Python 2
        ]
        for s in checks:
            take a look at.assert_equals(validate_pin(s), False, "Flawed output for '{}'".format(s))
    
@take a look at.describe("ought to work with random enter")
def random_tests():
        
    from random import randint, selection
    from string import ascii_letters, punctuation, digits
        
    all_chars = ascii_letters + punctuation + digits
        
    def resolution(pin):
        return len(pin) in (4, 6) and pin.isdigit()
        
    def rand_valid_pin():
        size = 4 if randint(0, 1) else 6
        return "".be part of(selection(digits) for _ in vary(size))
        
    def rand_pin():
        return "".be part of(selection(all_chars) for _ in vary(randint(0, 10)))
        
    for _ in vary(40):
        pin = rand_pin() if randint(0, 1) else rand_valid_pin()
        @take a look at.it(f"testing for validate_pin({pin})")
        def test_case():    
            take a look at.assert_equals(validate_pin(pin),resolution(pin))
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments