TopCoder problem "Inheritance" used in SRM 14 (Division I Level Two , Division II Level Two)



Problem Statement

    
Class name: Inheritance
Method name: resolve
Parameters: String[], String, String
Returns: String

As Java programmers, you are familiar with the mechanics of inheritance.  Each
class (except for Object) has an ancestor (in Java this ancestor is specified
after the "extends" keyword; when no ancestor is specified, the class extends
Object).  Each class inherits certain methods of its parent class, as well as
those of its parent, and so on, all the way back to Object.  However, a class
can also define its own version of each of these methods, overriding the one
inherited from its ancestors.

When a method is called on a particular object, there must be a mechanism at
work to determine which class's method is used.  For instance, if the toString
method is called on an object of a class that does not implement its own
toString method, then that class's parent is checked to see if it implements
that particular method.  If it doesn't, then its parent is checked in the same
manner, all the way up to Object, which has no parent.  If even Object does not
implement the method, then that method is not defined for the object it was
called on.

Implement a class Inheritance, which contains a method resolve.  The method
takes a description of all the available classes as a String[], a class name as
a String, and a method name called on the class as a String, and returns a
String that is the name of the class that defines the method that should be
executed.

The method signature is:
public String resolve(String[] classes, String className, String methodName);

The String[] contains the descriptions of each of the available classes.  Each
element of this String[], describe a single class.  The description of a class
is in the form of "CLASSNAME|PARENTNAME|METHOD1,METHOD2...".  If the class does
not have a parent (like Object in Java), then PARENTNAME will be of zero
length.  The description of the methods is a comma-separated list of method
names.  Each method is separated only by a comma (no whitespace).

Given className and methodName, the method returns the name of the class that
defines the method which should be called according to the rules outlined
above.  If the method specified by methodName does not exist in the given class
nor any of its ancestors, your method should return the empty string ("").

Constraints:
- The name of the parent class in each class's description must be the name of
some other class
- There cannot be cycles in the inheritance graph (in other words, no class can
be allowed to inherit from any class of which it is an ancestor)
- The names of all classes and methods may only contain letters, digits, and
the underscore character ('_'), and must begin with a letter or underscore
- The name of any class or method may be no less than one character and no more
than 50 characters in length
- There may be at most 50 classes
- Each class may define at most 10 methods
- All method names in the method list for each class must be unique
- The class name passed as the second parameter of your method must be the name
of some class defined in the classes String[]

The tester will ensure that all of the above constraints are met before any
input is passed to your method.

Notes:
- Because of the constraints listed above, there must be at least one class
defined with no ancestor (there may be more than one)
- All string comparisons are case sensitive

Examples:

Given the class description:
["A||a,b,c",
 "B|A|b,c,d",
 "C||e,f",
 "D|C|"
 "E|D|f,g"
 "F|B|b,h,i"]

-If className="A" and methodName="b" the method returns "A".
-If className="F" and methodName="c" the method returns "B".
-If className="E" and methodName="a" the method returns "".
-If className="D" and methodName="e" the method returns "C".
-If className="F" and methodName="a" the method returns "A".

Given the class description:

["Object||toString,getClass"
 "String|Object|toString"
 "InputStream|Object|read,close,reset"
 "FileInputStream|InputStream|read,close"]

-If className is "String" and methodName is "toString", the method returns
"String".
-If className is "String" and methodName is "getClass", the method returns
"Object".
 

Definition

    
Class:Inheritance
Method:resolve
Parameters:String[], String, String
Returns:String
Method signature:String resolve(String[] param0, String param1, String param2)
(be sure your method is public)
    

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=104

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=3013&pm=104

Writer:

Logan

Testers:

Problem categories: