Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
with StringIO solution in Clear category for Conversion from CamelCase by karlian
from io import StringIO
def partition(iterable):
part = next(iterable) # input is non-empty !!!
for x in iterable:
if x.islower():
part +=x
else:
yield part.lower()
part = x
yield part.lower()
def from_camel_case(name: str) -> str:
with StringIO() as output:
print(*partition(iter(name)), sep="_", end="", file=output)
return output.getvalue()
if __name__ == '__main__':
print("Example:")
print(from_camel_case("Name"))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert from_camel_case("MyFunctionName") == "my_function_name"
assert from_camel_case("IPhone") == "i_phone"
assert from_camel_case("ThisFunctionIsEmpty") == "this_function_is_empty"
assert from_camel_case("Name") == "name"
print("Coding complete? Click 'Check' to earn cool rewards!")
May 10, 2022
Comments: