Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Last active July 20, 2019 01:41
Show Gist options
  • Save ZechCodes/0d907ab71121a7f6f4e21d5d6805cf47 to your computer and use it in GitHub Desktop.
Save ZechCodes/0d907ab71121a7f6f4e21d5d6805cf47 to your computer and use it in GitHub Desktop.
import csv
import pandas as pd
from functools import reduce
#input below
ranked_fighters = {'Leon Edwards': {'Rank': 7}, 'Rafael Dos Anjos': {'Rank': 16}, 'Walt Harris': {'Rank': 2}, 'Aleksei Oleinik': {'Rank': 9}, 'Juan Adams': {'Rank': 1}, 'Greg Hardy': {'Rank': 4}, 'Dan Hooker': {'Rank': 3}, 'James Vick': {'Rank': 15}, 'Alexander Hernandez': {'Rank': 6}, 'Francisco Trinaldo': {'Rank': 20}, 'Ben Rothwell': {'Rank': 10}, 'Andrei Arlovski': {'Rank': 14}, 'Alex Caceres': {'Rank': 19}, 'Steven Peterson': {'Rank': 11}, 'Irene Aldana': {'Rank': 8}, 'Raquel Pennington': {'Rank': 24}, 'Klidson Abreu': {'Rank': 5}, 'Sam Alvey': {'Rank': 21}, 'Jennifer Maia': {'Rank': 13}, 'Roxanne Modafferi': {'Rank': 22}, 'Ray Borg': {'Rank': 17}, 'Gabriel Silva': {'Rank': 23}, 'Dom Pilarte': {'Rank': 12}, 'Felipe Colares': {'Rank': 26}, 'Jinsoo Son': {'Rank': 18}, 'Mario Bautista': {'Rank': 25}}
all_combinations = [row for row in csv.reader(open("C:/Users/micha/Desktop/Python/Diversification_data.csv", "r"))]
all_combinations.sort(key=lambda row: row[-1], reverse=True)
top_combinations = []
next_rank = 1
worst_rank = reduce(
lambda fighter_a, fighter_b: fighter_a
if fighter_a["Rank"] > fighter_b["Rank"]
else fighter_b,
ranked_fighters.values(),
)["Rank"]
rank_counts = [0] * worst_rank
recent_ranks = [-1] * 3
for _ in range(20):
recent_ranks.pop(0)
recent_ranks.append(next_rank)
for index, combination in zip(range(len(all_combinations)), all_combinations):
# Don't use the current combination if any rank has more than 8 fighters
if True or not any(map(lambda fighter: rank_counts[ranked_fighters[fighter]["Rank"] - 1] >= 8, combination[:6])): #45 is the max of a specific rank
# Don't use the current combination if it shares 5 fighters with an already chosen combination
if True or not any(map(lambda lineup: len(set(combination[:6]) & set(lineup[:6])) >= 5, top_combinations)):
for fighter in combination[:6]:
if ranked_fighters[fighter]["Rank"] == next_rank:
print(f"Found match at {index:3} for rank {next_rank:2}: {fighter}")
# Count the fighters in the combination we found
for _fighter in combination[:6]:
rank_counts[ranked_fighters[_fighter]["Rank"] - 1] += 1
top_combinations.append(all_combinations.pop(index)) # Add the combination we found
break # Stop looking for next_rank
else:
# This only happens when break isn't used
continue # Current combination isn't invalid so skip back to the top of the loop
break # Current combination was valid so stop looping
else:
# This only happens when break isn't used
print(
f"No combination could be found for {next_rank}, changing worst rank and moving on"
)
ranks_check_next = [
rank + 1
for rank in range(worst_rank - 1)
if rank + 1 not in recent_ranks and rank_counts[rank] <= rank_counts[rank + 1] #rank is the rank of a specific rank
]
next_rank = ranks_check_next[-1] if len(ranks_check_next) else worst_rank
with open('play16_8.csv', 'w', newline='') as f:
thewriter=csv.writer(f)
thewriter.writerow(['Player 1','Player 2','Player 3','Player 4','Player 5','Player 6','Score'])
for comb in top_combinations:
thewriter.writerow(comb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment