From f8f9df55ed4b9d997ef592ed03f8c29c3ebbf31a Mon Sep 17 00:00:00 2001 From: Felix Lu Date: Fri, 27 Jul 2012 17:05:18 -0700 Subject: [PATCH] Matchmaker and Prequisites --- MatchMaker/MatchMaker.txt | 100 ++++++++++++++++++++++++++++ MatchMaker/src/MatchMaker.java | 113 +++++++++++++++++++++++++++++++ MatchMaker/src/TestMain.java | 27 ++++++++ Prerequisites/Prerequisites | 118 +++++++++++++++++++++++++++++++++ 4 files changed, 358 insertions(+) create mode 100644 MatchMaker/MatchMaker.txt create mode 100644 MatchMaker/src/MatchMaker.java create mode 100644 MatchMaker/src/TestMain.java create mode 100644 Prerequisites/Prerequisites diff --git a/MatchMaker/MatchMaker.txt b/MatchMaker/MatchMaker.txt new file mode 100644 index 0000000..af53e40 --- /dev/null +++ b/MatchMaker/MatchMaker.txt @@ -0,0 +1,100 @@ + +Problem Statement +     +THIS PROBLEM WAS TAKEN FROM THE SEMIFINALS OF THE TOPCODER INVITATIONAL +TOURNAMENT + +DEFINITION +Class Name: MatchMaker +Method Name: getBestMatches +Paramaters: String[], String, int +Returns: String[] +Method signature (be sure your method is public): String[] +getBestMatches(String[] members, String currentUser, int sf); + +PROBLEM STATEMENT +A new online match making company needs some software to help find the "perfect +couples". People who sign up answer a series of multiple-choice questions. +Then, when a member makes a "Get Best Mates" request, the software returns a +list of users whose gender matches the requested gender and whose answers to +the questions were equal to or greater than a similarity factor when compared +to the user's answers. + +Implement a class MatchMaker, which contains a method getBestMatches. The +method takes as parameters a String[] members, String currentUser, and an int +sf: +- members contains information about all the members. Elements of members are +of the form "NAME G D X X X X X X X X X X" + * NAME represents the member's name + * G represents the gender of the current user. + * D represents the requested gender of the potential mate. +* Each X indicates the member's answer to one of the multiple-choice +questions. The first X is the answer to the first question, the second is the +answer to the second question, et cetera. +- currentUser is the name of the user who made the "Get Best Mates" request. +- sf is an integer representing the similarity factor. + +The method returns a String[] consisting of members' names who have at least sf +identical answers to currentUser and are of the requested gender. The names +should be returned in order from most identical answers to least. If two +members have the same number of identical answers as the currentUser, the names +should be returned in the same relative order they were inputted. + +TopCoder will ensure the validity of the inputs. Inputs are valid if all of +the following criteria are met: +- members will have between 1 and 50 elements, inclusive. +- Each element of members will have a length between 7 and 44, inclusive. +- NAME will have a length between 1 and 20, inclusive, and only contain +uppercase letters A-Z. +- G can be either an uppercase M or an uppercase F. +- D can be either an uppercase M or an uppercase F. +- Each X is a capital letter (A-D). +- The number of Xs in each element of the members is equal. The number of Xs +will be between 1 and 10, inclusive. +- No two elements will have the same NAME. +- Names are case sensitive. +- currentUser consists of between 1 and 20, inclusive, uppercase letters, A-Z, +and must be a member. +- sf is an int between 1 and 10, inclusive. +- sf must be less than or equal to the number of answers (Xs) of the members. + +NOTES +The currentUser should not be included in the returned list of potential mates. + + +EXAMPLES + +For the following examples, assume members = +{"BETTY F M A A C C", + "TOM M F A D C A", + "SUE F M D D D D", + "ELLEN F M A A C A", + "JOE M F A A C A", + "ED M F A D D A", + "SALLY F M C D A B", + "MARGE F M A A C C"} + +If currentUser="BETTY" and sf=2, BETTY and TOM have two identical answers and +BETTY and JOE have three identical answers, so the method should return +{"JOE","TOM"}. + +If currentUser="JOE" and sf=1, the method should return +{"ELLEN","BETTY","MARGE"}. + +If currentUser="MARGE" and sf=4, the method should return []. +Definition +     +Class: +MatchMaker +Method: +getBestMatches +Parameters: +String[], String, int +Returns: +String[] +Method signature: +String[] getBestMatches(String[] param0, String param1, int param2) +(be sure your method is public) +     + +This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. \ No newline at end of file diff --git a/MatchMaker/src/MatchMaker.java b/MatchMaker/src/MatchMaker.java new file mode 100644 index 0000000..53cb3d3 --- /dev/null +++ b/MatchMaker/src/MatchMaker.java @@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +public class MatchMaker { + private class Member { + public String _name; + public char _gender; + public char _desiredGender; + public List _answers = new ArrayList(); + + public int _compareResults = 0; + + public Member(String name, char gender, char desiredGender, + List answers) { + _name = name; + _gender = gender; + _desiredGender = desiredGender; + _answers.addAll(answers); + } + } + + private List _members = new ArrayList(); + + public MatchMaker() { + } + + String[] getBestMatches(String[] members, String currentUser, int sf) { + _members.clear(); + + parseMembers(members); + String[] bestMatches = compareAnswers(currentUser, sf); + + return bestMatches; + } + + private void parseMembers(String[] members) { + String name; + char gender; + char desiredGender; + + for (String memberInfo : members) { + StringTokenizer st = new StringTokenizer(memberInfo); + name = st.nextToken(); + gender = st.nextToken().toCharArray()[0]; + desiredGender = st.nextToken().toCharArray()[0]; + + List answers = new ArrayList(); + while (st.hasMoreTokens()) { + answers.add(st.nextToken().toCharArray()[0]); + } + + _members.add(new Member(name, gender, desiredGender, answers)); + } + } + + private String[] compareAnswers(String currentUser, int sf) { + List matches = new ArrayList(); + Member curMember = findMember(currentUser); + + for (Member member : _members) { + if (curMember != member) { + int compare = getCompare(curMember, member, sf); + + if (compare >= sf) { + member._compareResults = compare; + addToMatches(matches, member); + } + } + } + + String[] goodMatches = new String[matches.size()]; + int i = 0; + for (Member m : matches) { + goodMatches[i++] = m._name; + } + + return goodMatches; + } + + private void addToMatches(List matches, Member member) { + for (int i=0; i < matches.size(); i++) { + if (member._compareResults > matches.get(i)._compareResults) { + matches.add(i, member); + return; + } + } + + matches.add(member); + } + + private int getCompare(Member curMember, Member compareToMember, int sf) { + int sameAnswer = 0; + if (curMember._desiredGender == compareToMember._gender) { + for (int i = 0; i < curMember._answers.size(); i++) { + if (curMember._answers.get(i).equals(compareToMember._answers + .get(i))) { + sameAnswer++; + } + } + } + return sameAnswer; + } + + private Member findMember(String member) { + for (Member m : _members) { + if (m._name.equals(member)) { + return m; + } + } + return null; + } +} diff --git a/MatchMaker/src/TestMain.java b/MatchMaker/src/TestMain.java new file mode 100644 index 0000000..9287758 --- /dev/null +++ b/MatchMaker/src/TestMain.java @@ -0,0 +1,27 @@ + +public class TestMain { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + String[] members = {"BETTY F M A A C C", + "TOM M F A D C A", + "SUE F M D D D D", + "ELLEN F M A A C A", + "JOE M F A A C A", + "ED M F A D D A", + "SALLY F M C D A B", + "MARGE F M A A C C"}; + + MatchMaker mm = new MatchMaker(); + String[] best = mm.getBestMatches(members, "MARGE", 4); + + for (String b: best) { + System.out.println(b); + } + + } + +} diff --git a/Prerequisites/Prerequisites b/Prerequisites/Prerequisites new file mode 100644 index 0000000..7cf674d --- /dev/null +++ b/Prerequisites/Prerequisites @@ -0,0 +1,118 @@ + +Problem Statement +     +***Note: Please keep programs under 7000 characters in length. Thank you + + +Class Name: Prerequisites +Mathod Name: orderClasses +Parameters: String[] +Returns: String[] + +You are a student at a college with the most unbelievably complex prerequisite +structure ever. To help you schedule your classes, you decided to put together +a program that returns the order in which the classes should be taken. + +Implement a class Prerequisites which contains a method orderClasses. The +method takes a String[] that contains the classes you must take and returns a +String[] of classes in the order the classes should be taken so that all +prerequisites are met. + +String[] elements will be of the form (and TopCoder will ensure this): +"CLASS: PRE1 PRE2 ..." where PRE1 and PRE2 are prerequisites of CLASS. CLASS, +PRE1, PRE2, ... consist of a department name (3 or 4 capital letters, A-Z +inclusive) followed by a class number (an integer between 100 and 999, +inclusive). The department name should be immediately followed by the class +number with no additional characters, numbers or spaces (i.e. MATH217). It is +not necessary for a class to have prerequisites. In such a case, the colon is +the last character in the String. + +You can only take one class at a time, therefore, use the following rules to +determine which class to take : +1) Any prerequisite class(es) listed for a class must be taken before the class +can be taken. +2) If multiple classes can be taken at the same time, you take the one with the +lowest number first, regardless of department. +3) If multiple classes with the same number can be taken at the same time, you +take the department name which comes first in alphabetical order. +4) If the inputted course schedule has errors, return a String[] of length 0. +There is an error if it is impossible to return the classes in an order such +that all prerequisites are met, or if a prerequisite is a course that does not +have its own entry in the inputted String[]. + +Examples of valid input Strings are: +"CSE111: CSE110 MATH101" +"CSE110:" + +Examples of invalid input Strings are: +"CS117:" (department name must consist of 3 - 4 capital letters, inclusive) +"cs117:" (department name must consist of 3 - 4 capital letters, inclusive) +"CS9E11:" (department name must be letters only) +"CSE110: " (no trailing spaces allowed) +"CSE110: CSE101 " (no trailing spaces allowed) +"MATH211: MAM2222" (class number to large) +"MATH211: MAM22" (class number to small) +"ENGIN517: MATH211" (department name to large) + +Here is the method signature (be sure your method is public): +String[] orderClasses(String[] classSchedule); + +TopCoder will make sure classSchedule contains between 1 and 20 Strings, +inclusive, all of the form above. The Strings will have between 1 and 50 +characters, inclusive. TopCoder will check that the syntax of the Strings are +correct: The Strings will contain a valid class name, followed by a colon, +possibly followed by a series of unique prerequisite classes separated by +single spaces. Also, TopCoder will ensure that each class has at most one +entry in the String[]. + +Examples: +If classSchedule={ +"CSE121: CSE110", +"CSE110:", +"MATH122:", +} +The method should return: {"CSE110","CSE121","MATH122"} + +If classSchedule={ +"ENGL111: ENGL110", +"ENGL110: ENGL111" +} +The method should return: {} + +If classSchedule=[ +"ENGL111: ENGL110" +} +The method should return: {} + +If classSchedule={ +"CSE258: CSE244 CSE243 INTR100" +"CSE221: CSE254 INTR100" +"CSE254: CSE111 MATH210 INTR100" +"CSE244: CSE243 MATH210 INTR100" +"MATH210: INTR100" +"CSE101: INTR100" +"CSE111: INTR100" +"ECE201: CSE111 INTR100" +"ECE111: INTR100" +"CSE243: CSE254" +"INTR100:" +} +The method should return: +{"INTR100","CSE101","CSE111","ECE111","ECE201","MATH210","CSE254","CSE221","CSE2 +43","CSE244","CSE258"} +Definition +     +Class: +Prerequisites +Method: +orderClasses +Parameters: +String[] +Returns: +String[] +Method signature: +String[] orderClasses(String[] param0) +(be sure your method is public) +     + +This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. \ No newline at end of file