Problem Statement
You are given a project description. Calculate the hashcode for this project.
Given class ObjectT:
Represents any instance with a name.
class ObjectT {
/**
* Returns the name of this ObjectT.
*
* @return A String containing between 1 and 19 characters, inclusive.
* Each character in the string is one of 'a'-'z', 'A'-'Z', '0'-'9', '_'.
*/
public String getName();
}
Given class ClassT that extends ObjectT:
Represents a single class in a project. Each class has a name that can be obtained with a call to the inherited ObjectT.getName() method.
Given class PackageT:
Represents a part of a project called "package". Each package has a name that can be obtained with a call to the inherited ObjectT.getName() method. Packages might contain other packages and classes.
class PackageT extends ObjectT {
/**
* Gets the list of all packages directly contained in this PackageT.
* The order of packages in the list is not guaranteed.
*
* @return A list with a maximum of 100 packages.
*/
public List<PackageT> getPackages();
/**
* Returns the list of all classes directly contained within this PackageT.
* The order of the classes in the list is not guaranteed.
*
* @return A list with a maximum of 100 classes.
*/
public List<ClassT> getClasses();
}
Given class Project:
Represents a project. Projects are similar to packages except that they do not directly contain any classes. A single project can contain (directly or indirectly) a maximum of 100 packages, and (indirectly) a maximum of 100 classes.
There cannot be name conflicts within a project. If you consider an ObjectT[] with all classes and packages directly or indirectly contained within the Project, and the Project itself, then all items will have pairwise distinct names.
class Project extends ObjectT {
/**
* Gets the list of all packages directly contained within this Project.
* The order of packages in the list is not guaranteed.
*
* @return A list with a maximum of 100 packages.
*/
public List<PackageT> getPackages();
}
Given classes ProjectStorage and NoSuchProjectException:
Provides access to all the existing projects.
class ProjectStorage {
/**
* Gets the requested project instance.
*
* @param projectName The name of the project you want to access to.
* @return The project named projectName.
* @throws NoSuchProjectException if such project does not exist.
*/
public static Project getProject(String projectName) throws NoSuchProjectException;
}
Given class HashCodeGenerator:
Generates hashcodes for projects.
class HashCodeGenerator {
/**
* Generates the hash integer for a project description.
*
* @param description The description string for a project.
* @return The hashcode for a project with this description string.
*/
public static int generateHash(String description);
}
DEFINITION
You must define public class ProjectHashcode with public method int getHashcode(String projectName). projectName contains between 1 and 19 characters, inclusive. Each character of projectName is a letter ('a' - 'z', 'A' - 'Z'), a digit ('0' - '9') or an underscore ('_').
To implement this method, calculate the description string for the project named projectName. Return HashcodeGenerator.generateHash(String) for the description string. If the project named projectName does not exist, return -1.
CLASS DESCRIPTION STRING
The description string for a class is simply the first character of its name.
PACKAGE DESCRIPTION STRING
To calculate the description string for a package, separately order all its direct packages and all its direct classes according to the following rules:
- If package/class A and package/class B have names of different lengths, then the package/class with the shorter name should go first.
- If package/class A and package/class B have names of the same length, then A should go before B if and only if name(A).compareTo(name(B)) < 0, where name(A) and name(B) are the names of A and B, correspondingly.
Concatenate the description strings for all its direct packages (in the order described above), then add the description strings for all its direct classes (in the order described above) and finally add the first character of the package's name.
PROJECT DESCRIPTION STRING
To calculate the description string for a project, use the same rules and just treat it like a package without direct classes.
Definition
- Class:
- ProjectHashcode
- Method:
- getHashcode
- Parameters:
- String
- Returns:
- int
- Method signature:
- int getHashcode(String projectName)
- (be sure your method is public)
Constraints
- projectName will contain between 1 and 19 characters, inclusive.
- Each character of projectName will be a letter ('a' - 'z', 'A' - 'Z'), a digit ('0' - '9') or an underscore ('_').
Examples
"testcasea"
Returns: 116
This project does not contain any packages, so the desription string for it is "t"."_____m_o_n_i_tor"
Returns: 17247
This project contains only one package named "Cn". This package does not contain any classes or packages. The description string for the project is "C_"."keysae"
Returns: 207919
This project contains one package named "k0UrNyF31RD", and this package contains three classes named "TxT7UqTPDBP", "IO8YZXphbBjXe17" and "2ATaxsD8y5L9nYBWxW". The project tree looks as follows ('+' represents the project itself or a package; classes and packages within the same package or within the project itself are given in the order described in the statement): keysae + k0UrNyF31RD + TxT7UqTPDBP IO8YZXphbBjXe17 2ATaxsD8y5L9nYBWxW The description string for the project is "TI2kk"."competitio_n_"
Returns: 442593
This project contains two packages: "D_ws6" and "hwr2PlC". The first package contains nine classes and the second package does not contain packages or classes. The project tree looks as follows: competitio_n_ + D_ws6 + 176 rgY4Ak2dU W9aQn0Hrk8 Nig5IqADd1nD oUAHlxP1NnoE xuP1RP3Qmpo7 Bt1EJUUehRhJx PSqkloC3FXX0XLc tU1A_bgjYgzrtHV1 hwr2PlC + The description string for the project is "1rWNoxBPtDhc"."_k_e_y_s_"
Returns: -1
This project does not exist."a9u742"
Returns: 399969
The project tree looks as follows: a9u742 + 99I6fA49o + _LCV6vX0QHaT4Dt + Eft PyoqwrmC ix Rv6v mCMDwmvu pjGJUBaB kobixxrn6YcJ5laF GJNaYj5ulmSYCXXDSt 3AKyfHnZJwbasVwg + jc + 0rimAB_Ibzm8s4iZUr Za2ssCqce4THxWc6t14 + Qd8Ncff3 O1vPZEkB9O5EMIoPVV2 + The description string for the project is "EP_iRmpkG90jZQ3Oa". Please note that if a package directly contains both some packages and some classes, then packages and classes need to be added separately. In the description string for such package you must first list the description strings for all packages and then - for all classes.