Monday, July 22, 2024

The well-known Knuth-Morris-Pratt algorithm.

This algorithm can be explained in terms of the sequence matching between input and patterns this way:


void KMP(string pattern, string text, vector<int> *positions) {

    int patternLength = pattern.length();

    int textLength = text.length();

    int* next =  PreProcess(pattern);

 if (next == 0) return;

    int i = 0;

    int j = 0;

    while ( j < textLength )

 {

  while(true)

   if (text[j] == pattern[i]) //matches

   {

    i++;   // yes, move on to the next state

    if (i == patternLength)  // maybe that was the last state

    {

     // found a match;

     positions->push_back(j-(i-1));

     i = next[i];

    }

    break;

   }

   else if (i == 0) break; // no match in state j = 0, give up

   else i = next[i];

  j++;

 }

}


int* PreProcess( string pattern) {

 int patternLength = pattern.length();

 if (patternLength == 0) return 0;

    int * next = new int[patternLength + 1];

    if (next == 0) return 0;

    next[0] = -1;  // set up for loop below; unused by KMP


    int i = 0;

    int j = -1;

    // next[0] = -1;

    // int len = pattern.length();

    while (i < patternLength) {

  next[i + 1] = next[i] + 1;

  while ( next[i+1] > 0 &&

    pattern[i] != pattern[next[i + 1] - 1])

   next[i + 1] = next[next[i + 1] - 1] + 1;

  i++;

 }

    return next;

}

Usage: DroneDataAddition.docx

Sunday, July 21, 2024

