Sample Problem Statements
Here are a couple of examples of the types of problems you can expect from a TopCoder competition. Once you become a TopCoder member, you can view all of the problems that TopCoder has ever used in the statistics section of our web site. If you're looking to practice, you'll find that after each TopCoder competition the problem sets that were used will be added as "practice rooms" in the TopCoder competition Arena. The TopCoder practice rooms are a great place to test your skills without the pressure of live competition and at the same time, get a feel for the competition environment. During an event you are presented with mulitple problems (usually 3) of increasing difficulty.
You will be given a String[] nouns. Transform each element of nouns into its plural form using the rules above and return the resulting String[]. DEFINITION Class: NounReform Method: makePlural Parameters: String[] Returns: String[] Method signature: String[] makePlural(String[] nouns) (be sure your methodis public) CONSTRAINTS - nouns will contain between 1 and 50 elements, inclusive. - Each element of nouns will contain between 1 and 20 characters, inclusive. - Each element of nouns will consist of only lower-case letters ('a'-'z'). EXAMPLES 0) {"box", "church", "elephant", "stereo"} Returns: {"boxes", "churches", "elephants", "stereos" } 1) {"tray", "key", "enemy", "baby"} Returns: {"trays", "keys", "enemies", "babies" } 2) {"a", "s", "oy", "y", "yy"} Returns: {"as", "ses", "oys", "ies", "yies" } Java Submission public class NounReform { public String[] makePlural(String[] nouns) { String[] ret = new String[nouns.length]; int count = 0; for(String s : nouns) { if(s.endsWith("s") || s.endsWith("z") || s.endsWith("x") || s.endsWith("ch") || s.endsWith("sh")) { //Rule 1 ret[count++] = s + "es"; } else if(s.endsWith("ay") || s.endsWith("ey") || s.endsWith("iy") || s.endsWith("oy") || s.endsWith("uy")) { //Rule 2 ret[count++] = s + "s"; } else if(s.endsWith("y")) { //Rule 3 ret[count++] = s.substring(0, s.length() -1) + "ies"; } else { ret[count++] = s + "s"; } } return ret; } } C# Submission public class NounReform { public string[] makePlural(string[] nouns) { string[] ret = new string[nouns.Length]; int count = 0; foreach(string s in nouns) { if(s.EndsWith("s") || s.EndsWith("z") || s.EndsWith("x") || s.EndsWith("ch") || s.EndsWith("sh")) { //Rule 1 ret[count++] = s + "es"; } else if(s.EndsWith("ay") || s.EndsWith("ey") || s.EndsWith("iy") || s.EndsWith("oy") || s.EndsWith("uy")) { //Rule 2 ret[count++] = s + "s"; } else if(s.EndsWith("y")) { //Rule 3 ret[count++] = s.Substring(0, s.Length -1) + "ies"; } else { ret[count++] = s + "s"; } } return ret; } } C++ Submission #include<vector> #include<string> using namespace std; class NounReform { int endsWith(string a, string b) { if(a.size() < b.size()) return 0; for(int i = 0; i < b.size(); i++) { if(a[a.size()-i-1] != b[b.size()-i-1]) return 0; } return 1; } public: vector<string> makePlural(vector<string> nouns) { vector<string> ret; for(int i = 0; i < nouns.size(); i++) { if(endsWith(nouns[i],"s") || endsWith(nouns[i],"z") || endsWith(nouns[i],"x") || endsWith(nouns[i],"ch") || endsWith(nouns[i],"sh")) { //Rule 1 ret.push_back(nouns[i] + "es"); } else if(endsWith(nouns[i],"ay") || endsWith(nouns[i],"ey") || endsWith(nouns[i],"iy") || endsWith(nouns[i],"oy") || endsWith(nouns[i],"uy")) { //Rule 2 ret.push_back(nouns[i] + "s"); } else if(endsWith(nouns[i],"y")) { //Rule 3 ret.push_back(nouns[i].substr(0, nouns[i].size() -1) + "ies"); } else { ret.push_back(nouns[i] + "s"); } } return ret; } }; VB.NET Submission Public Class NounReform Public Function makePlural(nouns as String()) as String() Dim ret as String() Redim ret(nouns.Length - 1) For i as Integer = 0 To nouns.Length - 1 If nouns(i).EndsWith("s") Or nouns(i).EndsWith("z") Or nouns(i).EndsWith("x") Or nouns(i).EndsWith("ch") Or nouns(i).EndsWith("sh") Then 'Rule 1 ret(i) = nouns(i) + "es" Else If nouns(i).EndsWith("ay") Or nouns(i).EndsWith("ey") Or nouns(i).EndsWith("iy") Or nouns(i).EndsWith("oy") Or nouns(i).EndsWith("uy") Then 'Rule 2 ret(i) = nouns(i) + "s" Else If nouns(i).EndsWith("y") Then 'Rule 3 ret(i) = nouns(i).Substring(0, nouns(i).Length -1) + "ies" Else ret(i) = nouns(i) + "s" End If Next Return ret End Function End Class Example Level 2 Problem Statement (From SRM 302) Problem Statement Fans of the game X-ball frequently use the official players' stats. These stats are presented as a list of players' names and the positions they can play. If a player can play at several positions, he is shown in the list once for each position. The fans want to get the statistics in a new format. In the old format, each element of the list contained a single player and a single position played by that player. In the new format, each element of the list will contain a single player and all the positions that the player can play. The new list must contain the same number of elements as the old one, and players must appear in the same order as before. Each element of the new list must contain the name of the player, a '-' character, and a comma-separated list of all the positions the player can play. The first position must be the same position that was in the corresponding element of the old list, and the other positions must be sorted in alphabetical order. Old format stats: | New format stats: John-DH | John-DH,RP,SP Jack-SP | Jack-SP Sam-OF | Sam-OF John-SP | John-SP,DH,RP John-RP | John-RP,DH,SP You will be given a String[] placeboard containing the statistics in the old format. Each element will contain a player's name and an abbreviation of the player's position in the form "Name-Position" (quotes for clarity). Return the list after converting it to the new format. DEFINITION Class: XBallGame Method: newStatistics Parameters: String[] Returns: String[] Method signature: String[] newStatistics(String[] placeboard) (be sure your methodis public) NOTES - Note that the names of the players are case-sensitive. CONSTRAINTS - placeboard will contain between 1 and 50 elements, inclusive. - Each element of placeboard will contain between 4 and 23 characters, inclusive. - Each element of placeboard will be in the format "Name-Position" without any leading or trailing spaces. - Position will consist of two upper-case letters ('A'-'Z'). - Name will contain between 1 and 20 letters ('A'-'Z', 'a'-'z'), inclusive. - All elements of placeboard will be distinct. EXAMPLES 0) {"John-DH", "Jack-SP", "Sam-OF", "John-SP", "John-RP"} Returns: {"John-DH,RP,SP", "Jack-SP", "Sam-OF", "John-SP,DH,RP", "John-RP,DH,SP" } 1) {"John-DH", "Jack-SP", "Sam-OF", "John-SP", "John-RP", "John-RA", "John-RZ", "Jack-OF", "Sam-SP"} Returns: {"John-DH,RA,RP,RZ,SP", "Jack-SP,OF", "Sam-OF,SP", "John-SP,DH,RA,RP,RZ", "John-RP,DH,RA,RZ,SP", "John-RA,DH,RP,RZ,SP", "John-RZ,DH,RA,RP,SP", "Jack-OF,SP", "Sam-SP,OF" } 2) {"PFwL-GE", "PFwL-EV","agXL-SR", "PFwL-CS", "PFwL-FE", "PFwL-MS", "agXL-VS", "agXL-AL", "PFwL-BI", "PFwL-DK", "PFwL-MW", "PFwL-DS", "agXL-XJ", "agXL-PJ", "agXL-CK", "PFwL-KI", "agXL-KP", "agXL-EL", "PFwL-EA"} Returns: {"PFwL-GE,BI,CS,DK,DS,EA,EV,FE,KI,MS,MW", "PFwL-EV,BI,CS,DK,DS,EA,FE,GE,KI,MS,MW", "agXL-SR,AL,CK,EL,KP,PJ,VS,XJ", "PFwL-CS,BI,DK,DS,EA,EV,FE,GE,KI,MS,MW", "PFwL-FE,BI,CS,DK,DS,EA,EV,GE,KI,MS,MW", "PFwL-MS,BI,CS,DK,DS,EA,EV,FE,GE,KI,MW", "agXL-VS,AL,CK,EL,KP,PJ,SR,XJ", "agXL-AL,CK,EL,KP,PJ,SR,VS,XJ", "PFwL-BI,CS,DK,DS,EA,EV,FE,GE,KI,MS,MW", "PFwL-DK,BI,CS,DS,EA,EV,FE,GE,KI,MS,MW", "PFwL-MW,BI,CS,DK,DS,EA,EV,FE,GE,KI,MS", "PFwL-DS,BI,CS,DK,EA,EV,FE,GE,KI,MS,MW", "agXL-XJ,AL,CK,EL,KP,PJ,SR,VS", "agXL-PJ,AL,CK,EL,KP,SR,VS,XJ", "agXL-CK,AL,EL,KP,PJ,SR,VS,XJ", "PFwL-KI,BI,CS,DK,DS,EA,EV,FE,GE,MS,MW", "agXL-KP,AL,CK,EL,PJ,SR,VS,XJ", "agXL-EL,AL,CK,KP,PJ,SR,VS,XJ", "PFwL-EA,BI,CS,DK,DS,EV,FE,GE,KI,MS,MW" } 3) {"a-AA", "a-ZZ"} Returns: {"a-AA,ZZ", "a-ZZ,AA" } Java Submission import java.util.ArrayList; import java.util.Collections; public class XBallGame { public String[] newStatistics(String[] placeboard) { String[] ret = new String[placeboard.length]; for(int i = 0; i < ret.length; i++) { ret[i] = placeboard[i]; ArrayList C# Submission using System.Collections.Generic; public class XBallGame { public string[] newStatistics(string[] placeboard) { string[] ret = new string[placeboard.Length]; for(int i = 0; i < ret.Length; i++) { ret[i] = placeboard[i]; List C++ Submission #include<vector> #include<string> using namespace std; class XBallGame { public: vector<string> newStatistics(vector<string> placeboard) { vector<string> ret(placeboard.size()); for(int i = 0; i < ret.size(); i++) { ret[i] = placeboard[i]; vector<string> positions; for(int j = 0; j < placeboard.size(); j++) { if(i == j) continue; if(ret[i].substr(0,ret[i].find("-")) == placeboard[j].substr(0,placeboard[j].find("-"))) { //same person positions.push_back(placeboard[j].substr(placeboard[j].find("-")+1)); } } sort(positions.begin(), positions.end()); for(int k = 0; k < positions.size(); k++) { ret[i] = ret[i] + "," + positions[k]; } } return ret; } }; VB.NET Submission Imports System.Collections.Generic Public Class XBallGame Public Function newStatistics(placeboard as String()) as String() Dim ret as String() Redim ret(placeboard.Length-1) For i as Integer = 0 To ret.Length - 1 ret(i) = placeboard(i) Dim positions as New List(Of String) For j as Integer = 0 To placeboard.Length -1 If Not i = j Then If ret(i).Substring(0,ret(i).IndexOf("-")) = placeboard(j).Substring(0,placeboard(j).IndexOf("-")) Then 'same person positions.Add(placeboard(j).Substring(placeboard(j).IndexOf("-")+1)) End If End If Next positions.Sort() For k as Integer = 0 To positions.Count -1 ret(i) = ret(i) + "," + positions(k) Next Next Return ret End Function End Class |
|