TopCoder problem "ComputerExpert" used in SRM 236 (Division II Level Three)



Problem Statement

    

A human expert uses his prior knowledge to analyze a set of observations that are presented to him. These days, companies are starting to explore the benefits of computerized expert systems. Expert systems attempt to model the way human experts think and arrive at conclusions. Knowledge obtained in this way is more reliable, cheaper and reproducible.

facts contains all the observations presented to the expert. rules is a set of rules that the expert can use to arrive at new conclusions. rules will be formatted as (<FACT>/.../<FACT>),...,(<FACT>/.../<FACT>) -> <FACT>. In the input, '/' represents the OR operator and ',' represents the AND operator. Hence, the above input represents a boolean expression like (<FACT> OR ... OR <FACT>) AND ... AND (<FACT> OR ... OR <FACT>) -> <FACT>. Each <FACT> represents a sequence of uppercase characters and the character '_'. Note: brackets and parentheses are only used for clarity and are not part of the input.

A rule composed solely of AND will fire only if all the facts listed in the rule are known. Similarly, a rule composed solely of OR will fire if any of the facts listed in the rule are known. Once a rule has fired, its outcome fact can be added to the knowledge of the expert (if not already known).

Given a String[] of facts and a String[] of rules return a String[] of all outcome facts, sorted in ascending alphabetical order. If there are no outcome facts then return an empty String[].

 

Definition

    
Class:ComputerExpert
Method:operate
Parameters:String[], String[]
Returns:String[]
Method signature:String[] operate(String[] facts, String[] rules)
(be sure your method is public)
    
 

Constraints

-facts will contain between 1 and 50 elements inclusive.
-Each element of facts will contain between 1 and 50 characters inclusive.
-Elements of facts will only contain characters 'A'-'Z' and the character '_'.
-rules will contain between 1 and 50 elements inclusive.
-Each element of rules will contain between 6 and 50 characters inclusive.
-Each element in rules will be formatted as (<FACT>/.../<FACT>),...,(<FACT>/.../<FACT>) -> <FACT>. In the input, '/' represents the OR operator and ',' represents the AND operator. Each <FACT> must be a sequence of uppercase characters 'A'-'Z' and the character '_'. Note: brackets and parentheses are only used for clarity and are not part of the input.
 

Examples

0)
    
{"SKY_IS_CLOUDY","HAVE_MONEY"}
{"HAVE_MONEY,BAD_WEATHER -> WATCH_MOVIE","SKY_IS_CLOUDY -> BAD_WEATHER",
"GOOD_WEATHER -> PLAY_SOCCER"}
Returns: {"BAD_WEATHER", "WATCH_MOVIE" }
In this simplistic example we want to decide whether to watch a movie or play soccer. We observe that SKY_IS_CLOUDY and that we HAVE_MONEY. The second rule tells us that if SKY_IS_CLOUDY then we have BAD_WEATHER. So we conclude that the weather is bad. Knowing this fact, we can now use the first rule that tells us that if we HAVE_MONEY and BAD_WEATHER then we WATCH_MOVIE. So we choose to watch a movie. The third rule never gets fired.
1)
    
{"NEEDLE_LEAF","KEEPS_LEAVES"}
{"KEEPS_LEAVES -> EVERGREEN","NEEDLE_LEAF -> GYMNOSPERM",
"EVERGREEN,GYMNOSPERM -> PINE_TREE"}
Returns: {"EVERGREEN", "GYMNOSPERM", "PINE_TREE" }
In this example we try to classify a plant. Since it has a NEEDLE_LEAF we know its a GYMNOSPERM. Since it KEEPS_LEAVES we know its an EVERGREEN. An EVERGREEN and a GYMNOSPERM must be a PINE_TREE.
2)
    
{"I"}
{"I -> I","P -> CONSONANT", "Z -> CONSONANT", "A/E/I/O/U -> VOWEL"}
Returns: {"VOWEL" }
I is a VOWEL. Note that there is no point firing the first rule, because we already know the outcome fact I.
3)
    
{"Y","W","L","T","H","J","G","T","F","P","T","P","P","N"}
{"C/V,J,F/T,N,W,L -> M","N/L,F,B/N/E,Y -> C",
"M,G,G,S/F/Q,G,S/G,H/Z,W,F,C,Q/F,M -> E"}
Returns: {"C", "E", "M" }
4)
    
{"G","H"}
{"A/B/C/D/E/F,G/Z -> Y", "H,H,H,H,H,H,H,H -> Z"}
Returns: {"Z" }
5)
    
{"A","B","_X", "X_"}
{"A/B,C -> D","A,B,C -> D","X -> Y","_X_ -> Y"}
Returns: { }
Here there are no rules fired. Note that "_X" and "X_" are not the same as "X" or "_X_".

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=6535&pm=4460

Writer:

dimkadimon

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Simple Math, String Parsing