Problem Statement
PROBLEM STATEMENT When designing vehicles and LEGO robots, it is important to construct gear boxes tailored to a desired power and speed. One of the most useful pieces of information about a gearbox is its gear-reduction ratio... that is, the number of times the motor turns for each time the wheel turns. Below is a String representation of a gearbox. The rows each represent individual axles. The columns are positions on the axles that might contain gears (if not, then just the metal rod from the axle). If there is a "-", that signifies no gear. If there is an integer, n, that means there is a gear which has a n:1 gear reduction with respect to the motor. Assume the motor is attached to the top axle, and the wheel is always attached to the bottom axle. There is no such thing as a 0:1 gear reduction. Gears on the same axle turn at the same rate, if one gear turns one full time, all gears on that axle turn one full time. ---5- -2-3- -5--3 --3-5 --5-- The first and second axle are connected by a 5/3 ratio. The second and third axle are connected by a 2/5 ratio. The third and fourth by a 3/5 ratio. The fourth and fifth by a 3/5 ratio. Then, the first axle to the fifth axle has a ratio of (5/3 * 2/5 * 3/5 * 3/5) = 6/25. Remember gear reduction is the number of times the MOTOR turns for each time the WHEEL turns, so the actual gear-reduction of the box is: "25:6" If there exist two adjacent axles where none of the gears "mesh", that is, there is no column where both axles have a gear, then the gearbox is broken since it won't allow the motor to drive the wheel. In that case, return "0" See the example below: ---2- -3-5- -2--2 -54-- ---3- On the last two axles, there is no "meshing" since the only gear on the last axle is a 3, and there is no gear in that position in the previous axle. The reduction is "0". It is possible for two axles to mesh at multiple places, but one must be careful that the proportions are the same in that case, or else the gearbox will jam up since different sets of gears are trying to turn at different rates. -2-3- -4-6- --31- --5-- This box should be fine, the first and second axle mesh at multiple spots, but their ratios are the same, 1/2. The reduction is "5:9" -2-3- -4-4- --31- --5-- This box above will jam, so the reduction is "0". DEFINITION Class: GearBox Parameters: String[] Returns: String Method Signature: String reduction (String[] axles) Be sure to declare your method public NOTES - Gear reductions are always in simplified form. For example, "9:3" is NOT a valid gear reduction, because it can be simplified to "3:1" TopCoder will check the validity of the inputs. Inputs are valid if all of the following criteria are met: - All axles must be the same length. - axles must contain between 2 and 10 elements, inclusive. - elements of axles must be between 2 and 10 characters in length, inclusive. - Axle representation must only consist of digits [1-9] or '-'. EXAMPLES ---5- -2-3- -5--3 --3-5 --5-- Returns: "25:6" ---2- -3-5- -2--2 -54-- ---3- Returns: "0" -2-3- -4-6- --31- --5-- Returns: "5:9" -2-3- -4-4- --31- --5-- Returns: "0"
Definition
- Class:
- GearBox
- Method:
- reduction
- Parameters:
- String[]
- Returns:
- String
- Method signature:
- String reduction(String[] param0)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.