id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com > baseball
prev  |  next  |  chance

Play ball! In this exercise you will program the rules of baseball. The function baseball(away_hits, home_hits, N) plays an N-inning game, in which each successive batter's hits and outs are specified by the two "hits" lists: one for the away team and one for the home team. In each list, a 0 indicates an out, 1 a single, 2 a double, 3 a triple, and 4 a homerun. An inning consists of two half-innings of three outs each: the away team bats first, then the home team. A run is scored when a runner advances to home plate (also known as fourth base). On each hit, all runners advance the same number of bases as the hitter. If the teams are tied after N innings, they play extra innings until they are no longer tied. In the final inning, as soon as the home team is ahead, the game ends right there; don't wait for three outs. Define baseball(away_hits, home_hits, N) to play an N-inning game (including extra innings if necessary) and return a two-element list of runs scored: [away_score, home_score].


baseball([1, 2, 4, 0, 0, 0], [2, 2, 0, 0, 0], 1) → [3, 1]
baseball([0, 0, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 2, 1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 2, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 2, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0], 9) → [2, 6]
baseball([1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 2, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 2, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 4, 1, 0, 0, 1, 0, 4, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], 9) → [9, 2]

...Save, Compile, Run (ctrl-enter)

def baseball(away_hits, home_hits, N=9):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 780 Post-solution available

Copyright Nick Parlante 2017 - privacy