fork download
  1. # 商品データ: (商品名, 価格, 評価, レビュー数)
  2. products = [
  3. ("item1", 3000, 4.8, 20),
  4. ("item2", 1800, 4.2, 200),
  5. ("item3", 5000, 4.9, 5),
  6. ("item4", 2500, 4.5, 80),
  7. ]
  8.  
  9. recommendations = []
  10.  
  11. for name, price, rating, reviews in products:
  12. # --- スコア計算のロジック ---
  13. # 1. 信頼度スコア: レビュー数が少なくても評価が高い商品(item3など)の急浮上を防ぐため、
  14. # レビュー数も加味した総合スコアを計算します。
  15. total_score = rating * (1 - (1 / (reviews + 1)))
  16.  
  17. # 2. コスパスコア: 1円あたりの総合スコア(分かりやすく1000倍しています)
  18. cost_performance = (total_score / price) * 1000
  19.  
  20. recommendations.append({
  21. "name": name,
  22. "price": price,
  23. "rating": rating,
  24. "reviews": reviews,
  25. "total_score": round(total_score, 2),
  26. "cp_score": round(cost_performance, 2)
  27. })
  28.  
  29. # --- 1. 総合おすすめ順(スコアが高い順)でソートして表示 ---
  30. print("🏆 【総合おすすめランキング】(評価×レビューの信頼度)")
  31. sorted_by_score = sorted(recommendations, key=lambda x: x["total_score"], reverse=True)
  32. for i, p in enumerate(sorted_by_score, 1):
  33. print(f"{i}: {p['name']} (スコア: {p['total_score']}) - 価格: {p['price']}, 評価: {p['rating']}, レビュー: {p['reviews']}件")
  34.  
  35. print("\n" + "="*50 + "\n")
  36.  
  37. # --- 2. コスパおすすめ順(価格あたりの満足度が高い順)でソートして表示 ---
  38. print("💰 【コスパおすすめランキング】(価格の手頃さ重視)")
  39. sorted_by_cp = sorted(recommendations, key=lambda x: x["cp_score"], reverse=True)
  40. for i, p in enumerate(sorted_by_cp, 1):
  41. print(f"{i}: {p['name']} (コスパ: {p['cp_score']}) - 価格: {p['price']}, 評価: {p['rating']}, レビュー: {p['reviews']}件")
Success #stdin #stdout #stderr 0.02s 6964KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/x0mDZG/prog:6:29: Syntax error: Illegal start of term
ERROR: /home/x0mDZG/prog:13:83: Syntax error: Operator expected
ERROR: /home/x0mDZG/prog:17:30: Syntax error: Operator expected
ERROR: /home/x0mDZG/prog:29:30: Syntax error: Operator expected
ERROR: /home/x0mDZG/prog:41:143: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit