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?

数字3つの大小比較「数字3つの大小比較をどのように表現したらいいですか。何パターンですか。同じもあります。」をAI先生に聞きました。

Last updated at Posted at 2025-01-24

ChatGPT先生へ

・1行ずつです。
>(回答抜粋)
上記について、pythonで、変数a,b,cに値を代入して、変数名で大小の表示をお願いします。
記号a,b,cで
a=5,b=5,c=3に対応して下さい。
申し訳ありません。大きい順にお願いします。
(2025-01-25追加)
最後の行に、値の比較行を追加して下さい。
最後の行は、5=5>3になるようにお願いします。
申し訳ありません。ユーザー定義関数にして下さい。

def compare_variables(**kwargs):
    # 変数名と値をペアにしてリスト化
    variables = list(kwargs.items())

    # 値を基準に降順でソート (同じ値でも順番を保つ)
    sorted_vars = sorted(variables, key=lambda x: x[1], reverse=True)

    # 結果を組み立て (等しい値は "=" で表現)
    result = []
    values = []
    for i in range(len(sorted_vars)):
        if i > 0 and sorted_vars[i][1] == sorted_vars[i-1][1]:
            result[-1] += f" = {sorted_vars[i][0]}"
            values[-1] += f"={sorted_vars[i][1]}"
        else:
            result.append(sorted_vars[i][0])
            values.append(f"{sorted_vars[i][1]}")

    # 結果の出力
    print(" > ".join(result))
    print(" > ".join(values))


# 使用例
a = 5
b = 5
c = 3

compare_variables(a=a, b=b, c=c)

実行例

a = b > c
5=5>3

Gemini先生へ

copilot先生へ

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)  開発環境

いつもと違うおすすめです。

Qiita

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?