Search This Blog

Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Saturday, 22 October 2022

Validate Subsequence

List array = new List() { 5, 1, 22, 25, 6, -1, 8, 10 };
List sequence = new List { 1, 6, -1, 10 };
bool isValid = IsValidSubsequence(array, sequence);
Console.WriteLine(isValid);
//Expected Output: true
      

Solution 1.
public static bool IsValidSubsequence0(List array, List sequence)
{
    int seqIdx = 0;
    for (int arrIdx = 0; arrIdx < array.Count && seqIdx < sequence.Count; arrIdx++)
    {
        if (array[arrIdx] == sequence[seqIdx])
        {
            seqIdx++;
        }
    }
    return seqIdx == sequence.Count;
}
      

Test case

Test Case 1
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, -1, 10]
}
Test Case 2
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 25, 6, -1, 8, 10]
}
Test Case 3
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 6, -1, 8, 10]
}
Test Case 4
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [22, 25, 6]
}
Test Case 5
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, 10]
}
Test Case 6
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 10]
}
Test Case 7
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, -1, 8, 10]
}
Test Case 8
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [25]
}
Test Case 9
{
  "array": [1, 1, 1, 1, 1],
  "sequence": [1, 1, 1]
}
Test Case 10
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 25, 6, -1, 8, 10, 12]
}
Test Case 11
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [4, 5, 1, 22, 25, 6, -1, 8, 10]
}
Test Case 12
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 23, 6, -1, 8, 10]
}
Test Case 13
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 22, 25, 6, -1, 8, 10]
}
Test Case 14
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 22, 6, -1, 8, 10]
}
Test Case 15
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, -1, -1]
}
Test Case 16
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, -1, -1, 10]
}
Test Case 17
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, -1, -2]
}
Test Case 18
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [26]
}
Test Case 19
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 25, 22, 6, -1, 8, 10]
}
Test Case 20
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 26, 22, 8]
}
Test Case 21
{
  "array": [1, 1, 6, 1],
  "sequence": [1, 1, 1, 6]
}
Test Case 22
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, -1, 10, 11, 11, 11, 11]
}
Test Case 23
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [5, 1, 22, 25, 6, -1, 8, 10, 10]
}
Test Case 24
{
  "array": [5, 1, 22, 25, 6, -1, 8, 10],
  "sequence": [1, 6, -1, 5]
}

Multi String Search

string bigstring = "this is a big string";
string[] smallstrings = { "this", "yo", "is", "a", "bigger", "string", "kappa" };

List result = MultistringSearch(bigstring, smallstrings);
foreach (bool b in result)
{
    Console.WriteLine(b);
}

//Expected output: [true, false, true, true, false, true, false]
      

Solution 1.
public static List MultistringSearch(string bigstring, string[] smallstrings)
{
    List result = new List();
    foreach (string smalString in smallstrings)
    {
        result.Add(isContain(smalString, bigstring));
    }
    return result;
}


public static bool isContain(string smallString, string bigString)
{
    for (int i = 0; i <= bigString.Length - smallString.Length; i++)
    {
        int j;
        for (j = 0; j < smallString.Length; j++)
        {
            if (bigString[i + j] != smallString[j])
            {
                break;
            }
        }
        if (j == smallString.Length)
        {
            return true;
        }
    }
    return false;
}
      

Solution 2:
public static bool IsContain(string smallString, string bigString)
{
    for (int i = 0; i < bigString.Length; i++)
    {
        if (i + smallString.Length > bigString.Length)
        {
            break;
        }

        if(IsContain(smallString, bigString, i))
        {
            return true;
        }
    }
    return false;
}

private static bool IsContain(string smallString, string bigString, int startIdx)
{
    int leftBigidx = startIdx;
    int rightBigIdx = startIdx + smallString.Length - 1;
    int leftSmallIdx = 0;
    int rightSmallIdx = smallString.Length - 1;
    while (leftBigidx <= rightBigIdx)
    {
        if (bigString[leftBigidx] != smallString[leftSmallIdx] ||
                bigString[rightBigIdx] != smallString[rightSmallIdx])
        {
            return false;
        }
        leftBigidx++;
        rightBigIdx--;
        leftSmallIdx++;
        rightSmallIdx--;
    }
    return true;
}

Test case

Test Case 1
{
  "bigString": "this is a big string",
  "smallStrings": ["this", "yo", "is", "a", "bigger", "string", "kappa"]
}

