Statistics

Problem Statement for "Colors"

Problem Statement

PROBLEM STATEMENT

A crayon company has devised a way of encoding the color of the crayons used to
color a picture. Each of the eight colors of crayons is assigned a numeric
value. The sum of the individual color values represents the colors used. Code
a method that takes a sum of color values and produces the display text for the
colors encoded in the sum.

The eight colors are: red, orange, yellow, green, blue, purple, brown, and black.

The numeric values are: red=128, orange=64, yellow=32, green=16, blue=8,
purple=4, brown=2, black=1.

The sum is produced by adding the numeric values of the colors selected. A
color is either selected or not selected, and so the color's numeric value is
never included more than once in the sum. Valid sums range from 0 to 255
inclusive.

The display text produced has the following format.
F1. If no colors are selected, return an empty string.
F2. If exactly one color is selected, return the text for that color.
F3. If exactly two colors are selected, return the text for each color
separated by the text " and " (the word "and" with a single space before and
after). Do not include the double quote characters.
F4. If more than two colors are selected, return the text for each color
separated by ", " (a comma and a space) with one exception. Separate the last
two colors with ", and " (a comma, a space, the word "and", and a space). Do
not include the double quote characters.
F5. The colors appear in the order of their numeric value with the highest
values appearing first.
F6. All text in the output is lowercase.

DEFINITION
Class Name: Colors
Method Name: colorText
Parameters: int
Returns: String
Method signature (be sure your method is public): String colorText(int
selections);

TopCoder will ensure that the value of selections is from 0 to 255 inclusive.

EXAMPLES
Note: The double quote characters do not appear in the result.

colorText(0)   ==> ""     
colorText(4)   ==> "purple"     
colorText(24)  ==> "green and blue"
colorText(52)  ==> "yellow, green, and purple"
colorText(181) ==> "red, yellow, green, purple, and black"

Definition

Class:
Colors
Method:
colorText
Parameters:
int
Returns:
String
Method signature:
String colorText(int param0)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: