Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLayne Faler
iOS Development Techdegree Student 5,184 PointsCan someone give me a hint at what I'm missing
enum Teams: String { case Sharks case Dragons case Raptors }
// Data assigned to a player struct Players { let name: String let height: Double var experience: Bool let guardianName: String var team: String? }
// Dictionary of players var playDict = [0: Players(name: "Joe Smith", height: 42, experience: true, guardianName: "Jim and Jan Smith", team: nil), 1: Players(name: "Jill Tanner", height: 36, experience: true, guardianName: "Clara Tanner", team: nil), 2: Players(name: "Bill Bon", height: 43, experience: true, guardianName: "Sara and Jenny Bon", team: nil), 3: Players(name: "Eva Gordon", height: 45, experience: false, guardianName: "Wendy and Mike Gordon", team: nil), 4: Players(name: "Matt Gill", height: 40, experience: false, guardianName: "Charles and Sylvia Gill", team: nil), 5: Players(name: "Kimmy Stein", height: 41, experience: false, guardianName: "Bill and Hillary Stein", team: nil), 6: Players(name: "Sammy Adams", height: 45, experience: false, guardianName: "Jeff Adams", team: nil), 7: Players(name: "Karl Saygan", height: 42, experience: true, guardianName: "Heather Bledsoe", team: nil), 8: Players(name: "Suzane Greenburg", height: 44, experience: true, guardianName: "Henrietta Dumas", team: nil), 9: Players(name: "Sal Dali", height: 41, experience: false, guardianName: "Gala Dali", team: nil), 10: Players(name: "Joe Kavalier", height: 39, experience: false, guardianName: "Sam and Elaine Kavalier", team: nil), 11: Players(name: "Ben Finklestein", height: 44, experience: false, guardianName: "Aaron and Jill Finklestein", team: nil), 12: Players(name: "Diego Soto", height: 41, experience: true, guardianName: "Robin and Sarika Soto", team: nil), 13: Players(name: "Chloe Alaska", height: 47, experience: false, guardianName: "David and Jamie Alaska", team: nil), 14: Players(name: "Arnold Willis", height: 43, experience: false, guardianName: "Claire Willis", team: nil), 15: Players(name: "Phillip Helm", height: 44, experience: true, guardianName: "Thomas Helm and Eva Jones", team: nil), 16: Players(name: "Les Clay", height: 42, experience: true, guardianName: "Wynonna Brown", team: nil), 17: Players(name: "Herschel Krustofski", height: 45, experience: true, guardianName: "Hyman and Rachel Krustofski", team: nil)]
// finding number of experienced players var numberOfExperiencedPlayers = 0 for number in (0 ..< playDict.count) { if (playDict[number]?.experience == true) { numberOfExperiencedPlayers += 1 } }
// variable declaration var numberOfTeams = 3 var totalPlayersPerTeam = playDict.count/numberOfTeams var experiencedPlayersPerTeam = numberOfExperiencedPlayers/numberOfTeams var nonExperiencedPlayersPerTeam = totalPlayersPerTeam - experiencedPlayersPerTeam
// Helper function - finds average of a team func averageHeightForTeam(currentTeam: String) -> Double { var average = 0.0 for number in (0 ..< playDict.count) { if (playDict[number]?.team == currentTeam) { average += (playDict[number]?.height)! } } average /= Double(totalPlayersPerTeam) return average }
// Helper function - sorts players based on experience and not already associated with a team // assigning player to a team func sortPlayers(newTeam: String, experiencedPlayers: Int, experienced: Bool) { var counter = 0 for number in (0 ..< playDict.count) { if (playDict[number]?.experience == experienced && playDict[number]?.team == nil && counter < experiencedPlayers) { playDict[number]?.team = newTeam counter += 1 } } }
// Helper function - reset team to nil if the func resetTeamToNil(currentTeam: String) { for number in (0 ..< playDict.count) { if (playDict[number]?.team == currentTeam) { playDict[number]?.team = nil } } }
// Assign teams func teamAssignment(currentTeam: String) { repeat { sortPlayers(currentTeam, experiencedPlayers: experiencedPlayersPerTeam, experienced: true) sortPlayers(currentTeam, experiencedPlayers: nonExperiencedPlayersPerTeam, experienced: false) averageHeightForTeam(currentTeam)
if (averageHeightForTeam(currentTeam) > 1.5) {
resetTeamToNil(currentTeam)
}
} while (averageHeightForTeam(currentTeam) > 1.5)
}
// assigning teams to players teamAssignment(Teams.Sharks.rawValue) teamAssignment(Teams.Dragons.rawValue) teamAssignment(Teams.Raptors.rawValue)
// Writing letters to players on each team for number in (0 ..< playDict.count) { if (playDict[number]?.team == Teams.Sharks.rawValue) { print("Dear (playDict[number]?.guardianName), \nYour child (playDict[number]?.name) is on team (playDict[number]?.team) and practice is on Tuesdays at 6pm.\nThank you for joinning.") } if (playDict[number]?.team == Teams.Dragons.rawValue) { print("Dear (playDict[number]?.guardianName), \nYour child (playDict[number]?.name) is on team (playDict[number]?.team) and practice is on Wednesdays at 6pm.\nThank you for joinning.") } if (playDict[number]?.team == Teams.Raptors.rawValue) { print("Dear (playDict[number]?.guardianName), \nYour child (playDict[number]?.name) is on team (playDict[number]?.team) and practice is on Thursdays at 6pm.\nThank you for joinning.") } }
2 Answers
Pavel Fomchenkov
20,897 PointsYou aren't supposed to use enums yet.
Layne Faler
iOS Development Techdegree Student 5,184 PointsNever mind I got it, I forgot to add the overall average of heights but if there's a more efficient way to complete this task please let me know