1
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?

「電脳少女プログラミング2088」Dランク問題「カジノ」をコーディングしてみた

Last updated at Posted at 2025-02-23

この記事では「電脳少女プログラミング2088 ─壊レタ君を再構築─」のDランク問題 カジノ の解答コードをResultとChatGPTを使ってリファクタリングしていきます。

方針

  • 各チップの「額面×枚数」を合計する
  • 以下の順で処理:
    1. チップの額面リストを作成
    2. 標準入力から各チップの枚数を取得
    3. 合計金額を算出
    4. 合計金額を出力
values = [1, 5, 10]
total = 0

for value in values:
    x = int(input())
    total += x * value

print(total)

Reference: int() | input() | print()

改善

ChatGPTを使って可読性や効率を意識してみる:

chip_values = [1, 5, 10]
total_amount = sum(int(input()) * value for value in chip_values)
print(total_amount)

Reference: sum()

改善点:

  • 変数名の最適化
    • valueschip_values :チップの額面リスト
    • total → total_amount :合計金額
  • forループから内包表記+sum() で一行にまとめた
  • forループから組み込み関数の sum() 関数にまとめたことで処理速度が向上

終わりに

このイベントを通してコードをリファクタリングすることに慣れていきたい。あと、せっかくリファレンスを作成したので時間がある時に見返したい。

1
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
1
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?