0
0

【Python】Atcoder勉強記録⑦

Last updated at Posted at 2024-01-30

Atcoderの勉強した記録を書いていきます。
勉強の参考にしてください。
解いた問題一覧

過去に投稿した記事

Atcoder勉強記録
Atcoder勉強記録
Atcoder勉強記録
Atcoder勉強記録
Atcoder勉強記録
Atcoder勉強記録

1.問題①

A - Capitalized?Difficulty21

解答

S = input()

if len(S) == 1:
    if S.isupper():
        print('Yes')
    else:
        print('No')
else:
    flg_upper = S[0].isupper() # 大文字かを判断
    flg_lower = S[1:].islower() # 小文字か判断

    if flg_upper and flg_lower:
        print('Yes')
    else:
        print('No')

解法

1文字で大文字だった場合は「Yes」をする。
文字列をスライスで受け取り、1文字が大文字か、1文字目以降が全て小文字なのかをpythonのメソッドを使用して確認する。

1.問題②

B - FrequencyDifficulty48

解答

from collections import defaultdict
S = input()
cnt = defaultdict(int)
for i in S:
    cnt[i] += 1

ans = 'a'
for i in sorted(cnt):
    if cnt.get(i) == None:
        continue
    if cnt.get(i) > cnt[ans]:
        ans = i
print(ans)

解法

defaultdictを使用して辞書を作成する。
defaultdictとは、どのようなキーがきても指定したデフォルト値を初期値として要素を追加します。
初期値でint型をセットしています。
image.png
上記画像のように、cntの変数で辞書が作成されています。
後は、getメソッドを使用して出現回数の多い文字を確認してansに答えを格納します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0