diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 1645496..330c268 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -68,7 +68,7 @@ jobs: # yamllint enable rule:line-length steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v5 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index fba0ed6..0cc4b1f 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -18,7 +18,7 @@ jobs: name: "Build Docker images" runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -75,7 +75,7 @@ jobs: needs: build steps: - name: Download artifact - uses: actions/download-artifact@v4 + uses: actions/download-artifact@v5 with: name: ${{ env.ARTIFACT_NAME }}_lint path: /tmp/ @@ -95,7 +95,7 @@ jobs: needs: build steps: - name: Download artifact - uses: actions/download-artifact@v4 + uses: actions/download-artifact@v5 with: name: ${{ env.ARTIFACT_NAME }}_test path: /tmp/ @@ -109,53 +109,56 @@ jobs: run: | docker run --rm ${{ env.IMAGE_NAME }}:test make test - security: - name: "Snyk Container" - runs-on: ubuntu-24.04 - needs: build - permissions: - actions: read - contents: read - security-events: write - steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 - - name: Download artifact - uses: actions/download-artifact@v4 - with: - name: ${{ env.ARTIFACT_NAME }}_prod - path: /tmp/ + # yamllint disable rule:line-length + # security: + # name: "Snyk Container" + # runs-on: ubuntu-24.04 + # needs: build + # permissions: + # actions: read + # contents: read + # security-events: write + # steps: + # - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + # - name: Download artifact + # uses: actions/download-artifact@v4 + # with: + # name: ${{ env.ARTIFACT_NAME }}_prod + # path: /tmp/ + + # - name: Load image + # run: | + # docker load --input /tmp/${{ env.ARTIFACT_NAME }}_prod.tar + # docker image ls -a + + # - name: Run Snyk to check Docker image for vulnerabilities + # # Snyk can be used to break the build when it detects vulnerabilities. + # # In this case we want to upload the issues to GitHub Code Scanning + # continue-on-error: true + # uses: snyk/actions/docker@master + # env: + # # yamllint disable rule:line-length + # # In order to use the Snyk Action you will need to have a Snyk API token. + # # See https://docs.snyk.io/integrations/ci-cd-integrations/github-actions-integration#getting-your-snyk-token + # # or you can sign up for free at https://snyk.io/login + # # yamllint enable rule:line-length + # SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} + # with: + # image: ${{ env.IMAGE_NAME }}:${{ github.sha }} + # args: --file=Dockerfile + # # yamllint disable rule:line-length + # # https://github.com/github/codeql-action/issues/2187#issuecomment-2043220400 + # - name: Replace security-severity undefined for license-related findings + # run: | + # sed -i 's/"security-severity": "undefined"/"security-severity": "0"/g' snyk.sarif + # sed -i 's/"security-severity": "null"/"security-severity": "0"/g' snyk.sarif + # # yamllint enable rule:line-length + # - name: Upload result to GitHub Code Scanning + # uses: github/codeql-action/upload-sarif@v3 + # with: + # sarif_file: "snyk.sarif" + # yamllint enable rule:line-length - - name: Load image - run: | - docker load --input /tmp/${{ env.ARTIFACT_NAME }}_prod.tar - docker image ls -a - - - name: Run Snyk to check Docker image for vulnerabilities - # Snyk can be used to break the build when it detects vulnerabilities. - # In this case we want to upload the issues to GitHub Code Scanning - continue-on-error: true - uses: snyk/actions/docker@master - env: - # yamllint disable rule:line-length - # In order to use the Snyk Action you will need to have a Snyk API token. - # See https://docs.snyk.io/integrations/ci-cd-integrations/github-actions-integration#getting-your-snyk-token - # or you can sign up for free at https://snyk.io/login - # yamllint enable rule:line-length - SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} - with: - image: ${{ env.IMAGE_NAME }}:${{ github.sha }} - args: --file=Dockerfile - # yamllint disable rule:line-length - # https://github.com/github/codeql-action/issues/2187#issuecomment-2043220400 - - name: Replace security-severity undefined for license-related findings - run: | - sed -i 's/"security-severity": "undefined"/"security-severity": "0"/g' snyk.sarif - sed -i 's/"security-severity": "null"/"security-severity": "0"/g' snyk.sarif - # yamllint enable rule:line-length - - name: Upload result to GitHub Code Scanning - uses: github/codeql-action/upload-sarif@v3 - with: - sarif_file: "snyk.sarif" scan: name: "Trivy" runs-on: ubuntu-24.04 @@ -166,7 +169,7 @@ jobs: security-events: write steps: - name: Download artifact - uses: actions/download-artifact@v4 + uses: actions/download-artifact@v5 with: name: ${{ env.ARTIFACT_NAME }}_prod path: /tmp/ @@ -177,7 +180,7 @@ jobs: docker image ls -a - name: Run Trivy vulnerability scanner (cli report) - uses: aquasecurity/trivy-action@0.31.0 + uses: aquasecurity/trivy-action@0.33.0 with: image-ref: ${{ env.IMAGE_NAME }}:${{ github.sha }} format: "table" @@ -185,7 +188,7 @@ jobs: TRIVY_DB_REPOSITORY: ${{ vars.TRIVY_DB_REPOSITORY }} - name: Run Trivy vulnerability scanner (sarif report) - uses: aquasecurity/trivy-action@0.31.0 + uses: aquasecurity/trivy-action@0.33.0 with: image-ref: ${{ env.IMAGE_NAME }}:${{ github.sha }} format: "sarif" diff --git a/.github/workflows/dotnet-coverage.yml b/.github/workflows/dotnet-coverage.yml index 28f0231..7658d66 100644 --- a/.github/workflows/dotnet-coverage.yml +++ b/.github/workflows/dotnet-coverage.yml @@ -23,7 +23,7 @@ jobs: os: ["windows-2025"] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - name: Setup .NET uses: actions/setup-dotnet@v4 with: @@ -35,6 +35,6 @@ jobs: - name: Test run: dotnet test --no-build --verbosity normal - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v5.4.3 + uses: codecov/codecov-action@3cb13a12348ef4ffcf9783ac0f74954f92113e33 with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/dotnet-lint.yml b/.github/workflows/dotnet-lint.yml index e1b4902..74f24ba 100644 --- a/.github/workflows/dotnet-lint.yml +++ b/.github/workflows/dotnet-lint.yml @@ -24,7 +24,7 @@ jobs: dotnet-version: [8.x, 9.x] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - name: Setup .NET ${{ matrix.dotnet-version }} uses: actions/setup-dotnet@v4 with: diff --git a/.github/workflows/dotnet-test.yml b/.github/workflows/dotnet-test.yml index fa50b2e..a4ecaf4 100644 --- a/.github/workflows/dotnet-test.yml +++ b/.github/workflows/dotnet-test.yml @@ -24,7 +24,7 @@ jobs: dotnet-version: [8.x, 9.x] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - name: Setup .NET ${{ matrix.dotnet-version }} uses: actions/setup-dotnet@v4 with: diff --git a/.github/workflows/gitleaks.yml b/.github/workflows/gitleaks.yml index e0356e7..1184192 100644 --- a/.github/workflows/gitleaks.yml +++ b/.github/workflows/gitleaks.yml @@ -1,5 +1,4 @@ --- - name: gitleaks on: # yamllint disable-line rule:truthy @@ -22,10 +21,10 @@ jobs: name: gitleaks runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 with: fetch-depth: 0 - - uses: gitleaks/gitleaks-action@v2 + - uses: gitleaks/gitleaks-action@bf2dc8e55639c1e091e9b45970152e4313705814 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Only required for Organizations, not personal accounts. diff --git a/.github/workflows/markdown-lint.yml b/.github/workflows/markdown-lint.yml index 1b47c9a..19425ee 100644 --- a/.github/workflows/markdown-lint.yml +++ b/.github/workflows/markdown-lint.yml @@ -24,7 +24,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 - name: Set up Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 diff --git a/.github/workflows/snyk-code.yml b/.github/workflows/snyk-code.yml index 06548e0..969e7e9 100644 --- a/.github/workflows/snyk-code.yml +++ b/.github/workflows/snyk-code.yml @@ -27,7 +27,7 @@ jobs: - name: Restore dependencies run: dotnet restore algorithm_exercises_csharp.sln - name: Run Snyk to check for vulnerabilities - uses: snyk/actions/dotnet@master + uses: snyk/actions/dotnet@e2221410bff24446ba09102212d8bc75a567237d continue-on-error: true # To make sure that SARIF upload gets called env: SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 136cfb0..1a93c19 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -14,12 +14,12 @@ jobs: runs-on: windows-2025 steps: - name: Set up JDK - uses: actions/setup-java@v4 + uses: actions/setup-java@v5 with: java-version: 21 # Alternative distribution options are available. distribution: "temurin" - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: # Shallow clones should be disabled for a better relevancy of analysis fetch-depth: 0 @@ -51,7 +51,7 @@ jobs: run: | .\.sonar\scanner\dotnet-sonarscanner begin ` /k:"sir-gon_algorithm-exercises-csharp" ` - /o:"sir-gon" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" ` + /o:"sir-gon" /d:sonar.token="${{ env.SONAR_TOKEN }}" ` /d:sonar.host.url="https://sonarcloud.io" ` /d:sonar.cs.opencover.reportsPaths=**/coverage.opencover.xml @@ -59,4 +59,4 @@ jobs: dotnet build --no-restore dotnet test --no-build --verbosity normal .\.sonar\scanner\dotnet-sonarscanner end ` - /d:sonar.token="${{ secrets.SONAR_TOKEN }}" + /d:sonar.token="${{ env.SONAR_TOKEN }}" diff --git a/.github/workflows/yamllint.yml b/.github/workflows/yamllint.yml index d155b52..c9f16b2 100644 --- a/.github/workflows/yamllint.yml +++ b/.github/workflows/yamllint.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-24.04 steps: - name: Checkout repository - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 - name: Install yamllint run: pip install yamllint diff --git a/docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md b/docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md new file mode 100644 index 0000000..1177d8e --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md @@ -0,0 +1,143 @@ +# [Greedy Algorithms: Max Min](https://www.hackerrank.com/challenges/angry-children) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#greedyalgorithms` + +You will be given a list of integers, `arr`, and a single integer `k`. +You must create an array of length `k` from elements of `arr` such that +its unfairness is minimized. +Call that array `arr'`. +Unfairness of an array is calculated as + +$$ +\textsf{\textbf{max(arr')}} - \textsf{\textbf{min(arr')}} +$$ + +Where: + +- max denotes the largest integer in `arr'`. +- min denotes the smallest integer in `arr'`. + +## Example + +`arr = [1, 4, 7, 2]` +`k = 2` + +Pick any two elements, say `arr' = [4, 7]`. + +$ \textsf{\textbf{unfairness}} + = + \textsf{\textbf{max(4, 7)}} + - + \textsf{\textbf{min(4, 7)}} + = 7 - 4 = 3 +$ + +Testing for all pairs, the solution [1, 2] provides the minimum unfairness. + +**Note**: Integers in `arr` may not be unique. + +## Function Description + +Complete the maxMin function in the editor below. +maxMin has the following parameter(s): + +- `int k`: the number of elements to select +- `int arr[n]`: an array of integers + +## Returns + +- int: the minimum possible unfairness + +## Input Format + +The first line contains an integer , the number of elements in array . +The second line contains an integer . +Each of the next lines contains an integer where . + +## Constraints + +- $ 2 \leq n \leq 10^5 $ +- $ 2 \leq k \leq n $ +- $ 0 \leq arr[i] \leq 10^9 $ + +## Sample Input 0 + +```text +7 +3 +10 +100 +300 +200 +1000 +20 +30 +``` + +## Sample Output 0 + +```text +20 +``` + +## Explanation 0 + +Here `k = 3`; selecting the `3` integers `10, 20,30`, unfairness equals + +```text +max(10,20,30) - min(10,20,30) = 30 - 10 = 20 +``` + +## Sample Input 1 + +```text +10 +4 +1 +2 +3 +4 +10 +20 +30 +40 +100 +200 +``` + +## Sample Output 1 + +```text +3 +``` + +## Explanation 1 + +Here `k = 4`; selecting the `4` integers `1, 2, 3, 4`, unfairness equals + +```text +max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3 +``` + +## Sample Input 2 + +```text +5 +2 +1 +2 +1 +2 +1 +``` + +## Sample Output 2 + +```text +0 +``` + +## Explanation 2 + +Here `k = 2`. `arr' = [2, 2]` or `arr' = [1, 1]` give the minimum unfairness of `0`. diff --git a/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md b/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md new file mode 100644 index 0000000..6127c88 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md @@ -0,0 +1,65 @@ +# [Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` + +## Using bitwise operations + +A prime is a natural number greater than `1` that has no positive divisors other +than `1` and itself. +Given `p` integers, determine the primality of each integer and return `Prime` +or `Not prime` on a new line. + +**Note**: If possible, try to come up with an $ \mathcal{O}(\sqrt{n}) $ +primality algorithm, or see what sort of optimizations you can come up with for +san $ \mathcal{O}(\sqrt{n}) $ algorithm. Be sure to check out the Editorial +after submitting your code. + +## Function Description + +Complete the primality function in the editor below. +primality has the following parameter(s): + +- `int` n: an integer to test for primality + +## Returns + +- string: Prime if is prime, or Not prime + +## Input Format + +The first line contains an integer, , the number of integers to check for primality. +Each of the subsequent lines contains an integer, , the number to test. + +## Constraints + +- $ 1 \leq p \leq 30 $ +- $ 1 \leq n \leq 2 × 10^9 $ + +## Sample Input + +```text +STDIN Function +----- -------- +3 p = 3 (number of values to follow) +12 n = 12 (first number to check) +5 n = 5 +7 n = 7 +``` + +## Sample Output + +```text +Not prime +Prime +Prime +``` + +## Explanation + +We check the following $ p = 3 $ integers for primality: + +1. $ n = 12 $ is divisible by numbers other than $ 1 $ and itself + (i.e.: $ 2 $, $ 3 $, $ 4 $, $ 6 $). +1. $ n = 5 $ is only divisible and itself. +1. $ n = 7 $ is only divisible and itself. diff --git a/docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md b/docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md new file mode 100644 index 0000000..5544b33 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md @@ -0,0 +1,120 @@ +# [Miscellaneous: Flipping bits](https://www.hackerrank.com/challenges/flipping-bits) + +- Difficulty: `#easy` +- Category: `#ProblemSolvingBasic` `#BitManipulation` + +You will be given a list of 32 bit unsigned integers. +Flip all the bits ( `1 -> 0` and `0 -> 1`) +and return the result as an unsigned integer. + +## Example + +$ n = 9_{10} $. We're working with 32 bits, so: + +$ 9_{10} = 1001_{2} $ + +$ 00000000000000000000000000001001_{2} = 9_{10} $ +$ 11111111111111111111111111110110_{2} = 4294967286_{10} $ + +Return `4294967286` + +## Function Description + +Complete the flippingBits function in the editor below. + +flippingBits has the following parameter(s): + +- `int n`: an integer + +## Returns + +- `int`: the unsigned decimal integer result + +## Input Format + +The first line of the input contains `q`, the number of queries. +Each of the next `q` lines contain an integer, `n`, to process. + +## Constraints + +- $ 1 \leq q \leq 100 $ +- $ 0 \leq n \leq 2^{32} $ + +## Sample Input 0 + +```text +3 +2147483647 +1 +0 +``` + +## Sample Output 0 + +```text +2147483648 +4294967294 +4294967295 +``` + +## Explanation 0 + +$ 01111111111111111111111111111111_{2} = 2147483647_{10} $ +$ 10000000000000000000000000000000_{2} = 2147483648_{10} $ + +$ 00000000000000000000000000000001_{2} = 1_{10} $ +$ 11111111111111111111111111111110_{2} = 4294967294_{10} $ + +$ 00000000000000000000000000000000_{2} = 0_{10} $ +$ 11111111111111111111111111111110_{2} = 4294967295_{10} $ + +## Sample Input 1 + +```text +2 +4 +123456 +``` + +## Sample Output 1 + +```text +4294967291 +4294843839 +``` + +## Explanation 1 + +$ 00000000000000000000000000000100_{2} = 4_{10} $ +$ 11111111111111111111111111111011_{2} = 4294967291_{10} $ + +$ 00000000000000011110001001000000_{2} = 4_{10} $ +$ 11111111111111100001110110111111_{2} = 429484389_{10} $ + +## Sample Input 2 + +```text +3 +0 +802743475 +35601423 +``` + +## Sample Output 2 + +```text +4294967295 +3492223820 +4259365872 +``` + +## Explanation 2 + +$ 00000000000000000000000000000000_{2} = 4_{10} $ +$ 11111111111111111111111111111111_{2} = 4294967295_{10} $ + +$ 00101111110110001110010010110011_{2} = 802743475_{10} $ +$ 11010000001001110001101101001100_{2} = 3492223820_{10} $ + +$ 00000010000111110011110000001111_{2} = 35601423_{10} $ +$ 11111101111000001100001111110000_{2} = 4259365872_{10} $ diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs index edece1c..58e25a4 100644 --- a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs @@ -27,13 +27,8 @@ public static int minimumBribesCalculate(List q) List fragment = q[Math.Min(Math.Max(value - 2, 0), i)..i]; - foreach (int k in fragment) - { - if (k > value) - { - bribes += 1; - } - } + bribes += fragment.Count(k => k > value); + i += 1; } diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs index 616ecd0..16b9775 100644 --- a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs @@ -32,16 +32,9 @@ public static bool checkMagazineCompute(List magazine, List note ArgumentNullException.ThrowIfNull(magazine); ArgumentNullException.ThrowIfNull(note); - Dictionary dictionary = []; - - foreach (string word in magazine) - { - if (!dictionary.TryAdd(word, 1)) - { - int currentValue = dictionary[word]; - dictionary[word] = currentValue + 1; - } - } + Dictionary dictionary = magazine + .GroupBy(word => word) + .ToDictionary(g => g.Key, g => g.Count()); foreach (string word in note) { diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs new file mode 100644 index 0000000..4412feb --- /dev/null +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFlorist.cs @@ -0,0 +1,33 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms; + +using System.Diagnostics.CodeAnalysis; + + +/** + * AngryFlorist. + * + */ +public static class AngryFlorist +{ + /** + * maxMin. + */ + + public static int maxMin(int k, List arr) + { + List sortedlist = new List(arr); + sortedlist.Sort(); + int result = sortedlist[sortedlist.Count - 1] - sortedlist[0]; + + for (int i = 0; i < sortedlist.Count - k + 1; i++) + { + int tmin = sortedlist[i]; + int tmax = sortedlist[i + k - 1]; + result = Math.Min(result, tmax - tmin); + } + + return result; + } +} diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.cs new file mode 100644 index 0000000..87d90e4 --- /dev/null +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.cs @@ -0,0 +1,32 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +using System.Diagnostics.CodeAnalysis; +using System.Text; + +public static class FlippingBits +{ + public static long flippingBits(long n) + { + string binaryString = Convert.ToString(n, 2); + binaryString = binaryString.PadLeft(32, '0'); // Ensure 32 bits + StringBuilder flippedBinaryString = new StringBuilder(); + + foreach (char binaryDigit in binaryString) + { + if (binaryDigit == '1') + { + flippedBinaryString.Append('0'); + } + else + { + flippedBinaryString.Append('1'); + } + } + + long number = Convert.ToUInt32(flippedBinaryString.ToString(), 2); + + return number; + } +} diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsAlternative.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsAlternative.cs new file mode 100644 index 0000000..d6a7672 --- /dev/null +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsAlternative.cs @@ -0,0 +1,14 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +using System.Diagnostics.CodeAnalysis; +using System.Text; + +public static class FlippingBitsAlternative +{ + public static long flippingBits(long n) + { + return ~n & 0xFFFFFFFF; // Use bitwise NOT and mask to ensure 32 bits + } +} diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs new file mode 100644 index 0000000..9273f8a --- /dev/null +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs @@ -0,0 +1,56 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +using System.Diagnostics.CodeAnalysis; +using System.Text; + + +public static class TimeComplexityPrimality +{ + private const string PRIME = "Prime"; + private const string NOT_PRIME = "Not prime"; + + + private static Int32? primeFactor(int n) + { + if (n < 2) + { + return null; + } + + int divisor = n; + Int32? maxPrimeFactor = null; + int i = 2; + while (i <= Math.Sqrt(divisor)) + { + if (divisor % i == 0) + { + divisor = divisor / i; + maxPrimeFactor = i; + } + else + { + i += 1; + } + } + + if (maxPrimeFactor == null) + { + return n; + } + + return maxPrimeFactor; + } + + + /** + * primality. + */ + public static string primality(int n) + { + Int32? pf = primeFactor(n); + + return (pf == null || pf != n) ? NOT_PRIME : PRIME; + } +} diff --git a/src/algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj b/src/algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj index edba766..c0b82e5 100644 --- a/src/algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj +++ b/src/algorithm_exercises_csharp_base/algorithm_exercises_csharp_base.csproj @@ -30,9 +30,9 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - - + + + diff --git a/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json new file mode 100644 index 0000000..fe259d7 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json @@ -0,0 +1,26 @@ +[ + { + "title": "Sample Test case 0", + "k": 3, + "arr": [10, 100, 300, 200, 1000, 20, 30], + "expected": 20 + }, + { + "title": "Sample Test case 1", + "k": 4, + "arr": [1, 2, 3, 4, 10, 20, 30, 40, 100, 200], + "expected": 3 + }, + { + "title": "Sample Test case 2", + "k": 2, + "arr": [1, 2, 1, 2, 1], + "expected": 0 + }, + { + "title": "Sample Test case 16", + "k": 3, + "arr": [100, 200, 300, 350, 400, 401, 402], + "expected": 2 + } +] diff --git a/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json new file mode 100644 index 0000000..04ec605 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json @@ -0,0 +1,66 @@ +[ + { + "title": "Sample Test case 0", + "tests": [ + { + "input": 12, + "answer": "Not prime" + }, + { + "input": 5, + "answer": "Prime" + }, + { + "input": 7, + "answer": "Prime" + } + ] + }, + { + "title": "Sample Test case 1", + "tests": [ + { + "input": 31, + "answer": "Prime" + }, + { + "input": 33, + "answer": "Not prime" + } + ] + }, + { + "title": "Sample Test case 2", + "tests": [ + { + "input": 2, + "answer": "Prime" + }, + { + "input": 7, + "answer": "Prime" + }, + { + "input": 1982, + "answer": "Not prime" + }, + { + "input": 14582734, + "answer": "Not prime" + }, + { + "input": 9891, + "answer": "Not prime" + } + ] + }, + { + "title": "Sample Test case 0", + "tests": [ + { + "input": 1, + "answer": "Not prime" + } + ] + } +] diff --git a/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json new file mode 100644 index 0000000..2e5d736 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json @@ -0,0 +1,49 @@ +[ + { + "title": "Sample Test Case 0", + "tests": [ + { + "input": 2147483647, + "answer": 2147483648 + }, + { + "input": 1, + "answer": 4294967294 + }, + { + "input": 0, + "answer": 4294967295 + } + ] + }, + { + "title": "Sample Test Case 1", + "tests": [ + { + "input": 4, + "answer": 4294967291 + }, + { + "input": 123456, + "answer": 4294843839 + } + ] + }, + { + "title": "Sample Test Case 2", + "tests": [ + { + "input": 0, + "answer": 4294967295 + }, + { + "input": 802743475, + "answer": 3492223820 + }, + { + "input": 35601423, + "answer": 4259365872 + } + ] + } +] diff --git a/src/algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj b/src/algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj index 6d04acc..323f735 100644 --- a/src/algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj +++ b/src/algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj @@ -38,10 +38,10 @@ all - - + + - + diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs index f32f986..df835fc 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs @@ -8,22 +8,9 @@ public class ArraysLeftRotationTest { public class ArraysLeftRotationsTestCase(List input, int d_rotations, List expected) { - private List input = input; - private int d_rotations = d_rotations; - private List expected = expected; - - public List Input - { - get { return input; } - } - public int D_rotations - { - get { return d_rotations; } - } - public List Expected - { - get { return expected; } - } + public List Input { get; } = input; + public int D_rotations { get; } = d_rotations; + public List Expected { get; } = expected; } private List testCases = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushBruteForce.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushBruteForce.Test.cs index 85a33cc..58cf105 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushBruteForce.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushBruteForce.Test.cs @@ -8,26 +8,10 @@ public class CrushBruteForceTest { public class CrushBruteForceTestCase(string title, List> queries, int n, long expected) { - private string title = title; - private List> queries = queries; - private int n = n; - private long expected = expected; - public string Title - { - get { return title; } - } - public List> Queries - { - get { return queries; } - } - public int N - { - get { return n; } - } - public long Expected - { - get { return expected; } - } + public string Title { get; } = title; + public List> Queries { get; } = queries; + public int N { get; } = n; + public long Expected { get; } = expected; } private List testCases { get; set; } = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushOptimized.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushOptimized.Test.cs index 52a9eda..add2787 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushOptimized.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushOptimized.Test.cs @@ -8,26 +8,10 @@ public class CrushOptimizedTest { public class CrushOptimizedTestCase(string title, List> queries, int n, long expected) { - private string title = title; - private List> queries = queries; - private int n = n; - private long expected = expected; - public string Title - { - get { return title; } - } - public List> Queries - { - get { return queries; } - } - public int N - { - get { return n; } - } - public long Expected - { - get { return expected; } - } + public string Title { get; } = title; + public List> Queries { get; } = queries; + public int N { get; } = n; + public long Expected { get; } = expected; } private List testCases { get; set; } = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs index 17e2e4a..01ff49b 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs @@ -8,24 +8,9 @@ public class NewYearChaosTest { public class NewYearChaosTestCase(string title, int[] input, string expected) { - private readonly string title = title; - private readonly List input = [.. input]; - private readonly string expected = expected; - - public string Title - { - get { return title; } - } - - public List Input - { - get { return input; } - } - - public string Expected - { - get { return expected; } - } + public string Title { get; } = title; + public List Input { get; } = [.. input]; + public string Expected { get; } = expected; } private List testCases { get; set; } = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/TwoDArray.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/TwoDArray.Test.cs index 1f4fb50..2a7387d 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/TwoDArray.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/TwoDArray.Test.cs @@ -8,22 +8,9 @@ public class TwoDArrayTest { public class TwoDArrayTestCase(string title, List> input, long expected) { - private string title = title; - private List> input = input; - private long expected = expected; - - public string Title - { - get { return title; } - } - public List> Input - { - get { return input; } - } - public long Expected - { - get { return expected; } - } + public string Title { get; } = title; + public List> Input { get; } = input; + public long Expected { get; } = expected; } private List testCases { get; set; } = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs new file mode 100644 index 0000000..baa50b8 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/AngryFloristTest.cs @@ -0,0 +1,50 @@ +namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.greedy_algorithms; + +using algorithm_exercises_csharp_test.common; +using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms; + +/** + * AngryFloristTest. + */ +[TestClass] +public class AngryFloristTest +{ + public class AngryFloristTestCase(string title, int k, List arr, int expected) + { + public string Title { get; } = title; + public int K { get; } = k; + public List Arr { get; } = arr; + public int Expected { get; } = expected; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testLuckBalance() + { + foreach (AngryFloristTestCase test in testCases) + { + int result = AngryFlorist.maxMin(test.K, test.Arr); + + Assert.AreEqual( + test.Expected, + result, + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "AngryFlorist.maxMin({0}, {1}) => must be: {2}", + test.K, + test.Arr.ToString(), + test.Expected + ) + ); + } + } +} diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.Test.cs index f261dfb..5267192 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.Test.cs @@ -8,27 +8,10 @@ public class LuckBalanceTest { public class LuckBalanceTestCase(string title, int k, List> contests, int expected) { - private string title = title; - private int k = k; - private List> contests = contests; - private int expected = expected; - - public string Title - { - get { return title; } - } - public int K - { - get { return k; } - } - public List> Contests - { - get { return contests; } - } - public int Expected - { - get { return expected; } - } + public string Title { get; } = title; + public int K { get; } = k; + public List> Contests { get; } = contests; + public int Expected { get; } = expected; } private List testCases { get; set; } = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.Test.cs index 13f7484..f9825a6 100644 --- a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.Test.cs +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.Test.cs @@ -8,22 +8,9 @@ public class MinimumAbsoluteDifferenceInAnArrayTest { public class MinimumAbsoluteDifferenceInAnArrayTestCase(string title, List input, int expected) { - private readonly string title = title; - private readonly List input = [.. input]; - private readonly int expected = expected; - - public string Title - { - get { return title; } - } - public List Input - { - get { return input; } - } - public int Expected - { - get { return expected; } - } + public string Title { get; } = title; + public List Input { get; } = [.. input]; + public int Expected { get; } = expected; } private List testCases { get; set; } = default!; diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.Test.cs new file mode 100644 index 0000000..6218359 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.Test.cs @@ -0,0 +1,54 @@ +namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous; + +using algorithm_exercises_csharp_test.common; +using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +[TestClass] +public class FlippingBitsTest +{ + public class FlippingBitsTestCaseTest(long input, long answer) + { + public long Input { get; } = input; + public long Answer { get; } = answer; + } + + public class FlippingBitsTestCase(string title, List tests) + { + public string Title { get; } = title; + public List Tests { get; } = tests; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testFlippingBits() + { + long result; + + foreach (FlippingBitsTestCase tests in testCases) + { + foreach (FlippingBitsTestCaseTest test in tests.Tests) + { + result = FlippingBits.flippingBits(test.Input); + Assert.AreEqual( + test.Answer, + result, + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "FlippingBits.flippingBits({0}) => must be: {1}", + test.Input, + test.Answer + ) + ); + } + } + } +} diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsAlternative.Test.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsAlternative.Test.cs new file mode 100644 index 0000000..bbe9a19 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsAlternative.Test.cs @@ -0,0 +1,54 @@ +namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous; + +using algorithm_exercises_csharp_test.common; +using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +[TestClass] +public class FlippingBitsAlternativeTest +{ + public class FlippingBitsAlternativeTestCaseTest(long input, long answer) + { + public long Input { get; } = input; + public long Answer { get; } = answer; + } + + public class FlippingBitsAlternativeTestCase(string title, List tests) + { + public string Title { get; } = title; + public List Tests { get; } = tests; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testFlippingBitsAlternative() + { + long result; + + foreach (FlippingBitsAlternativeTestCase tests in testCases) + { + foreach (FlippingBitsAlternativeTestCaseTest test in tests.Tests) + { + result = FlippingBitsAlternative.flippingBits(test.Input); + Assert.AreEqual( + test.Answer, + result, + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "FlippingBitsAlternative.flippingBits({0}) => must be: {1}", + test.Input, + test.Answer + ) + ); + } + } + } +} diff --git a/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs new file mode 100644 index 0000000..a7ce1c8 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs @@ -0,0 +1,53 @@ +namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous; + +using algorithm_exercises_csharp_test.common; +using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +[TestClass] +public class TimeComplexityPrimalityTest +{ + public class TimeComplexityPrimalityTestCaseTest(int input, String answer) + { + public int Input { get; } = input; + public String Answer { get; } = answer; + } + + public class TimeComplexityPrimalityTestCase(string title, List tests) + { + public string Title { get; } = title; + public List Tests { get; } = tests; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testTimeComplexityPrimality() + { + foreach (TimeComplexityPrimalityTestCase tests in testCases) + { + foreach (TimeComplexityPrimalityTestCaseTest test in tests.Tests) + { + String result = TimeComplexityPrimality.primality(test.Input); + + Assert.AreEqual( + test.Answer, + result, + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "TimeComplexityPrimality.primality({0}) => must be: {1}", + test.Input, + test.Answer + ) + ); + } + } + } +}