Style guides say which naming style each kind of name uses. Recognise the style of a name.
What to build
A program that classifies identifiers by their naming style.
Requirements
UPPER_CASE: only the charactersA-Z,0-9and_, with at least one letter.snake_case: only the charactersa-z,0-9and_, with at least one letter.CamelCase: starts with an upper-case letter, has only letters and digits, and has at least one lower-case letter.- Anything else, including a name that starts with a digit, is
other. - Print
NAME: STYLEfor each name, in order.
Input
The first line has n (1 to 50). Each of the next n lines is one name without spaces.
Output
n lines like total_price: snake_case.
Constraints
1 ≤ n ≤ 50.
Example
Input:
4
total_price
MAX_SIZE
LinkedList
my-var
Output:
total_price: snake_case
MAX_SIZE: UPPER_CASE
LinkedList: CamelCase
my-var: other
Why it works
The three styles have disjoint character rules, so testing the characters of each name against them gives exactly one answer; everything that fits none of them is other.
Examples
4 total_price MAX_SIZE LinkedList my-var
total_price: snake_case MAX_SIZE: UPPER_CASE LinkedList: CamelCase my-var: other
3 x X Xy
x: snake_case X: UPPER_CASE Xy: CamelCase
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use the string methods isupper() and islower() on the whole name, and any(c.isalpha() ...) to find a letter.
Hint 2
A name that is empty or starts with a digit is other.
Hint 3
CamelCase starts with an upper-case letter, has only letters and digits, and has some lower-case letter.
Solution
Show one correct solution (try it yourself first)
n = int(input())
for _ in range(n):
name = input().strip()
has_letter = any(ch.isalpha() for ch in name)
if not name or name[0].isdigit():
style = "other"
elif has_letter and all(ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" for ch in name):
style = "UPPER_CASE"
elif has_letter and all(ch in "abcdefghijklmnopqrstuvwxyz0123456789_" for ch in name):
style = "snake_case"
elif name[0].isupper() and name.isalnum() and any(ch.islower() for ch in name):
style = "CamelCase"
else:
style = "other"
print(f"{name}: {style}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step