見出し画像

atcoder練習問題⑥

今日も勉強した記録をnoteに記載致します。
atcoderを勉強している人は是非参考にしてください。

問題①

問題
出力例

解答コード

def Playing_Cards_Validation_v1():
    """自分の解答"""
    n = int(input())
    n_list = []
    n_chk = False
    cnt = 0
    for _ in range(n):
        n_1 = input() 
        n_list.append(n_1)
    # 2 文字目は A , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , T , J , Q , K
    for i in range(n):
        cnt = 0
        n_chk = False
        for j in range(2):
            if cnt == 0:
                cnt += 1
                if n_list[i][j] == 'H' or n_list[i][j] == 'D' or n_list[i][j] == 'C' or n_list[i][j] == 'S':
                    n_chk = True
            elif cnt == 1:
                if n_list[i][j] == 'A' or n_list[i][j] == '2' or n_list[i][j] == '3' or \
                    n_list[i][j] == '4' or n_list[i][j] == '5' or n_list[i][j] == '6' or n_list[i][j] == '7' or\
                    n_list[i][j] == '8' or n_list[i][j] == '9' or n_list[i][j] == 'T' or n_list[i][j] == 'J' or\
                    n_list[i][j] == 'Q' or n_list[i][j] == 'K':
                    n_chk = True
    if len(n_list) != len(set(n_list)): # 重複したデータが存在するのかを確認
        n_chk = False

    if n_chk:
        print('Yes')
    else:
        print('No')

解答コード②

def Playing_Cards_Validation_v2():
    """解答"""
    import re
    pattern = re.compile(r'([HDCS])(A|[2-9]|[TJQK])'# 正規表現パターン文字列をコンパイルして正規表現パターンオブジェクトを作成できる。
    n = int(input())
    strs = [input() for _ in range(n)]
    if any(not re.match(pattern, s) for s in strs): # 正規表現とマッチしているのか確認する。
        print("No")
    else:
        print("Yes" if len(set(strs)) == n else "No"# データが重複しているのか確認する。

問題②

問題
出力例

解答コード

NX = map(int, input().split())
n_list = list(map(int, input().split()))
cnt = 1
for i in n_list:
    if X == i:
        break
    cnt += 1
print(cnt)

以上になります。

この記事が参加している募集

この記事が気に入ったらサポートをしてみませんか?