TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

BBallBoxScore

Problem statement, definition, constraints, and public examples.

Problem Statement

In the typical basketball box score in the sports pages, the individual player stats will be listed in this format: "NAME FGM-FGA FTM-FTA PTS", where NAME is the player's name, FGM is the number of field goals made, FGA is the number of field goals attempted, FTM is the number of free throws made, FTA is the number of free throws attempted, and PTS is the number of points scored by the player.

For example, the line "Jim 4-7 2-4 14" would mean that Jim made 4 of 7 field goals, 2 of 4 free throws, and scored 14 points in all. Now, there is some ambiguity here - while free throws are worth one point each, field goals can either be worth two or three points.

Your method will be given a String line, a properly formatted box score line, and your method should return the number of three-point field goals scored by that player. If a player apparently scored an impossible number of points, your method should return -1 (see example 1.)

For example, if you were given line = "Bill 3-5 1-1 8", then your method should return 1, because Bill made two 2-point field goals, one 3-point field goal, and one free throw, for a total of 2x2 + 1x3 + 1x1 = 8 points.

Definition

Class:
BBallBoxScore
Method:
threesMade
Parameters:
String
Returns:
int
Method signature:
int threesMade(String line)
(be sure your method is public)

Constraints

  • line will contain between 11 and 40 characters, inclusive.
  • line will be properly formatted as "NAME FGM-FGA FTM-FTA PTS", each field separated by exactly one space (' ').
  • NAME will be between 1 and 20 characters in length, inclusive, and will contain only lower and uppercase letters ('a'-'z' and 'A'-'Z').
  • FGM will be between 0 and FGA, inclusive.
  • FTM will be between 0 and FTA, inclusive.
  • FGM, FGA, FTM, FTA, and PTS will all be between 0 and 999, inclusive, and will contain no leading zeroes.

Examples

  1. "Bill 3-5 1-1 8"
    Returns: 1
    See problem statement for explanation.
  2. "TheCheat 9-10 5-5 85"
    Returns: -1
    There's no way to score 85 points with that combination.
  3. "IDidntPlay 0-0 0-0 0"
    Returns: 0
    Or, at least, he didn't attempt any sort of shot at all.
Back to all problems