|
| 1 | +""" |
| 2 | +
|
| 3 | +SCREAMING_SNAKE_CASE |
| 4 | +Given a string representing a variable name, return the variable name converted to SCREAMING_SNAKE_CASE. |
| 5 | +
|
| 6 | +The given variable names will be written in one of the following formats: |
| 7 | +
|
| 8 | +camelCase |
| 9 | +PascalCase |
| 10 | +snake_case |
| 11 | +kebab-case |
| 12 | +In the above formats, words are separated by an underscore (_), a hyphen (-), or a new word starts with a capital letter. |
| 13 | +
|
| 14 | +To convert to SCREAMING_SNAKE_CASE: |
| 15 | +
|
| 16 | +Make all letters uppercase |
| 17 | +Separate words with an underscore (_) |
| 18 | +""" |
| 19 | + |
| 20 | +import unittest |
| 21 | + |
| 22 | +class ScreamingSnakeCaseTest(unittest.TestCase): |
| 23 | + |
| 24 | + def test1(self): |
| 25 | + self.assertEqual(to_screaming_snake_case("userEmail"),"USER_EMAIL") |
| 26 | + |
| 27 | + |
| 28 | + def test2(self): |
| 29 | + self.assertEqual(to_screaming_snake_case("UserPassword"),"USER_PASSWORD") |
| 30 | + |
| 31 | + def test3(self): |
| 32 | + self.assertEqual(to_screaming_snake_case("user_id"),"USER_ID") |
| 33 | + |
| 34 | + |
| 35 | + def test4(self): |
| 36 | + self.assertEqual(to_screaming_snake_case("user-address"),"USER_ADDRESS") |
| 37 | + |
| 38 | + def test5(self): |
| 39 | + self.assertEqual(to_screaming_snake_case("username"),"USERNAME") |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +def to_screaming_snake_case(variable_name): |
| 44 | + result = [] |
| 45 | + |
| 46 | + if '-' in variable_name: |
| 47 | + result = variable_name.split('-') |
| 48 | + elif '_' in variable_name: |
| 49 | + result = variable_name.split('_') |
| 50 | + |
| 51 | + res = "" |
| 52 | + |
| 53 | + if not result: |
| 54 | + for index, char in enumerate(variable_name): |
| 55 | + if char.isupper() and index != 0: |
| 56 | + res += "_"+char |
| 57 | + else: |
| 58 | + res += char.upper() |
| 59 | + |
| 60 | + |
| 61 | + return res if res else '_'.join(result).upper() |
| 62 | + |
| 63 | + |
| 64 | +""" |
| 65 | +Minor Issues for the above solution |
| 66 | +
|
| 67 | +1. Initialization of result |
| 68 | + -> You set result = [] and later overwrite it with a list of words if - or _ is found. |
| 69 | + -> That works, but it's a bit confusing because result is sometimes a list of words and sometimes just an empty list. |
| 70 | +
|
| 71 | +2. Mixed fomats |
| 72 | + -> if a variable name consists both - and _ (eg. "myVar-name_test"), the above code only handles once separator. |
| 73 | + -> This solution words it explicitly mentioned for kebab-case, PascalCase, snake_case and camelCase . there is no case involving both the '-' and '_' together. |
| 74 | +
|
| 75 | +This solution is funcationally correct and meets the requirements. The refinements below just make it more robust and concise. |
| 76 | +""" |
| 77 | + |
| 78 | + |
| 79 | +import re |
| 80 | +def to_screaming_snake_case(variable_name): |
| 81 | + # Step 1: Replace hyphens with underscores |
| 82 | + |
| 83 | + variable_name = variable_name.replace('-','_') |
| 84 | + |
| 85 | + |
| 86 | + # Step 2: Insert underscores before capital letters (for camelCase/ PascalCase) |
| 87 | + variable_name = re.sub(r'([a-z0-9])([A-Z])',r'\1_\2',variable_name) |
| 88 | + |
| 89 | + return variable_name.upper() |
| 90 | + |
| 91 | + |
| 92 | +""" |
| 93 | +
|
| 94 | +
|
| 95 | + Example: "UserPassword" |
| 96 | +- First two characters: "Us" → U is uppercase, s is lowercase. |
| 97 | +Regex looks for lowercase/digit followed by uppercase. Here we have uppercase followed by lowercase, so no match. |
| 98 | +→ No underscore before the first U. |
| 99 | +- Later: "rP" → r is lowercase, P is uppercase. |
| 100 | +Regex matches this pair. |
| 101 | +Replacement: "r_P". |
| 102 | +→ Underscore inserted between r and P. |
| 103 | +Final result: "User_Password" → then .upper() → "USER_PASSWORD". |
| 104 | +
|
| 105 | +✅ Why it doesn’t add _ before the first U |
| 106 | +Because the regex requires a lowercase/digit before the uppercase. |
| 107 | +At the start of "UserPassword", the U has no preceding lowercase/digit — it’s the first character. So the pattern doesn’t match there. |
| 108 | +
|
| 109 | +🧠 Key Takeaway |
| 110 | +- The regex only splits at lowercase→uppercase boundaries (like rP, eM, dA). |
| 111 | +- It does not split at the very beginning if the string starts with an uppercase letter. |
| 112 | +- That’s why "UserPassword" becomes "USER_PASSWORD", not "_USER_PASSWORD". |
| 113 | +
|
| 114 | +=> Hyphens (-) -> underscores (_) |
| 115 | +=> Insert underscores before capital letters to split camelCase/PascalCase. |
| 116 | +=> Convert everything to uppercase. |
| 117 | +=> Works for all four formats. |
| 118 | +
|
| 119 | +""" |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + print(to_screaming_snake_case("userNamePassword")) |
| 126 | + unittest.main() |
0 commit comments