5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python3】【電脳少女プログラミング2088 ─壊レタ君を再構築─】「自然の残る公園」をchatGPTにコードレビューしてもらった。

Last updated at Posted at 2025-02-17

Qiita公式で投稿のネタを提供してくださったので...

paizaの新作プログラミングゲーム【電脳少女プログラミング2088 ─壊レタ君を再構築─】自然の残る公園(paizaランク:C相当)をPython3で実装しました。


問題


内容

image.png

現在地の座標から最短の座標の距離(ユークリッド距離)を求める問題だったので、
実装後、chatGPTにコードレビューしてもらって、その後に改良したコードもレビューしてもらいました。

注意
情報漏洩の危険性があるため、
コードに個人情報、認証情報、顧客情報が含まれる場合は注意してください。

最初のコード

import numpy #平方を求めたい

#ビーコンの数、現在地x座標、現在地y座標
beacon_num, now_x, now_y = list(map(int, input().split()))
dict_distance = {} #キー:ビーコン番号 バリュー:距離

#ビーコンの回数分ループ
for i in range(beacon_num):
    #まずはビーコン番号:[x座標, y座標]の形で辞書を作る
    dict_distance[i+1] = list(map(int, input().split()))
    #現在の座標との距離を計算する
    distance = ((now_x - dict_distance[i+1][0]) **2  + (now_y - dict_distance[i+1][1]) **2) 
    #平方根を計算して結果をビーコン番号:距離の形で格納
    dict_distance[i+1] = int(numpy.sqrt(distance))

#最終的な答えを入れる変数を作成して0で初期化
answer = 0
#辞書のバリューを比較するためのダミーのデータを入れておく(後述のif文で必ずTrueが返る値)
test_case = 10000000

#距離の比較をして一番小さい値のビーコン番号がanswerに格納されるようにループ処理
for key, value in dict_distance.items():
    if value <= test_case:
        answer = key
        test_case = value
        
#結果を表示
print(answer)

という感じで作りました。

結果

image.png

・数の比較ができればいいから平方根の計算がいらない
・結果欲しいのは最短距離を示すビーコンの番号だけだから他のビーコンの番号を記憶しておく必要ない

冷たい。
ということなので修正してみました。

2回目のコード

#ビーコンの数、現在地x座標、現在地y座標
beacon_num, now_x, now_y = list(map(int, input().split()))

#最終的な答えを入れる変数を作成して0で初期化
answer = 0
#辞書のバリューを比較するためのダミーのデータを入れておく(後述のif文で必ずTrueが返る値)
test_case = 10000000

#ビーコンの数分ループ(iはビーコンの番号として使う)
for i in range(beacon_num):
    #ビーコンのx座標、y座標を取得
    x, y = list(map(int, input().split()))
    #距離計算してresultへ格納
    result = (now_x - x) ** 2 + (now_y - y) ** 2
    #resultとtest_caseを比較する
    if result <= test_case:
        answer = i + 1 #indexは0からスタートするためビーコンの番号として機能するように+1をする
        test_case = result

print(answer)

numpyと辞書を使わなければこうなりました。

結果

image.png
褒めてくれました。

image.png
最初のレビューでは冷たかったのに今ではすっかりと仲良しになりました。

まとめ

問題の判定としては両コード間違いではないですが、ふと自分のコードってどうなの?って客観視したいときには
AIを有効活用して更なるスキルアップを目指したいですね!

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?