Home > Net >  Removing duplicate from string challenge
Removing duplicate from string challenge

Time:09-26

i have one problem i want to remove the duplicates from string for example if i have uppercase letter and second letter is lowercase of same type (C,c) i want to remove the lowercase for example look the how is my output and how it should look like downwards in code , but i have idea when result finishes i want to iterate through each element and check if its for example letter of same type but i coudn't implement it.

def camel_case(string):
    result = ''
    new_string = string.capitalize()
    for i in range(len(new_string)):
        if new_string[i] == " ":
            result  = new_string[i 1].upper()
        else:  
            result  = new_string[i]
    return result 
print(camel_case("camel case method")) # my output: CamelCcaseMmethod , output for question should look like:CamelCaseMethod

CodePudding user response:

I have changed your function a bit.

def camel_case(string):
    t = False
    result = ''
    new_string = string.capitalize()
    for i in new_string:
        if i == " ":
            t = True
        else:
            if t:
                result  = i.upper()
            else: 
                result = i
            t = False
    return result 
print(camel_case("camel case method"))

You can use this one also

def camel_case(string):
    return string.title().replace(' ','') 
print(camel_case("camel case method"))

CodePudding user response:

Your solution for this callenge does not works effective:

Try this instead:

from re import sub

def camel_case(s):
    s = sub(r"(_|-) ", " ", s).title().replace(" ", "")
    return ''.join([s[0].lower(), s[1:]])

CodePudding user response:

Simple solution would be;

s.title().replace(" ","")
  • Related