AtCoder ABC 104 B – AcCepted Python解説
AcCepted
文字列 S が与えられます。S のそれぞれの文字は英大文字または英小文字です。 S が次の条件すべてを満たすか判定してください。
S の先頭の文字は大文字の
AtCoder Beginner Contest 「AcCepted」A
である。
S の先頭から 3 文字目と末尾から 2 文字目の間(両端含む)に大文字のC
がちょうど 1 個含まれる。
以上のA
,C
を除く S のすべての文字は小文字である。
与えられた条件をひとつずつ実装していくだけなのですが面倒くさいです。
flagで条件を満たしているか管理していきます。
s = input()
flag = True
if s[0] != "A":
flag = False
if s[2: -1].count("C") != 1:
flag = False
for i in s:
if not "a" <= i <= "z" and i != "A" and i != "C":
flag = False
print("AC" if flag else "WA")
「A,Cを除くすべての文字は小文字である」に関しては、文字列SからAとCをreplaceを使う方法もあります。
s = input()
flag = True
if s[0] != "A":
flag = False
if s[2: -1].count("C") != 1:
flag = False
s = s.replace("A", "").replace("C", "")
if not s.islower():
flag = False
print("AC" if flag else "WA")
replaceで置き換えたら次のif文でislower関数を使い、すべての文字が小文字になっているか確認します。