+ 2
[Solved] Camel to Snake Python Coding Challenge
I'm currently working on a Camel to Snake Python coding challenge and have managed to pass all the test cases except the last one. Below is the code I've written: camelCaseText = input() snake_case_text = "" for pos, letter in enumerate(camelCaseText): x = letter if letter.isupper(): x = "_" + letter.lower() snake_case_text += x print(snake_case_text) Unfortunately, I'm struggling to identify what's the issue with the code. Could someone explain where I might be going wrong?
9 Respostas
+ 6
Parveen ,
the task description says:
`Every capital letter is replaced with its lowercase prefixed by an underscore _, except for the first letter, which is lowercased without the underscore, so that SomeName becomes some_name.`
so we can assume that the input of the last test case starts with an uppercase letter.
try to rework your code to fix this issue.
+ 2
Someone asked a similar question before, so I will answer a similar answer this time.
What would happen if the string is "IsItFun"?
+ 2
Lothar
I only read the upper part of question once that's why I couldn't figure out the problem anyway thanks .
+ 2
Mihaly Nyilas ,
your last comment might be right, but not for sololearn.
i have prepared a code that differs only in handling of the first letter. so the last test case provides a word starting with an upper case letter.
+ 1
Can you link the camel to snake challenge? I’m not sure how to find it otherwise.
+ 1
Parveen Sorry, when I click some links (including that one), Sololearn app goes bye-bye.
+ 1
import string
L = list(string.ascii_lowercase)
U = list(string.ascii_uppercase)
a = input()
if a[0] in U:
a = a.replace(a[0], L[U.index(a[0])], 1)
for x in a[1:]:
if x in U:
i = U.index(x)
a = a.replace(x, "_" + L[i], 1)
print(a)