PROBLEM STATEMENT
Create a class Funnel that contains the method charAt, which takes a String,
shapes it into a funnel, and returns the character at a given point (x, y),
where the upper-left-most character is at point (0, 0).
Shape the string into a funnel with the following guidelines:
1. The funnel must be a perfect funnel (an upside-down triangle). If the
string has too few characters to make a perfect funnel, pad the beginning of
the input string with the necessary amount of spaces before you begin. If the
string contains just enough characters to make a perfect funnel, then the first
layer will contain no added spaces.
2. Each layer must contain two more characters drawn from the input string than
the layer beneath it. The bottom layer must be composed of only the last
character in the input string, padded with the necessary spaces on either side.
3. Each layer must have an equal amount of spaces (excluding those drawn from
the input string) to the left and to the right of the characters.
DEFINITION
Class: Funnel
Method: charAt
Parameters: String, int, int
Returns: char
Method signature (be sure your method is public): char charAt(String str, int
x, int y);
NOTES
- The upper-left-most character in the funnel lies at the point (0, 0).
- If there is no character at the specified coordinate, return a space (' ').
- If the specified coordinate does not exist in the funnel, return a space (' ').
- The x-coordinate of a character increases as it moves to the right.
- The y-coordinate of a character increases as it moves down.
- The spaces in str are not to be ignored. Treat them as characters, too.
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- str contains between 1 and 50 characters, inclusive.
- Each character in str must contain only lower-case letters ('a'-'z'),
upper-case letters('A'-'Z'), dashes ('-'), commas (','), periods ('.'),
exclamation points ('!'), question marks ('?'), and spaces.
- x and y will be between 0 and 49, inclusive.
EXAMPLES
Example 1:
str = "Hi, I m a funnel!"
x = 6
y = 1
Shaped into a funnel, the above string looks like this:
Columns:012345678
Layer 0: H
Layer 1: i, I m
Layer 2: a fun
Layer 3: nel
Layer 4: !
Now, the method returns the character that resides in column 6, layer 1, which
is 'm'.
Example 2:
str = "I am also an upside-down triangle."
x = 5
y = 6
Shaped into a funnel, the above string looks like this:
Columns:012345678901
Layer 0: I am also
Layer 1: an upsid
Layer 2: e-down
Layer 3: trian
Layer 4: gle
Layer 5: .
Now, the method looks for a character in column 5, layer 6, but finds none, and
so returns ' '.
Example 3:
str = "The quick brown fox jumps over the lazy dog."
x = 6
y = 6
Method returns '.'.
Example 4:
str = "Man who jumps off cliff jumps to conclusion!"
x = 7
y = 2
Method returns 'j'.
Example 5:
str = "Hope fills Dreams. Disappointment haunts Reality."
x = 4
y = 0
Method returns ' '.