0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[paizaラーニング]ランクCの問題をPythonでチャレンジしてみた

Posted at

pythonの学習のため、paizaラーニングのCランクのプログラミングスキルチェック問題を
5つ回答してみました。以下の私の回答はpaizaのテストケースを
すべて通過したものとなります。

問題1:【キャンペーン問題】枠で囲む

S = input()
len_S = len(S)

for i in range(len_S + 2):
    print("+",end="")
    
print(f"\n+{S}+\n",end="")

for i in range(len_S + 2):
    print("+",end="")

割と直感的に答えられるシンプルな問題でした

問題2:プレゼント応募企画の実施

N,A,B = (int(i) for i in input().split())
for x in range(1,N+1):
    if x % A == 0 and x % B == 0:
        print("AB")
    elif x % A == 0:
        print("A")
    elif x % B == 0:
        print("B")
    else:
        print("N")

ほぼFizzBuzz問題のようなものでした。

問題3:戦闘シミュレーション

N,L = (int(x) for x in input().split())
list_x = []
for i in range(N):
    list_x.append(int(input()))
for i in list_x:
    if L > i:
        L = L + (i // 2)
    elif L < i:
        L = L // 2
print(L)

問題をよく読んだらやっていることは結構単純だなと思いました。

問題4:じゃんけんの結果

janken_count = int(input())
alice_win_count = 0
list_alice = []
list_bob = []

for i in range(janken_count):
    A,B = input().split()
    list_alice.append(A)
    list_bob.append(B)

for i in range(janken_count):
    if (list_alice[i] == "G" and list_bob[i] == "C") or (list_alice[i] == "C" and list_bob[i] == "P") or (list_alice[i] == "P" and list_bob[i] == "G"):
        alice_win_count += 1

print(alice_win_count)

じゃんけんの勝敗の条件分岐のところをもう少しスマートにかけるといいかなと振り返って思いました。

問題5:怪盗からのメッセージ

message = input()
new_message = ""
keep_num = ""
alphabet_list = [chr(i)for i in range(65,91)]

for i,alpha in enumerate(message):
    if not alpha in alphabet_list:
        if keep_num:
            keep_num += alpha
        else:
            keep_num = alpha
        continue
    if not keep_num == "":
        int_keep_num = int(keep_num) + 1
        if new_message == "":
            new_message = str(int_keep_num)
        else:
            new_message += str(int_keep_num)
        keep_num = ""
    idx = alphabet_list.index(alpha)
    if alpha == "Z":
        if new_message == "":
            new_message = "A"
        else:
            new_message += "A"
    elif new_message == "":
        new_message = alphabet_list[idx+1]
    else:
        new_message += alphabet_list[idx+1]

if not keep_num == "":
    int_keep_num = int(keep_num) + 1
    new_message += str(int_keep_num)
print(new_message)

処理をべた書きしすぎてかなり冗長になってしまっているので、
数字を加算する処理やメッセージを置換する処理など複数回出てきているものは
関数にしてしまったほうが良かったなと思いました。

Bランクの問題もいけるかな?と思って試しに一問(Bランク:ドーナツ)解こうとしましたが
撃沈しました。ひとまずCラング問題が大方解けるようになるまでどんどんチャレンジしていきたいと思います。

今後の課題
・変数名を誰が見てもわかるようにする
・コードを書く時間を短縮する
・問題を解く数を増やしていく

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?