Amazon OA Question
Anonymous User
440

Given a string, convert all snake case words into camel case. However, underscores at the start or end of the word are to be presevered.

Ex:

My name is bob_and I like __churros_and_tacos__
Expected output:

My name is bobAnd I like __churrosAndTacos__
im not sure how to solve this and account for the start and end underscores

this is the code I have but not sure if it works

def run(w):

    l=0
    r=len(w)-1
    while w[l] == '_':
        l+=1

    while w[r]== '_':
        r-=1
    subString = w[l:r+1]

    ws = subString.split('_')
    ans=ws[0]+"".join(w.title() for w in ws[1:])

    while l >0:
        ans ="_"+ans
        l-=1

    while r < len(w)-1:
        ans+='_'
        r+=1
    return ans
snake_case_word = 'My name is bob_and I like __churros_and_tacos__'


words = snake_case_word.split(' ')
ans=[]
for w in words:
    if '_' in w:
        ans.append(run(w))
    else:
        ans.append(w)

print(" ".join(ans))
Comments (2)