+ 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?

11th May 2024, 1:25 PM
Parveen
Parveen - avatar
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.
11th May 2024, 3:10 PM
Lothar
Lothar - avatar
+ 2
Someone asked a similar question before, so I will answer a similar answer this time. What would happen if the string is "IsItFun"?
11th May 2024, 2:01 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Lothar I only read the upper part of question once that's why I couldn't figure out the problem anyway thanks .
11th May 2024, 3:29 PM
Parveen
Parveen - avatar
+ 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.
12th May 2024, 2:45 PM
Lothar
Lothar - avatar
+ 1
Can you link the camel to snake challenge? I’m not sure how to find it otherwise.
11th May 2024, 1:46 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
11th May 2024, 1:51 PM
Parveen
Parveen - avatar
+ 1
Parveen Sorry, when I click some links (including that one), Sololearn app goes bye-bye.
11th May 2024, 3:18 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 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)
11th May 2024, 5:23 PM
JaiprakashJH
JaiprakashJH - avatar
+ 1
this is just a note Lothar, Parveen: camelCase does not start with a capital letter, but PascalCase does
11th May 2024, 6:26 PM
Mihaly Nyilas
Mihaly Nyilas - avatar