每个其他字符的大写字符,但忽略符号和空格

时间:2018-04-20 00:00:43

标签: python python-3.x

我是一名练习脚本的新手,我有90%的想法,但是我被困在一部分上。我正在做dmoj上的mocking spongebob "challenge",它要求你将给定字符串大写的每个其他字符都设置为大写,但要求忽略符号和空格,并且只计算要计数的字母。

我能够使所有其他字符大写或小写,但我不知道如何忽略符号和空格?到目前为止,我会放弃批评。感谢您的时间。

meme1 = "I don't even know her like that"
meme2 = "You can't just make a new meme from a different Spongebob clip every 
couple of months"
meme3 = "I must have listened to that latest Ed Sheeran song 10000 times!"

memeFIN1 = [""] * len(meme1)
memeFIN2 = [""] * len(meme2)
memeFIN3 = [""] * len(meme3)

memeFIN1[1::2] = meme1[1::2].upper()
memeFIN2[1::2] = meme2[1::2].upper()
memeFIN3[1::2] = meme3[1::2].upper()

memeFIN1[::2] = meme1[::2].lower()
memeFIN2[::2] = meme2[::2].lower()
memeFIN3[::2] = meme3[::2].lower()

memeFIN1 = "".join(memeFIN1)
memeFIN2 = "".join(memeFIN2)
memeFIN3 = "".join(memeFIN3)

print(memeFIN1)
print(memeFIN2)
print(memeFIN3)

编辑: 答案中的所有以下解决方案都帮助我找到了自己,但是它们似乎都没有完全靠自己工作。在cheesits解决方案中,将计数器改为1开始工作,但没有意识到(作为一个完全的菜鸟)我做了不同的反击,基本上做同样的事情。这是对我有用的解决方案:

meme1 = "I don't even know her like that"
meme2 = "You can't just make a new meme from a different Spongebob clip every couple of months"
meme3 = "I must have listened to that latest Ed Sheeran song 10000 times!"

def spongebobify(meme):
    count = 0
    char = []

    for ch in meme:
        if ch.isalpha() and count % 2 == 1:
            char.append(ch.upper())
            count += 1
        elif ch.isalpha():
            char.append(ch.lower())
            count += 1
        else:
            char.append(ch)

    return ''.join(char)

m1 = spongebobify(meme1)
m2 = spongebobify(meme2)
m3 = spongebobify(meme3)

print (m1)
print (m2)
print (m3)

4 个答案:

答案 0 :(得分:4)

如果你想要一些可读的东西,试试这个:

def spongebobify(phrase):

    ## Turn every character lowercase
    phrase = phrase.lower()

    ## Keep track of how many letters you've seen
    counter = 0

    ## Create a list to hold characters
    chars = []

    ## Go through the entire string
    for ch in phrase:

        ## If this is a letter, increment
        if ch.isalpha():
            counter += 1

        ## If this is a letter and our counter is odd:
        if ch.isalpha() and counter % 2:
            chars.append(ch.upper())

        ## Otherwise, just add it as-is
        else:
            chars.append(ch)

    return ''.join(chars)

如果你想要一个班轮(无视效率):

def spongebobify(phrase):
    return ''.join([ch.lower() if ch.isalpha() and len([c for c in phrase[:i] if c.isalpha()]) % 2 else ch for i, ch in enumerate(phrase.upper())])

    ## Readable version:
    #return ''.join([
    #    ch.lower()
    #    if ch.isalpha()
    #    and len([c for c in phrase[:i] if c.isalpha()]) % 2
    #    else ch
    #    for i, ch in enumerate(phrase.upper())
    #])

答案 1 :(得分:0)

你需要使用循环,所以你只能在字符是字母时增加一个计数器,然后检查该计数器是奇数还是偶数。

def mock_spongebob(input):
    letters = 0
    result = ""
    for c in input:
        if c.isalpha():
            result += c.lower() if letters % 2 == 0 else c.upper()
            letters += 1
        else:
            result += c
    return result

答案 2 :(得分:0)

you can use a bit to indicate which lower/upper you're currently looking at, toggling the bit whenever an alpha character is found.

def speak_like_spongebob(phrase):
    case_bit = 0
    res = []
    for lower_upper in zip(phrase.lower(), phrase.upper()):
        res.append(lower_upper[case_bit])
        case_bit ^= lower_upper[0].isalpha()
    return ''.join(res)

答案 3 :(得分:0)

As others have mentioned, you will need to iterate through each character in the strings, determine whether it's a character or a symbol, and act accordingly.

The following program would print every other character in uppercase, not counting non-alphabetic characters:

meme1 = "I don't even know her like that"
meme2 = "You can't just make a new meme from a different Spongebob clip every couple of months"
meme3 = "I must have listened to that latest Ed Sheeran song 10000 times!"

for meme in [meme1, meme2, meme3]:
    count = 0
    meme_mod = ""
    for c in meme:
        if not c.isalpha():
            meme_mod += c
            continue
        elif count % 2 == 0:
            meme_mod += c.lower()
        else:
            meme_mod += c.upper()
        count += 1
    print meme_mod

This program would output:

i DoN't EvEn KnOw HeR lIkE tHaT
yOu CaN't JuSt MaKe A nEw MeMe FrOm A dIfFeReNt SpOnGeBoB cLiP eVeRy CoUpLe Of MoNtHs
i MuSt HaVe LiStEnEd To ThAt LaTeSt Ed ShEeRaN sOnG 10000 tImEs!