def create_scoreboard(profiles): """Creates a scoreboard for all player profiles. :param profiles: The player profiles used add to the new scoreboard. :return: The newly created, unordered, scoreboard. """ scoreboard = Tree(lambda a, b: b - a) for name, stats in profiles: scoreboard.insert(stats.points, stats) return scoreboard
def __init__(self, ordered_seasons=List(), seasons=HashTable(), men=HashTable(), women=HashTable(), tournament_types=HashTable(), ranking_points=List(), men_scoreboard=Tree(), women_scoreboard=Tree()): self.running = True self.seasons = seasons.clone() self.ordered_seasons = ordered_seasons.clone() self.current_season = self.ordered_seasons.last() self.men = men.clone() self.women = women.clone() self.tournament_types = tournament_types.clone() self.ranking_points = ranking_points.clone() self.men_scoreboard = men_scoreboard self.women_scoreboard = women_scoreboard
def load_season_player_scoreboard(season_stats): """Loads and sorts a player scoreboard for a given season. :param season_stats: The seasons player statistics. :return: The sorted player statistics for the given season. """ scoreboard = Tree(lambda a, b: b - a) for name, player_stats in season_stats: scoreboard.insert(player_stats.points, player_stats) return scoreboard
def pipe_vs_tree(): print('-' * 120) print('Pitting trees against pipe sort for single modification ranking.') print('The tree is expected to be faster.') print('Season ranking uses trees for this reason.') print('Pipe Sort = Uses runs then merges, average O(n log n)') print('Trees = Binary search then insertion, average O(log n)') print('-' * 120) array = [] tree = Tree() for i in range(0, 5000): value = random.randint(0, 5000) array.append(value) tree.insert(value, value) value = random.randint(0, 5000) pipe_single(array, value) tree_single(tree, value) prompt_next()
def sort(self): """Collects all runs and iteratively merges each, smallest first. :return: The sorted array. """ # Collect the final run. self._runs.insert(len(self._run), self._run) self._run = List() self.consume = self.__run_init # Iteratively merge each of the runs, smallest first. while len(self._runs) > 1: new_runs = Tree() iterator = iter(self._runs) run_a = next_run(iterator) run_b = next_run(iterator) while run_a is not None and run_b is not None: merged = self._merge(run_a, run_b) new_runs.insert(len(merged), merged) run_a = next_run(iterator) run_b = next_run(iterator) if run_a is not None: new_runs.insert(len(run_a), run_a) self._runs = new_runs sorted_run = next_run(iter(self._runs)) # Convert the run to an array if currently a linked list. if isinstance(sorted_run, List): sorted_run = sorted_run.to_array() # Return the final sorted run. return sorted_run
def list_vs_tree_vs_hash(): print('-' * 120) print( 'Pitting lists, trees and hash tables against each other for search.') print( 'Hashing is expected to be the fastest, followed by trees, then by lists.' ) print('All player statistics use hash tables for indexing.') print('Lists = Sequential search, average O(n)') print('Trees = Binary search, average O(log n)') print('Hash Tables = Hashing, average O(1)') print('-' * 120) linked_list = List() tree = Tree() hash_table = HashTable() for i in range(0, 5000): value = random.randint(0, 5000) linked_list.append(value) tree.insert(value, True) hash_table.insert(value, True) list_search(linked_list) tree_search(tree) hash_search(hash_table) prompt_next()
def tree_multiple(array): tree = Tree() for element in array: tree.insert(element, element)
def __init__(self, comparator=lambda a, b: a - b): self._compare = comparator self._runs = Tree() self._run = List() self._previous = None self.consume = self.__run_init
def load_circuit_player_scoreboard(players): scoreboard = Tree(lambda a, b: b - a) for name, player in players: scoreboard.insert(player.stats.points, player.stats) return scoreboard