fork download
  1. # -*- coding: utf-8 -*-
  2.  
  3. # 商品データ: (商品名, 価格, 評価, レビュー数)
  4. products = [
  5. ("item1", 3000, 4.8, 20),
  6. ("item2", 1800, 4.2, 200),
  7. ("item3", 5000, 4.9, 5),
  8. ("item4", 2500, 4.5, 80),
  9. ]
  10.  
  11. recommendations = []
  12.  
  13. for name, price, rating, reviews in products:
  14. # 【おすすめスコアの計算ロジック】
  15. # 評価が高くてもレビュー数が極端に少ない商品(サクラなどのリスク)を弾くため、
  16. # レビュー数に応じた信頼度(重み)を評価に掛け合わせます。
  17. # ※レビュー数が多いほど 1 に近づき、本来の評価が活きます。
  18. reliability = 1 - (1 / (reviews + 1))
  19. score = rating * reliability
  20.  
  21. recommendations.append({
  22. "name": name,
  23. "price": price,
  24. "rating": rating,
  25. "reviews": reviews,
  26. "score": round(score, 2)
  27. })
  28.  
  29. # スコア(score)が高い順にソート(並び替え)
  30. ranked_products = sorted(recommendations, key=lambda x: x["score"], reverse=True)
  31.  
  32. # 結果の出力
  33. print("=== おすすめ商品ランキング ===")
  34. for rank, p in enumerate(ranked_products, 1):
  35. print(f"{rank}: {p['name']}")
  36. print(f" [スコア] {p['score']} 点")
  37. print(f" [詳細] 価格: {p['price']}| 評価: {p['rating']} | レビュー数: {p['reviews']}件")
  38. print("-" * 40)
Success #stdin #stdout #stderr 0.02s 6884KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/7nYVun/prog:38:18: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit