123456789101112131415161718192021222324252627282930313233343536373839 |
- #!/usr/bin/env python3
- import csv
- from collections import defaultdict
- data = []
- with open("database.csv") as infile:
- for name, *nums in csv.reader(infile, delimiter=",", quotechar="'"):
- data.append((name, *[float(n) for n in nums]))
- counts = defaultdict(int)
- results = {}
- try:
- with open("best.csv") as infile:
- for r, g, b, name, score in csv.reader(infile, delimiter=",", quotechar="'"):
- results[(int(r), int(g), int(b))] = (name, float(score))
- except:
- pass # file not found, assume no prior results
- step = 8
- for r in range(0, 256, step):
- for g in range(0, 256, step):
- for b in range(0, 256, step):
- if (known := results.get((r, g, b), None)) is not None:
- counts[known[0]] += 1
- continue
- best_score, best_name = min((x - r * yr - g * yg - b * yb, name) for name, x, yr, yg, yb in data)
- results[(r, g, b)] = (best_name, best_score)
- counts[best_name] += 1
- with open("best.csv", "w") as outfile:
- csv.writer(outfile, delimiter=",", quotechar="'").writerows((*k, *v) for k, v in results.items())
- with open("counts.csv", "w") as outfile:
- csv.writer(outfile, delimiter=",", quotechar="'").writerows(counts.items())
- print(f"Top ten most hit:")
- for k in sorted(list(counts), key=counts.get, reverse=True)[:10]:
- print(f"{k} - {counts[k]}")
|