codewars:Convert string to camel case

代码

import string
def to_camel_case(text):
    print text
    word=list(text)
    for i in range(0,len(word)-2):
        if word[i]=='-' or word[i]=='_' or word[i]=='=':
            word[i+1]=word[i+1].upper()
            #del(word[i])#用红字将会出现out of range 的情况
            #i=i-1
    a=''
    for x in word:
        if x=='-' or x=='_' or x=='=':
            a=a
        else:
            a+=x
    return a     

测试代码

test.describe("Testing function to_camel_case")
test.it("Basic tests")
test.assert_equals(to_camel_case(''), '', "An empty string was provided but not returned")
test.assert_equals(to_camel_case("the_stealth_warrior"), "theStealthWarrior", "to_camel_case('the_stealth_warrior') did not return correct value")
test.assert_equals(to_camel_case("The-Stealthtext=Warrior"), "TheStealthWarrior", "to_camel_case('The-Stealth-Warrior') did not return correct value")
test.assert_equals(to_camel_case("A-B-C"), "ABC", "to_camel_case('A-B-C') did not return correct value")

1.不能直接用‘=’对字符串进行修改,必须使用相关函数。为了方便修改,最好将其列表化。对于一整个字符串,需使用list(string),而对于字符串块,则可以使用string.split().
2.小写转化成大写用.upper。大写转化成小写用.lower。
3.对于数组中删除某个元素这一点,我实在是掌握不好i的取值,与其这样,不如灵活使用字符串的+,以及数组的append。
4.学习不好好想死啊。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值