+ 11

Strange out put on CamelCase vs camelCase Why ???

I ran several test on words and sentences and my code worked yet when I ran the two versions of CamelCase mentioned above I received two different results. CamelCase converted to all lowercase and no underscores... camelcase 🤔 camelCase converted to all lowercase as well but correct form as camel_case 👍 As I mentioned, I ran other test from AbraCadAbra ( abra_cadabra 🤔) to DicTionAry ( dic_tion_ary 👍) to AntiDisEstablishMenTarianIsm ( anti_dis_establish_men_tarian_ism 👍 ) I thought this was odd as this a SnakeCase code and yet when execution gave two different results .. https://code.sololearn.com/c0Z2xn1iJ8We/?ref=app

14th Mar 2023, 1:52 AM
BroFar
BroFar - avatar
10 Respuestas
+ 5
BroFar the conditions that cause failure are: 1. The first letter is capitalized and 2. Another letter in the word matches that first letter. It will skip inserting the underscore because of the logic in line 4 where it checks for a match with the first letter.
14th Mar 2023, 3:00 AM
Brian
Brian - avatar
+ 4
Thanks Jay Matthews but why does that print statement have to be outside the for loop ? It did correct both the CamelCase entry and the AbraCadAbra entry .. print(x[0].lower(),end="") understandable that x[0] comparison is not needed in the flow of the for loop.
14th Mar 2023, 2:13 AM
BroFar
BroFar - avatar
+ 4
Thank you Jay Matthews But still don't exactly understand the other oddities like why the no underscore in CamelCase and the second missing underscore in AbraCadAbra as _abra .. The if condition was an 'and' not an 'or' so elif condition is isupper() boolean by itself.
14th Mar 2023, 2:29 AM
BroFar
BroFar - avatar
+ 4
Ah yes that makes totally sense.. Thanks Brian I see the error of my ways.
14th Mar 2023, 3:06 AM
BroFar
BroFar - avatar
+ 4
Saheed Kehinde please I understand that you are new here. Only add comments to threads that are of value to the quality of the threads.
15th Mar 2023, 6:20 PM
BroFar
BroFar - avatar
+ 3
You're welcome, BroFar. I find that it can be hard to find a bug when I am overconfident that my good intentions translated to a good implementation. It's a good example to explain to newbies why testing is necessary.
14th Mar 2023, 3:27 AM
Brian
Brian - avatar
+ 2
why the multiple print(end='')? surely it would be more efficient to build a string then print at the end? s = '' for c in input(): if c.isupper(): s += f'_{c.lower()}' else: s += c s = s.lstrip('_') print(s) or one liner: print(''.join(['_'+c.lower() if c.isupper() else c for c in input()]).lstrip('_')) and because it's Python, there's a library for this. 😁 https://code.sololearn.com/cqPOJTVZVV4I/?ref=app
14th Mar 2023, 10:02 AM
Bob_Li
Bob_Li - avatar
+ 1
But there is always the edge case of multiple consecutive uppercase that is always messed up... example: IOError or HTTPRequest
14th Mar 2023, 10:41 AM
Bob_Li
Bob_Li - avatar
+ 1
It's PascalCase and cameCase
15th Mar 2023, 9:15 PM
Diego Sealtiel Valderrama García
Diego Sealtiel Valderrama García - avatar
+ 1
Hello
16th Mar 2023, 5:27 PM
Saheed Kehinde
Saheed Kehinde - avatar