Search code examples
algorithmcamelcasing

Camel Case 4 - Challenger from hackerhank


Challenger explain: 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()

My question:

I thought when the second argument is C (of class), the first letter is of each word is upper case (because Camel Case). So, in the following example ['S;C;OrangeHighlighter'], the output should be 'Orange Highlighter' instead 'orange highlighter', should not? And Why!?

f`unction main(inputLines: string[]) {
    inputLines.forEach((el) => {
        let shouldSeparate: boolean;
        let firstLetterUpper: boolean;
        let haveParentheses: boolean;
        let isCapitalizeWithoutTheFirstChar: boolean;
    
        // identify requirements
        const temp = el.split(';');
        if (temp[0] === 'S') {
            shouldSeparate = true;
        } else if (temp[0] === 'C') {
            shouldSeparate = false;
        }
        
        if (temp[1] === 'C') {
            firstLetterUpper = true;
            isCapitalizeWithoutTheFirstChar = true;
            haveParentheses = false;
        } else if (temp[1] === 'M') {
            firstLetterUpper = false;
            haveParentheses = true;
            isCapitalizeWithoutTheFirstChar = true;
        } else {
            firstLetterUpper = false;
            haveParentheses = false;
            isCapitalizeWithoutTheFirstChar = false;
        }

    
        let tempCleaned: string[] | string = temp[2]
        let def = [];
        if (shouldSeparate) {
            if (isCapitalizeWithoutTheFirstChar) {
                let i = 0;
                def = tempCleaned.split(/(?=[A-Z])/).map((str) => {
                    if (!i) {
                        i++;
                        return str;
                    } else {
                       return str.substring(0,1).toUpperCase() + str.substring(1);
                    }
                })
                tempCleaned = def.join(' ')
            } else {
                let i = 0;
                def = tempCleaned.split(/(?=[A-Z])/).map((str) => {
                    if (!i) {
                        i++;
                        return str;
                    } else {
                        return str.substring(0,1).toLowerCase() + str.substring(1);
                    }
                })
                tempCleaned = def.join(' ')
            }
        } else {
            tempCleaned = tempCleaned.split(' ')
            if (isCapitalizeWithoutTheFirstChar) {
                for (let k = 0; k < tempCleaned.length; k++) {
                    if (k === 0) {
                        def.push(tempCleaned[k])
                    } else {
                        const firstLetterUpper = tempCleaned[k].substring(0,1).toUpperCase()
                        const restsOfWord = tempCleaned[k].substring(1)

                        def.push(firstLetterUpper + restsOfWord);
                    }   
                }
            }
            tempCleaned = def
            tempCleaned = tempCleaned.join('')
        }

        if (firstLetterUpper) {
            tempCleaned = tempCleaned[0].toUpperCase() + tempCleaned.slice(1);        
        } else {
            tempCleaned = tempCleaned[0].toLowerCase() + tempCleaned.slice(1);        
        }

        if (haveParentheses) {
            tempCleaned = tempCleaned + '()';
        }

        console.log(tempCleaned)
    })
}`

I don't understanding why the output in the case ['S;C;OrangeHighlighter'] is orange highlighter


Solution

  • As per your question - ['S;C;OrangeHighlighter'] means we need to Split a Class Name - OrangeHighlighter

    And as mentioned in the actual question - 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.

    that's why it's orange highlighter instead of Orange Highlighter, as the space-delimited words should start with small letters.