Camel Case 4 Hacker Rank Solution – Camel Case is a naming style common in many programming languages. In Java, method and variable names typically start with a lowercase letter, with all subsequent words starting with a capital letter (example: startThread). Names of classes follow the same pattern, except that they start with a capital letter (example: BlueCar).
Your task is to write a program that creates or splits Camel Case variable, method, and class names.
Input Format
- Each line of the input file will begin with an operation (S or C) followed by a semi-colon followed by M, C, or V followed by a semi-colon followed by the words you’ll need to operate on.
- The operation will either be S (split) or C (combine)
- M indicates method, C indicates class, and V indicates variable
- In the case of a split operation, the words will be a camel case method, class or variable name that you need to split into a space-delimited list of words starting with a lowercase letter.
- In the case of a combine operation, the words will be a space-delimited list of words starting with lowercase letters that you need to combine into the appropriate camel case String. Methods should end with an empty set of parentheses to differentiate them from variable names.
Output Format
- For each input line, your program should print either the space-delimited list of words (in the case of a split operation) or the appropriate camel case string (in the case of a combine operation).
Sample Input
S;M;plasticCup()
C;V;mobile phone
C;C;coffee machine
S;C;LargeSoftwareBook
C;M;white sheet of paper
S;V;pictureFrame
Sample Output
plastic cup
mobilePhone
CoffeeMachine
large software book
whiteSheetOfPaper()
picture frame
Explanation
- Use Scanner to read in all information as if it were coming from the keyboard.
- Print all information to the console using standard output (System.out.print() or System.out.println()).
- Outputs must be exact (exact spaces and casing).
Camel Case 4 Hacker Rank Solution
Problem solution in Python programming
import sys def splitOperation(words): newword='' space=" " for index, w in enumerate(words): if str(w).isupper(): w=str(w).lower() if index==0: space="" newword= newword + space + w else: space=" " newword=newword +w if typeOf=='M': newword=newword.replace("()","") return newword def concatOperation(words): newword='' wordarray = str(words).split(" ") for index, word in enumerate(wordarray): if index==0 and typeOf!='C': newword= newword+ word else: newword= newword+ str(word).capitalize() if typeOf=='M': newword=newword+"()" return newword for x in sys.stdin.read().splitlines(): oper, typeOf, actualWord = str(x).split(";") newword='' if oper=='S': newword=splitOperation(actualWord) else: newword=concatOperation(actualWord) print(newword)
Problem solution in C# programming
static void Main(String[] args) { var sr = new StreamReader(Console.OpenStandardInput()); var inputs = sr.ReadToEnd().Split('\n').ToList(); string result = string.Empty; string tmpResult = string.Empty; inputs.ForEach(input => { if (input[0] == 'S') tmpResult = Split(input); else if (input[0] == 'C') tmpResult = Combine(input, input[2]); if (!string.IsNullOrEmpty(result)) result += '\n'; result += tmpResult; }); Console.WriteLine(result.Replace("\r", "")); } static string Combine(string s, char mode) { var result = new StringBuilder(); bool nextIsCapital = false; for (var i = 4; i < s.Length; i++) { char c = s[i]; if (i == 4 && mode == 'C') nextIsCapital = true; else if (c == ' ') { nextIsCapital = true; continue; } if (nextIsCapital) { c = (char) (c - 32); nextIsCapital = false; } result.Append(c); } if (mode == 'M') result.Append('(').Append(')'); return result.ToString(); } static string Split(string s) { var result = new StringBuilder(); for (var i = 4; i < s.Length; i++) { char c = s[i]; if (c < 'A') break; if (c < 'a') { result.Append(' '); c = (char) (c + 32); } result.Append(c); } return result.ToString().Trim(); }
Problem solution in Python programming
function processData(input) { //Enter your code here //Convert each line into an item of an array const inputArray = (input.includes("\r")) ? input.split("\r\n") : input.split("\n"); const resultArray = []; for (let input of inputArray) { //Case Split: if (input[0] === "S") { console.log(processSplit(input.substring(4, input.length))); } //Case Concat: else if (input[0] === "C") { console.log(processConcat(input.substring(4, input.length), input[2])); } } //Helper Functions //Case Split: function processSplit(input) { let processedInput = ""; for (let i = 0; i < input.length; i++) { //Case 1: Method's parenthesis -> End if (input[i] === "(") { break; //Case 2: Letter is uppercase -> Add a space before //Only apply for none-beginning letters (i != 0) } else if (input[i] === input[i].toUpperCase() && i > 0) { processedInput += " " + input[i].toLowerCase(); } //Case3: Regular letters else { processedInput += input[i].toLowerCase(); } } return processedInput; } //Case Concat: function processConcat(input, type) { let processedInputArr = input.split(" "); let processedInput = ""; for (let i = 0; i < processedInputArr.length; i++) { //Case1: Concatenate into a Variable or Method for first letter if (i === 0 && ["M", "V"].includes(type)) { processedInput += processedInputArr[0]; } //Case2: After first letter, upper case all beginning letters else { processedInput += processedInputArr[i][0].toUpperCase() + processedInputArr[i].substring(1, processedInputArr[i].length); } } //Case 3: Add method parenthesis if (type === "M") { processedInput += "()"; } return processedInput; } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
Leave a Reply