Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
All answers annotated "Conversion from CamelCase" solution in Clear category for Conversion from CamelCase by BootzenKatzen
# My solution without hints:
def from_camel_case(name: str) -> str:
caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Creating a string of capital letters to check against
new = "" # creating a new string to add letters to
for i in range(len(name)): # iterates through each letter of the name
if name[i] in caps and i > 0: # if the letter is a capital, and not the first letter
new += "_" + name[i] # add a _ in front of it, then add the letter
else: #if it's not a capital
new += name[i] # add just the letter
new = new.lower() # makes the whole string lowercase
return new # returns the underscore string
# Their solution:
def from_camel_case2(name: str) -> str:
words = [] # Establishing a list we can add to
for ch in name: # Iterating through the input text
if ch.isupper(): # If their is a capital letter (I forgot isupper() existed lol)
words.append(ch.lower()) # append the list with the lower case letter
else: # if it's not capitalized
words[-1] += ch # add it to the most recent entry on the list (the most recent capital letter)
return '_'.join(words) # once we have our list of lower-case words, we can join them together with "_"
# Bonus solution 1:
import re #importing regular expressions
def from_camel_case3(name: str) -> str:
return '_'.join(i.lower() for i in re.findall('[A-Z][^A-Z]*', name))
# I'm so bad at understanding regular expressions, please excuse me if I mess this up
# re.findall looks for a pattern in a string
# The [] indicate a set of characters, so our first set is [A-Z] so all capital letters
# The second set [^A-Z]* means anything not in that set of capital letters - so our lowercase letters
# and the * lets it keep adding to the string until the next capital letter
# so it's essentially breaking it into words based on the capital letters
# then we're making them lowercase with i.lower()
# and then ultimately joining them together with '_'.join()
# Bonus solution 2:
def from_camel_case4(name: str) -> str:
res = ''
for i in name:
res += f"_{i.lower()}" if i.isupper() else i
return res.lstrip("_")
# This is my first time seeing f-strings
# but from what I can tell, it's a method of re-formatting a string
# so we establish a new string
# and then for each letter in out input string
# it will format it to _lowercase if there is an uppercase letter
# or leave it be as a lowercase, and then add them all to the new string
# then lstrip() is used to remove leading characters of a string
# so we remove the extra '_' at the front
# Bonus solution 3:
def from_camel_case5(name: str) -> str:
for i in name: # for each letter in our input text
if i.isupper(): # if that letter is uppercase
name=name.replace(i,'_'+i.lower()) # replace that letter with "_" and the lowercase letter
return name[1:] # then we return name[1:] to exclude that first '_' at the beginning
print("Example:")
print(from_camel_case("MyFunctionName"))
# These "asserts" are used for self-checking
assert from_camel_case("MyFunctionName") == "my_function_name"
assert from_camel_case("IPhone") == "i_phone"
print("The mission is done! Click 'Check Solution' to earn rewards!")
April 17, 2023
Comments: