A Validator That Needs No self

Easy ⏱ 8 min 81% acceptance ★★★★☆ 4.3
Add to a User class a static method valid_username(name) returning True when name is 3-12 characters of lowercase letters/digits starting with a letter — logic that belongs *with* the class but touches no instance state. Then use it inside __init__ to raise ValueError on bad names.

Examples

Example 1
Input
User.valid_username('rahul7')
Output
True
Explanation

Callable straight off the class.

Example 2
Input
User('7bad')
Output
ValueError
Explanation

The constructor delegates to the validator.

Constraints

  • Use @staticmethod.
  • Rules: 3-12 chars, [a-z0-9], first char alphabetic.

Topics

OOPstaticmethod

Companies

InfosysWiproCapgemini

Hints

Hint 1

No self parameter at all.

Hint 2

name.isalnum(), name.islower() nuances: digits-only strings fail islower — check name[0].isalpha() and each char.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.