알고리즘 문제/CodeWar
Convert PascalCase string into snake_case
BEstyle
2022. 10. 29. 02:14
DESCRIPTION:
Complete the function/method so that it takes a PascalCase string and returns the string in snake_case notation. Lowercase characters can be numbers. If the method gets a number as input, it should return a string.
Examples
"TestController" --> "test_controller"
"MoviesAndBooks" --> "movies_and_books"
"App7Test" --> "app7_test"
1 --> "1"
def to_underscore(s):
s=str(s)
ans=""
for i in s:
if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
ans+="_"
ans+=i.lower()
else:
ans+=i
if ans[0]=="_":
return ans[1:]
return ans