骆驼案4 - 来自hackerhank的挑战者

问题描述 投票:0回答:1

挑战者解释: 驼峰命名法是许多编程语言中常见的命名风格。在 Java 中,方法和变量名称通常以小写字母开头,所有后续单词都以大写字母开头(例如:startThread)。类的名称遵循相同的模式,只是它们以大写字母开头(例如:BlueCar)。

您的任务是编写一个程序来创建或拆分 Camel Case 变量、方法和类名称。

输入格式

输入文件的每一行都以一个操作(S 或 C)开头,后跟一个分号,后跟 M、C 或 V,再后跟一个分号,后跟您需要操作的单词。 操作可以是 S(拆分)或 C(组合) M表示方法,C表示类,V表示变量 在拆分操作的情况下,单词将是驼峰式方法、类或变量名称,您需要将其拆分为以小写字母开头的空格分隔的单词列表。 在组合操作的情况下,单词将是以空格分隔的以小写字母开头的单词列表,您需要将其组合成适当的驼峰式字符串。方法应以一组空括号结尾,以将其与变量名区分开。 输出格式

对于每个输入行,您的程序应该打印以空格分隔的单词列表(在拆分操作的情况下)或适当的驼峰式字符串(在组合操作的情况下)。 输入示例

S;M;塑料杯()

我的问题:

我认为当第二个参数是 C (类)时,每个单词的第一个字母都是大写(因为驼峰式命名法)。那么,在下面的示例 ['S;C;OrangeHighlighter'] 中,输出应该是 'Orangehighlighter' 而不是 'orangehighlighter',不是吗?为什么!?

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)
    })
}`

我不明白为什么 ['S;C;OrangeHighlighter'] 的输出是橙色荧光笔

algorithm camelcasing
1个回答
0
投票

根据你的问题 - ['S;C;OrangeHighlighter'] 意味着我们需要 Split 一个 Class 名称 - OrangeHighlighter

正如实际问题中提到的 - split 操作的情况下,单词将是 camel case 方法、class 或变量名称,您需要将其拆分为空格分隔的单词列表 以小写字母开头。

这就是为什么它是橙色荧光笔而不是橙色荧光笔,因为空格分隔的单词应该以小写字母开头。

© www.soinside.com 2019 - 2024. All rights reserved.