PACER:プログラミング問題を解決するための構造化プロセス


あなたがプログラミングに新しいとき、あなたは単純化されるならば、3つの層に減らされることができた異なる挑戦に直面します:
  • 論理層
  • 構文レイヤ
  • 生産層
  • 論理層は、プログラミングの問題を理解し、それを解決する1つ以上の方法を提案する能力とスキルを参照します.構文レイヤは、指定された言語でその構文を記述する能力と、その構文と仕様を使用して、生産的なレイヤをツールのセットに示します.フレームワーク、ライブラリ、テクノロジ…そして、ベストプラクティス、技術とソフト、あなたが経験していると経験を持っているように、あなたが与えられた仕事で生産することができます.
    私はあなたとPhaer(問題-アルゴリズム-コード-実行-リファクタ)という名前のプロセスを共有するつもりです、それはあなたが構造化され、意図的な方法でアルゴリズムの問題を解決するために精神的なモデルを作成するのに役立ちますので、あなたの論理層のスキルを向上させる.
    このプロセスは、10年以上の間のプログラミング論理問題を解決するために異なるアプローチを研究して、テストする結果です.

    歩道過程


    問題

  • あなた自身の言葉で問題を定義:これはあなたが解決するために必要なものを理解するために認知プロセスを通過するのに役立ちます.

  • 入力/出力:予想される入力と出力データ型を特定します.この手順では、入力として取得するデータ型/構造体、およびソリューションから戻る必要があるデータ型/構造を指定できます.

  • 例:入力データの例に基づいて期待される結果の例をいくつか書きます.特定の入力を与えられて、あなたが解決から得るべき出力を理解するために、デスクチェック(マニュアルコンピューティング)を使用してください.

  • エッジケース:尋ねるか、(インタビューにおいてでないとき)解決がカバーするべきであるエッジケース.これにより、明示的に問題文に表示されない場合を識別できます.

  • 制限:ソリューションの境界を設定するには、問題に与えられた制限/規則を書き留めます.
  • /* 
    PROBLEM
    
    Write a function that shortens a string of characters fro a - z as follows:
      If the same character repeats consecutively twice, eliminate it from the string.
      Return the shortened string.
    
    - Input: string
    - Output: string
    - Examples:
      "aaabccddd" -> "abd"
    - Edge cases: 
       * What should I return  if I get an empty string
       * What should I return if I get a data type or structure different from a String
    - Restrictions: 
       The string characters are a-z
    */ 
    

    アルゴリズム


    英語では、入力されたデータを期待される出力に変換するように、次の手順を書き留めます.
    /*
    ALGORITHM
    
    Option 1
    
    Define a function superReducedString( ) that receives a string  - str - as a parameter 
      declare a variable newString and assign an empty string as its value
      iterate over every character of the str
        if the character is different from the last character of newString, add it to newString
        otherwise eliminate the last character from newString
      return newString
    
    Option 2   - Regex + Recursion - 
    
    Define a function  superReducedString( ) that receives a string  - str -  as a parameter
     declare a regular expression that matches two equal consecutive characters
     call the string match method to see if there is still duplicates
       if null and str length is 0, return "Empty String"
       if null return str
       otherwise, call the replace method to replace/eliminate the two equal consecutive 
       characters and pass the returned value as the string parameter to call the function 
        recursively
    */
    

    コード


    選択したプログラミング言語に前のアルゴリズムの手順を翻訳します.
    // CODE
    
    // Code for algorithm - option 1 - 
    
    function superReducedString(str) {
      let newString = "";
      for(let i = 0; i < str.length; i += 1) {
        if (str[i] === newString[newString.length -1]) {
          newString = newString.slice(0,-1);
        } else {
          newString += str[i];
        }
      };
      return newString[0]  !== newString[1] ? newString : "Empty String";
    }
    
    // Code for algorithm - option 2 -  // Using regex and recursion
    
    function superReducedString(str) {
      let regex = new RegExp(/([a-z])\1/, "g");
      if (str.match(regex) === null && str.length === 0) {
        return "Empty String";
      } else if (str.match(regex) === null) {
        return str;
      } else {
        return superReducedString(str.replace(regex, ""));
      }
    }
    

    実行


    テストケースが期待結果を返すことを検証するコードを実行します.
    通常、問題の一部としてテストケースを与えられます.次の例では、テストケースと対応する期待結果を以下に示します.
    console.log(superReducedString("aaabccddd")); // "abc"
    console.log(superReducedString("cccxllyyy" )); // "cxy"
    console.log(superReducedString("aa")); // "Empty String"
    console.log(superReducedString("baab")); // "Empty String"
    console.log(superReducedString("fghiiijkllmnnno")); // "fghijkmno"
    console.log(superReducedString("chklssstt")); // "chkls"
    

    リファクタ


    次のことを考えてみましょう.
  • どのように、私はこのコードをより読みやすくすることができましたか?
  • どのように、私はこの複雑さを時間複雑性に関してより効率的にすることができましたか?Big O Notation )?
  • あなたがまだカバーしていないかもしれないエッジケースについて考えて、新しいケースをカバーするために必要であるものを変えるために、アルゴリズムステップからペースメーカープロセスを適用してください.
  • /* 
    REFACTOR
    This example shortens the lines of code written
    */
    
    const regex = /([a-z])\1/g;
    const superReducedString = str => regex.test(str) ? superReducedString(str.replace(regex, "")) : str || "Empty String";
    
    console.log(superReducedString("aaabccddd")); // "abc"
    console.log(superReducedString("cccxllyyy" )); // "cxy"
    console.log(superReducedString("aa")); // "Empty String"
    console.log(superReducedString("baab")); // "Empty String"
    console.log(superReducedString("fghiiijkllmnnno")); // "fghijkmno"
    console.log(superReducedString("chklssstt")); // "chkls"
    
    
    あなたはcheck out the complete example here
    あなたはwatch this video with the process applied in real time - Audio in Spanish -
    あなたが単純な問題を望むならば、以下のリポジトリの1つのフォームを選ぶのを自由に感じてくださいfollow through this one - in Spanish -

    オープンリポジトリ

  • Check out the PACER process repo in English
  • Check out the PACER process repo in Spanish
  • あなたがペースで避けるために起こっている問題

  • あなたが解決しなければならない問題を本当に理解することなくコードを書き始めてください.
  • 代替解を考慮することなく問題を解決する.
  • エッジケースを考慮しないソリューションを記述する無駄時間.
  • アルゴリズム的問題への無効解または不完全解の記述
  • 間違ったデータ型またはデータ構造を返すソリューションを記述する.
  • あなたのプログラミング論理技術を実践するために使用できるプラットフォーム

  • LeetCode
  • Edabit
  • Hacker Rank
  • あなたが疑問を持っているならば、そして、私は至急あなたに戻ります.
    ビッグO記法:アルゴリズムのランタイムがどのように成長するかを記述する方法です.換言すれば、アルゴリズムへのデータの入力が増加するにつれて、アルゴリズムを実行する操作の数がどのように増加するかを記述する方法である.チェックアウトしたいリソースもありますBig O Notation 101 , Big O notation calculator
    注:このブログの記事で使用されるプログラミングの問題は、PacerプロセスをEdabit