About 10 months ago I started my coding boot camp. It was an immersive software engineering school that lasted 15 weeks. Each future student is given what I'd call homework we would need to pass to be accepted. This is a good idea business-wise because a school doesn't want to deal with dropouts and refunds because it's too hard for them.
I remember The Hashketball Lab being the last set of problems and I was stressed about it. I think it took me all day to figure it out. I thought it was so hard I actually blogged about it because I didn't want to forget how I did it, haha! I thought it would be a good idea to revisit this challenge but, since I dislike Ruby, use Python instead. Let's do this!
Right off the bat, I see errors with the team data that I had saved. There had to have been errors when I tried to save this file. How did I not notice? Also, the data I was given is messy, messy, messy! I'm not going to change it but leave it as is because, in the real world, I'll probably be dealing with things like this. Here is the correct data:
teams = {
'away': {
'team_name': 'Charlotte Hornets',
'colors': [
"Turquoise",
"Purple"
],
'players': [
{
'player_name': 'Jeff Adrien',
'number': 4,
'shoe': 18,
'points': 10,
'rebounds': 1,
'assists': 1,
'steals': 2,
'blocks': 7,
'slam_dunks': 2
},
{
'player_name': 'Bismack Biyombo',
'number': 0,
'shoe': 16,
'points': 12,
'rebounds': 4,
'assists': 7,
'steals': 22,
'blocks': 15,
'slam_dunks': 10
},
{
'player_name': 'DeSagna Diop',
'number': 2,
'shoe': 14,
'points': 24,
'rebounds': 12,
'assists': 12,
'steals': 4,
'blocks': 5,
'slam_dunks': 5
},
{
'player_name': 'Ben Gordon',
'number': 8,
'shoe': 15,
'points': 33,
'rebounds': 3,
'assists': 2,
'steals': 1,
'blocks': 1,
'slam_dunks': 0
},
{
'player_name': 'Kemba Walker',
'number': 33,
'shoe': 15,
'points': 6,
'rebounds': 12,
'assists': 12,
'steals': 7,
'blocks': 5,
'slam_dunks': 12
}
]
},
'home': {
'team_name': 'Brooklyn Nets',
'colors': [
'Black',
'White'
],
'players': [
{
'player_name': 'Alan Anderson',
'number': 0,
'shoe': 16,
'points': 22,
'rebounds': 12,
'assists': 12,
'steals': 3,
'blocks': 1,
'slam_dunks': 1
},
{
'player_name': 'Reggie Evans',
'number': 30,
'shoe': 14,
'points': 12,
'rebounds': 12,
'assists': 12,
'steals': 12,
'blocks': 12,
'slam_dunks': 7
},
{
'player_name': 'Brook Lopez',
'number': 11,
'shoe': 17,
'points': 17,
'rebounds': 19,
'assists': 10,
'steals': 3,
'blocks': 1,
'slam_dunks': 15
},
{
'player_name': 'Mason Plumlee',
'number': 1,
'shoe': 19,
'points': 26,
'rebounds': 11,
'assists': 6,
'steals': 3,
'blocks': 8,
'slam_dunks': 5
},
{
'player_name': 'Jason Terry',
'number': 31,
'shoe': 15,
'points': 19,
'rebounds': 2,
'assists': 2,
'steals': 4,
'blocks': 11,
'slam_dunks': 1
}
]
}
}
- Build a method,
num_points_scored
that takes in an argument of a player's name and returns the number of points scored for that player
def num_points_scored(player_name):
for player in teams['home']['players'] + teams['away']['players']:
if player['player_name'] == player_name:
return player['points']
return "Player not found"
- Build a method,
shoe_size
, that takes in an argument of a player's name and returns the shoe size for that player.
def shoe_size(player_name):
for player in teams['home']['players'] + teams['away']['players']:
if player['player_name'] == player_name:
return player['shoe']
return "Player not found"
- Build a method,
team_colors
, that takes in an argument of the team name and returns an array of that team's colors.
def team_colors(team_name):
if team_name == 'Charlotte Hornets':
return teams["away"]['colors']
elif team_name == 'Brooklyn Nets':
return teams["home"]['colors']
else:
return 'Team not found'
- Build a method,
team_names
, that operates on the game hash to return an array of the team names.
def team_names():
arr = []
arr.append(teams['away']['team_name'])
arr.append(teams['home']['team_name'])
return arr
- Build a method,
player_numbers
, that takes in an argument of a team name and returns an array of the jersey numbers for that team.
def player_numbers(team_name):
arr = []
if team_name == 'Charlotte Hornets':
players = teams['away']['players']
for player in players:
arr.append(player['number'])
return arr
elif team_name == 'Brooklyn Nets':
players = teams['home']['players']
for player in players:
arr.append(player['number'])
return arr
else:
return 'Team not found'
- Build a method,
player_stats
, that takes in an argument of a player's name and returns a hash of that player's stats.
def player_stats(player_name):
players = teams['home']['players'] + teams['away']['players']
for player in players:
if player['player_name'] == player_name:
return player
return "Player not found"
Top comments (0)