Last active
February 7, 2022 02:29
-
-
Save luiznasciment0/64d888a18c6cd61490083181ae130f61 to your computer and use it in GitHub Desktop.
My first Ruby challenge :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# MP: Matches Played | |
# W: Matches Won | |
# D: Matches Drawn (Tied) | |
# L: Matches Lost | |
# P: Points | |
# | |
# output expected: | |
# Team | MP | W | D | L | P | |
# Devastating Donkeys | 3 | 2 | 1 | 0 | 7 | |
# Allegoric Alaskans | 3 | 2 | 0 | 1 | 6 | |
# Blithering Badgers | 3 | 1 | 0 | 2 | 3 | |
# Courageous Californians | 3 | 0 | 1 | 2 | 1 | |
# | |
class Tournament | |
def self.downcase?(text = '') | |
text[0] == text[0].downcase | |
end | |
def self.setup_scout(team_names) | |
scout = {} | |
team_names.each do |team_name| | |
scout[team_name] = { | |
'MP' => 0, | |
'W' => 0, | |
'D' => 0, | |
'L' => 0, | |
'P' => 0 | |
} | |
end | |
scout | |
end | |
def self.get_team_names(data) | |
data.reject { |element| downcase?(element) }.uniq | |
end | |
def self.tally(tournament_data = '') | |
split_input_by_semi_colon = tournament_data.split(';') | |
split_input_by_line = tournament_data.split("\n") | |
team_names = get_team_names(split_input_by_semi_colon) | |
scout = setup_scout(team_names) | |
split_input_by_line.each do |line| | |
team1, team2, result = line.split(';') | |
scout[team1]['MP'] += 1 | |
scout[team2]['MP'] += 1 | |
if result == 'win' | |
scout[team1]['W'] += 1 | |
scout[team2]['L'] += 1 | |
scout[team1]['P'] += 3 | |
end | |
if result == 'loss' | |
scout[team2]['W'] += 1 | |
scout[team1]['L'] += 1 | |
scout[team2]['P'] += 3 | |
end | |
if result == 'draw' | |
scout[team2]['D'] += 1 | |
scout[team1]['D'] += 1 | |
scout[team2]['P'] += 1 | |
scout[team1]['P'] += 1 | |
end | |
end | |
text = '' | |
scout.each do |key, value| | |
text += "#{key} | #{value['MP']} | #{value['W']} | #{value['D']} | #{value['L']} | #{value['P']}\n" | |
end | |
result = "Team | MP | W | D | L | P\n" + text | |
result | |
end | |
end | |
puts Tournament.tally("Allegoric Alaskans;Blithering Badgers;win") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment