一个群里看到的面试题

问题

给定一个字符串,字符串里有大写、小写,如何统计这个字符串中敲击了多少次CapsLk键

个人思路

先统计大写的出现次数,然后将大写出现的次数乘以two,不知道有没有别的好思路呢~

def test_caps(str,caps=False):
    i=0
    for word in str:
        if word.islower() == False and caps == False:
            i+=1
            caps=True
        if word.islower() == True and caps == True:
            i+=1
            caps=False
    print(i)

我的思路

1 个赞
len(re.findall(r"[A-Z](?=[a-z])|[a-z](?=[A-Z])",in_str))

如果这个字符串只包含字母的话,感觉这样可行?

我是这样想的,遍历一边字符串,期间如果出现大小写变更count+1