Test Case 2
{
  "bigString": "abcdefghijklmnopqrstuvwxyz",
  "smallStrings": ["abc", "mnopqr", "wyz", "no", "e", "tuuv"]
}

Test Case 3
{
  "bigString": "abcdefghijklmnopqrstuvwxyz",
  "smallStrings": ["abcdefghijklmnopqrstuvwxyz", "abc", "j", "mnopqr", "pqrstuvwxyz", "xyzz", "defh"]
}

Test Case 4
{
  "bigString": "hj!)!%Hj1jh8f1985n!)51",
  "smallStrings": ["%Hj7", "8f198", "!)5", "!)!", "!!", "jh81", "j181hf"]
}

Test Case 5
{
  "bigString": "Mary goes to the shopping center every week.",
  "smallStrings": ["to", "Mary", "centers", "shop", "shopping", "string", "kappa"]
}

Test Case 6
{
  "bigString": "adcb akfkw afnmc fkadn vkaca jdaf dacb cdba cbda",
  "smallStrings": ["abcd", "acbd", "adbc", "dabc", "cbda", "cabd", "cdab"]
}

Test Case 7
{
  "bigString": "test testing testings tests testers test-takers",
  "smallStrings": ["tests", "testatk", "testiing", "trsatii", "test-taker", "test"]
}

Test Case 8
{
  "bigString": "ndbajwhfawkjljkfaopwdlaawjk dawkj awjkawkfjhkawk ahjwkjad jadfljawd",
  "smallStrings": ["abc", "akwbc", "awbc", "abafac", "ajjfbc", "abac", "jadfl"]
}

Test Case 9
{
  "bigString": "Is this particular test going to pass or is it going to fail? That is the question.",
  "smallStrings": ["that", "the", "questions", "goes", "mountain", "passes", "passed", "going", "is"]
}

Test Case 10
{
  "bigString": "Everything in this test should fail.",
  "smallStrings": ["everything", "inn", "that", "testers", "shall", "failure"]
}

Test Case 11
{
  "bigString": "this ain't a big string",
  "smallStrings": ["this", "is", "yo", "a", "bigger"]
}

Test Case 12
{
  "bigString": "bbbabb",
  "smallStrings": ["bbabb"]
}

Two Number Sum

int[] array = { 1, 2, 4, 5, 7, 8, 9, 6 };
int targetSum = 10;
int[] expectedResult = TwoNumberSum_Solution0(array, targetSum);

foreach (int item in expectedResult)
{
	Console.WriteLine(item);
}
      

Solution 1.
public static int[] TwoNumberSum_Solution0(int[] array, int targetSum)
{

    for (int i = 0; i < array.Length - 1; i++)
    {
        int firstNum = array[i];
        for (int j = i + 1; j < array.Length; j++)
        {
            int secondNum = array[j];
            if (firstNum + secondNum == targetSum)
            {
                return new int[] { array[i], array[j] };
            }
        }
    }
    return new int[0];
}
      

Solution 2
//using System.Collections.Generic;

public static int[] TwoNumberSum(int[] array, int targetSum)
{
    HashSet visited = new HashSet();

    foreach (int num in array)
    {
        int expectedResult = targetSum - num;

        if (visited.Contains(expectedResult))
        {
            return new int[] { expectedResult, num };
        }
        else
        {
            visited.Add(num);
        }
    }
    return new int[0];
}

Test case

{
  "array": [4, 5, -4, 8, 12, 1, -2, 6],
  "targetSum": 10
}

{
  "array": [4, 6],
  "targetSum": 10
}

{
  "array": [4, 6, 1],
  "targetSum": 5
}

{
  "array": [4, 6, 1, -3],
  "targetSum": 3
}

{
  "array": [1, 2, 3, 4, 5, 6, 7, 8, 9],
  "targetSum": 17
}

{
  "array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15],
  "targetSum": 18
}

{
  "array": [-7, -5, -3, -1, 0, 1, 3, 5, 7],
  "targetSum": -5
}

{
  "array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47],
  "targetSum": 163
}

{
  "array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47],
  "targetSum": 164
}

{
  "array": [3, 5, -4, 8, 11, 1, -1, 6],
  "targetSum": 15
}

{
  "array": [14],
  "targetSum": 15
}

{
  "array": [15],
  "targetSum": 15
}

Creating a NuGet Package Feed to Host Artifacts

Step-by-Step Guide: Creating a NuGet Package Feed to Host Artifacts 🔹 Step 1: Create a C# Class Library and Generate NuG...

Recent Post