From 36c999fe65467a1c923fb56f3b6e017ad313354d Mon Sep 17 00:00:00 2001 From: 24520293-cyber <24520293@gm.uit.edu.vn> Date: Sun, 5 Oct 2025 11:03:38 +0000 Subject: [PATCH] Closes: #1 Updated validations.py python script. Fixed the behavior of validate_user function in validations.py --- Course3/Lab4/validations.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Course3/Lab4/validations.py b/Course3/Lab4/validations.py index b18de65a2e..7bc0335893 100644 --- a/Course3/Lab4/validations.py +++ b/Course3/Lab4/validations.py @@ -1,24 +1,55 @@ -#!/usr/bin/env python3 import re + + def validate_user(username, minlen): + """Checks if the received username matches the required conditions.""" + if type(username) != str: + raise TypeError("username must be a string") + if minlen < 1: + raise ValueError("minlen must be at least 1") + + # Usernames can't be shorter than minlen + if len(username) < minlen: + return False + # Usernames can only use letters, numbers, dots and underscores + if not re.match('^[a-z0-9._]*$', username): + return False + # Usernames can't begin with a number + if username[0].isnumeric(): + return False + + # Usernames can't begin with '.' or '_' + + if username[0] in "._": + + return False + return True +print(validate_user("blue.kale", 3)) # True + +print(validate_user(".blue.kale", 3)) # False + +print(validate_user("red_quinoa", 4)) # True + +print(validate_user("_red_quinoa", 4)) # False +