Knuth-Morris-Pratt method of string matching

 Public void KMP-Matcher(String text, String pattern) { 


Int n = text.length(); 


Int m = pattern.length(); 


Int[] prefixes = ComputePrefixFunction(pattern); 


Int noOfCharMatched = 0; 


for ( int I = 1; I <= n; I++) { 


       While (noOfCharMatched > 0 && pattern[noOfCharMatched + 1] != Text[I]) 


    NoOfCharMatched = prefixes[nofOfCharMatched] 


       If (pattern[noOfCharMatched + 1] == text[I])  


   NoOfCharMatched = NoOfCharMatched + 1; 


       If (noOfCharMatched == m) { 


                 System.out.println(“Pattern occurs at “ + I); 


                 NoOfCharMatched = prefixes[NoOfCharMatched]; 


        } 




Public int[] ComputePrefixFunction(String pattern) { 


Int m = pattern.length(); 


Int[] prefixes  = new int[m+1]; 


Prefixes[1] = 0; 


Int k = 0; 


For (int q = 2; q <=m ; q++) { 


While (k > 0 && Pattern[k + 1] != Pattern[q]) 


       K = pattern[k]; 


If (pattern[k+1] == Pattern[q]) { 


      K = k + 1; 



Pattern[q] = k; 



Return prefixes; 


}


Saturday, July 20, 2024

 The steps to create a machine learning pipeline in Azure Machine Learning Workspace:

1. Create an Azure Machine Learning Workspace:

If you don't have one already, create an Azure Machine Learning workspace. This serves as the central hub for managing your machine learning resources.

2. Set Up Datastores:

Datastores allow you to access data needed in your pipeline. By default, each workspace has a default datastore connected to Azure Blob storage. You can register additional datastores if necessary [4].

3. Define Your Pipeline Steps:

Break down your ML task into manageable components (steps). Common steps include data preparation, model training, and evaluation.

Use the Azure Machine Learning SDK to create these steps. You can define them as PythonScriptStep or other relevant step types.

4. Configure Compute Targets:

Set up the compute targets where your pipeline steps will run. Options include Azure Machine Learning Compute, Azure Databricks, or other compute resources.

5. Orchestrate the Pipeline:

Use the Azure Machine Learning pipeline service to automatically manage dependencies between steps.

Specify the order in which steps should execute and how they interact.

6. Publish the Pipeline:

Once your pipeline is ready, publish it. This makes it accessible for later use or sharing with others.

7. Monitor and Track Performance:

Monitor your pipeline's performance in real-world scenarios.

Detect data drift and adjust your pipeline as needed.


This workspace provides an environment to create and manage the end-to-end life cycle of Machine Learning models. Unlike general purpose software, Azure machine learning has significantly different requirements such as the use of a wide variety of technologies, libraries and frameworks, separation of training and testing phases before deploying and use of a model and iterations for model tuning independent of the model creation and training etc.  Azure Machine Learning’s compatibility with open-source frameworks and platforms like PyTorch and TensorFlow makes it an effective all-in-one platform for integrating and handling data and models which tremendously relieves the onus on the business to develop new capabilities. Azure Machine Learning is designed for all skill levels, with advanced MLOps features and simple no-code model creation and deployment. 


Friday, July 19, 2024

 Sample program to count the number of different triplets (a, b, c) in which a occurs before b and b occurs before c from a given array.

Solution: Generate all combinations in positional lexicographical order for given array using getCombinations method described above. Select those with size 3. When selecting the elements, save only their indexes, so that we can determine they are progressive. 

 

class solution {

 

public static void getCombinations(List<Integer> elements, int N, List<List<Integer>> combinations) { 

      for (int i = 0; i < (1<<N); i++) { 

          List<Integer> combination = new ArrayList<>(); 

          for (int j = 0; j < elements.size(); j++) { 

              if ((i & (1 << j)) > 0) { 

                combination.add(j); 

              } 

          } 

          List<Integer> copy = new ArrayList<Integer>(combination); 

          combinations.add(copy); 

      } 

   }

 public static void main (String[] args) {

                  List<Integer> elements = Arrays.asList(1,2,3,4);

List<List<Integer>> indices = new ArrayList<Integer>();

        getCombinations(elements, elements.size(), indices);

        indices.stream().filter(x -> x.size() == 3)  

                      .filter(x -> x.get(0) < x.get(1)  && x.get(1) < x.get(2)) 

                       .forEach(x -> printList(elements, x)); 

       }

 

public static void printList(List<Integer> elements, List<Integer> indices) {

                 StringBuilder sb = new StringBuilder();

                 for (int i = 0; i < indices.size(); i++) {

                        sb.append(elements.get(indices.get(i)) + " "); 

                 }

                 System.out.println(sb.toString());

}

}

/* sample output:

1 2 3 

1 2 4 

1 3 4 

2 3 4

 */


Thursday, July 18, 2024

 

This is a summary of the book titled “The Canary Code:  A guide to Neurodiversity, Dignity and Intersectional Belonging at work”  written by Ludmila Praslova and published by Berrett-Koehler in 2024.  This book is about how to foster an inclusive workplace that celebrates neurodiversity and intersectional dignity and where everyone feels valued and respected. Neurodivergent people with conditions such as autism spectrum disorder, attention deficit disorder, dyslexia, or obsessive-compulsive disorder that impacts the way brain processes information, have suffered to keep up with the rest of the workforce because they don’t fit the status quo. The flexibility provided during Covid-19 came to their rescue and more steps can be taken to promote inclusivity such as hiring and onboarding them by understanding their needs, heeding their input on office space design and workflow flexibility. Leaders should create psychologically safe workplaces by listening, communicating clearly, and aiming for objective performance reviews. When these same employees become leaders, they could pass on the same benefits to others.

Inclusive workplaces promote diverse interaction, communication, and productivity styles by challenging neuronormative standards. Neurodiversity acknowledges the vast variations in human cognition, emotion, and perception, including conditions like ADHD, autism, and dyslexia. Myths about neurodiversity perpetuate exclusion in the workplace, as they lump people together while ignoring neurodivergent needs and preferences. To enable neurodivergent employees to do their best work, organizations must create flexible, inclusive environments that respect individual differences in social, cognitive, emotional, and sensory needs.

The "Canary Code" framework promotes inclusivity for neurodivergent employees. It emphasizes the importance of involving marginalized employees in decision-making, focusing on outcomes, ensuring flexibility, promoting organizational justice, and maintaining transparency, and using appropriate decision-making tools. By adopting these principles, organizations can create a more productive and inclusive workplace.

Companies like Deloitte, Infinite Flow, Legalite, Call Yachol, Ultranauts, and Dell have implemented these principles to ensure a diverse workforce. These practices have improved onboarding processes, engagement, and overall performance. Companies like Dell have also implemented neurodiversity programs, allowing candidates to showcase their abilities. Overall, these principles promote a more inclusive and productive workplace.

To make hiring and onboarding more inclusive, organizations should understand the needs of neurodivergent employees and conduct thorough analyses to ensure job descriptions accurately reflect the position's requirements. This includes separating essential qualifications from desirable ones, using plain language, and focusing on outcomes rather than methods. Onboarding should integrate new employees into the organization, offering a quality "preboarding" experience, providing clear information, and tailoring training methods.

Inclusive office spaces should accommodate a wide range of sensory, physical, and cognitive needs, with employees' input in the design process. Flexible work arrangements, such as flexible schedules, remote work options, and hybrid models, can enhance productivity for neurodivergent employees. Psychologically safe workspaces should be created by listening, communicating clearly, and aiming for objective performance reviews. A toxic work environment features non-inclusive, disrespectful, unethical, cutthroat, and abusive behaviors, which can negatively impact all employees' well-being and performance. By implementing these strategies, organizations can create a more inclusive and supportive work environment for all employees.

Neurodivergent leaders can create more inclusive workspaces by overcoming myths and stigmas that limit recognition and development of diverse leadership talents. By leveraging individual strengths, creating growth tracks, and fostering a culture that values diverse perspectives, companies can unlock innovation, improve morale, and build more resilient leadership teams. Neurodivergent leaders can overcome biases by embracing unique experiences, fostering empathy, and fostering inclusivity within their teams. By remaining authentic, trusting in their unique perspective, and focusing on transparent communication, neurodivergent leaders can inspire others and promote a culture of acceptance and understanding.

Wednesday, July 17, 2024

 Problem Statement: Given an integer array arr, in one move you can select a palindromic subsequence arr[i], ..., arr[j] where 0 <= i <= j < arr.length, and remove that subsequence from the given array. Note that after removing a subarray, the elements move to remove the gap between them.

 

Return the minimum number of moves needed to remove all numbers from the array.

 

Solution:

import java.util.*;

class Solution {

    public int minimumMoves(int[] arr) {

        int N = arr.length;

        int max = 1;

        int min = Integer.MAX_VALUE;

        List<Integer> A = new ArrayList<>();

        for (int i = 0; i < arr.length; i++) A.add(arr[i]);

        int count = 0;

        while(A.size() > 0) {

           boolean hasPalindrome = false; 

           List<Integer> elements = new ArrayList<>();

           for (int i = 0; i < (1<<N); i++) { 

               

               List<Integer> combination = new ArrayList<>(); 

                for (int j = 0; j < A.size(); j++) { 

                  if ((i & (1 << j)) > 0) { 

                    combination.add(j); 

                  } 

                } 

                if (isPalindrome(A, combination) && (combination.size() > max) && getCharactersToRemove(A, combination) < min) {

                      hasPalindrome = true;

                      max = combination.size();

                      min = getCharactersToRemove(A, combination);

                      elements = new ArrayList<>(combination);                

                      if (getCharactersToRemove(A, combination) == 0) { break;}

                } else {

                    // System.out.println("A: " + print(A) + " Elements: " + print(elements) + " Combination: " + print(combination) + "isPalindrome=" + String.valueOf(isPalindrome(A, combination)) + " getCharsToRemove=" + getCharactersToRemove(A, combination) + " min = " + min);

                }

           }            

           if (!hasPalindrome) {

               count += 1;

               A.remove(A.size() - 1);

           } else {

               count += getCharactersToRemove(A, elements) + 1;

               A = removeCharacters(A, elements);

               // System.out.println("Removing " + count + " characters at indices:" + print(elements) + " and remaining elements: " + print(A));

               // elements = new ArrayList<>();

               max = 1;

               min = Integer.MAX_VALUE;

           }

        }

        return count;

    }

    public boolean isPalindrome(List<Integer> A, List<Integer> combination) {

        int start = 0;

        int end = combination.size()-1;

        while (start <= end) {

            if (A.get(combination.get(start)) != A.get(combination.get(end))) {

                return false;

            }

            start++;

            end--;

        }

        return true;

    }

    public int getCharactersToRemove(List<Integer> A, List<Integer> combination){

        if (combination.size() < 2) return 0;

        List<Integer> clone = new ArrayList<>(A); 

        return removeCharacters(clone, combination).size();

    }

    public List<Integer> removeCharacters(List<Integer> A, List<Integer> combination) {

     int start = 0;

     int end = combinations.size()-1;

     int last = 0;

     while (start <= end) {

             for (int i = last; i< A.size(); i++) {

                    if (A.get(i) == combination.get(start)) {

                          A.set(I, Integer.MAX_VALUE);

                          last = i+1;

                          start++;

                    }

             }

     }

    List<Integer> result = new ArrayList<>();

    For (int I = 0; I < A.size(); i++) {

         if (A.get(i) != Integer.MAX_VALUE) {

               result.add(A.get(i));

          }

    }

    return result;

    }

    public List<Integer> removeCharacters(List<Integer> A, List<Integer> combination) {

        int start = combination.get(0);

        int end = combination.get(combination.size()-1);

        List<Integer> result = new ArrayList<>();

        if (start > 0){

            result.addAll(A.subList(0, start));

        }

        if (end < A.size() - 1) {

            result.addAll(A.subList(end + 1,A.size()));

        }

        return result;

    }

    public String print(List<Integer> elements){

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < elements.size(); i++) {

            sb.append(elements.get(i) + " ");

        }

        return sb.toString();

    }

}


Examples:

A = [-1,0,1]           => 3

A = [-1,0,-1]          => 1

A = [-1]                    => 1

A = [-1,0]                => 2

A = [0,0]                 => 1

A = [1,0,1,2,3]     => 3

A = [-2,-1,0,1,0]   => 3


Tuesday, July 16, 2024

 These are more use cases targeted for a commercial drone fleet management software that scales elastically and helps drones manage their flight path in real-time.

Case 8: Safety enforcement on failures across multiple drone units is a scenario that should not be the norm, but it is dedicated to the platform for its capability to operate different fleets. Take the specific example of 55 drone units failing out of 200 for a 4th of July show where the failed units landed in Angle Lake close to SeaTac airport and sank to the bottom, some with their lights on. It was a technical glitch where multiple airborne units failed with “no global positioning” and instead of falling from the sky, made controlled landings into the lake. If there were an override to the GPS failure, it could have resulted in runaways, injury or damage. Each of the drones cost about $2600 to the Great Lakes Drone Co based out of Coloma, Michigan before they engaged in safety policies, procedures and programming to make controlled landings.  External interference such as radio deterrence devices including radio frequency jammers or internal malfunctions such as system compromise were not ruled out. The point of this use case is that the responsibility for the flight of the drones lies not just with the controller but also with the platform relaying commands to the drones. Such a use case that spans all drone fleets and their formations and flight paths across tenants is a specific use case that must be tried out with drills and controlled environments. Some amount of Chaos Engineering practices apply to the handling of these drones via the portal.

Case 9: One of the benefits of a cloud native platform for drone fleet management is that this pseudo resource can make use of other user-friendly services such as OpenAI for chatbot like interaction with the software, Cognitive services for multi-media analysis from drone captures, and for voice translation into commands for the drone fleet. From mundane tasks of using a data lake to stash all sensor captures from the drone fleet organized by individual drones as folders and with data plane access separate from control plane access, to more advanced and sophisticated use cases of correlating and automation service requests against drone fleet inventory and dynamic control of fleet crew, the possibilities are endless. Even workflow management becomes easier with cloud resources and integration. Customized automations are facilitated by the cloud’s powerful methods of interactions namely api, sdk, command line and portal.

Previous use cases: DFCSUseCasesList.docx : https://1drv.ms/w/s!Ashlm-Nw-wnWhPB1Ov2NRhBtAQFyNQ?e=GhfAMw

References: https://1drv.ms/w/s!Ashlm-Nw-wnWhPA9saJLYQGA7q2Wiw?e=AONTxo for data architecture 

https://1drv.ms/w/s!Ashlm-Nw-wnWhO4OGADjCj0GVLyFTA?e=UGMEpB for software description.