HackerRank Js-Week1 5. Camel Case 4

    HackerRank

    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.

    Camel Case(駝峰式命名) 是許多程式語言中常見的命名風格。 在 Java 中,method 和 variable通常以小寫字母開頭,後面的單字則以大寫字母開頭(ex. startThread)。
    class 的名稱按照相同的模式,只是以大寫字母開頭。(ex. BlueCar)。
    
    您的任務是編寫一個程式,可以創建或拆分駝峰式 variable、method 和 class 的名稱。
    

    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.

    輸入格式:
    
    - 輸入文件的每一行都提供一個操作(S 或 C)作為開頭,接著是一個分號,然後是 M、C 或 V,然後是一個分號,最後是您需要處理的單詞。
    - 操作分別是 S(拆分)或 C(合併)。
    - M 表示方法,C 表示類,V 表示變量。
    - 在 s 拆分操作的情況下,單詞將是駝峰式方法、類別或變量名,您需要將其拆分為以空格分隔的以小寫字母開頭的單詞列表。
    - 在 C 組合操作的情況下,單詞將是一個以空格分隔的單詞列表,以小寫字母開頭,您需要將這些單詞組合成適當的駝峰式字符串。 方法應該以一組空括號結尾,以區別於變量名。
    

    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).

    輸出格式:
    
    對於每個輸入行,您的程序應該印出以空格為分隔的單詞列表(在拆分操作的情況下)或適當的駝峰式字符串(在組合操作的情況下)。
    

    Example 1:

    
    S;M;plasticCup()
    C;V;mobile phone
    C;C;coffee machine
    S;C;LargeSoftwareBook
    C;M;white sheet of paper
    S;V;pictureFrame
    

    solution:
    先宣告 arr 為將 input 轉成陣列,如下:

    [
      'S;V;iPad',
      'C;M;mouse pad',
      'C;C;code swarm',
      'S;C;OrangeHighlighter'
    ]
    
    這裡有一個小細節,因為題目給的 input 是連續 6 個待判斷的字串,
    所以用 '\r\n' 來判斷 Enter 後的結果做分割,而‘\r’是回車,’\n’是換行。
    

    接著宣告 upper 為將單詞第一個字母大寫,
    而 lower 為將單詞第一個字母小寫。
    運用 for 迴圈開始處理 arr 中的每個元素:
    – 使用解構賦值的做法將 element 中的字串用 split(‘;’) 提取出來,分別為[sc, mcv, name]。
    – 宣告 checkOperation 變數為 sc === ‘S’ ,如為 true,則代表要拆分,如為 false,則代表要合併。
    – 宣告 checkName 變數,先判斷 checkOperation 是否為 ture,接著運用正則表達式(/(?=[A-Z])/)進行大寫字母前的分割,如為 false,則在大寫字母前使用空格’ ‘。在 map 陣列依序放入元素時,判斷是否需使用lower 或 upper 的大小寫,以及運用 join()合併時是否需要空格。
    – 如果 mcv 為 ‘M’,則配合 method 的需求在單詞尾部加入()。
    – 如果 mcv 為 ‘C’,則配合 class 的需求單詞需小寫。
    – 印出 checkName。

    Code 1: BigO(n)

    function processData(input) {
        //Enter your code here
        const arr = input.split('\r\n')
        const upper = word => word.charAt(0).toUpperCase() + word.slice(1)
        const lower = word => word.charAt(0).toLowerCase() + word.slice(1)
        for (let element of arr) {
            const [sc, mcv, name] = element.split(';')
            const checkOperation = sc === 'S'
            let checkName = name.split(checkOperation ? (/(?=[A-Z])/) : ' ').map(checkOperation ? lower : upper).join(checkOperation ? ' ' : '')
            if (mcv === 'M') checkName = checkOperation ? checkName.slice(0, -2) : checkName + '()'
            if (mcv !== 'C') checkName = lower(checkName)
            console.log(checkName)
        }
    } 
    
    process.stdin.resume();
    process.stdin.setEncoding("ascii");
    _input = "";
    process.stdin.on("data", function (input) {
        _input += input;
    });
    
    process.stdin.on("end", function () {
       processData(_input);
    });
    
    

    FlowChart:
    Example 1

    
    S;M;plasticCup() //plastic cup
    C;V;mobile phone //mobilePhone
    C;C;coffee machine //CoffeeMachine
    S;C;LargeSoftwareBook //large software book
    C;M;white sheet of paper //whiteSheetOfPaper()
    S;V;pictureFrame //picture frame