Friday, April 12, 2024

 Given clock hands positions for different points of time as pairs A[I][0] and A[I][1] where the order of the hands does not matter but their angle enclosed, count the number of pairs of points of time where the angles are the same

    public static int[] getClockHandsDelta(int[][] A) {

        int[] angles = new int[A.length];

        for (int i = 0; i < A.length; i++){

            angles[i] = Math.max(A[i][0], A[i][1]) - Math.min(A[i][0],A[i][1]);

        }

        return angles;

    }

    public static int NChooseK(int n, int k)

    {

        if (k < 0 || k > n || n == 0) return 0;

        if ( k == 0 || k == n) return 1;

        return Factorial(n) / (Factorial(n-k) * Factorial(k));

    }

 

    public static int Factorial(int n) {

        if (n <= 1) return 1;

        return n * Factorial(n-1);

    }


    public static int countPairsWithIdenticalAnglesDelta(int[] angles){

        Arrays.sort(angles);

        int count = 1;

        int result = 0;

        for (int i = 1; i < angles.length; i++) {

            if (angles[i] == angles[i-1]) {

                count += 1;

            } else {

                if (count > 0) {

                    result += NChooseK(count, 2);

                }

                count = 1;

            }

        }

        if (count > 0) {

            result += NChooseK(count, 2);

            count = 0;

        }

        return result;

    }


        int [][] A = new int[5][2];

         A[0][0] = 1;    A[0][1] = 2;

         A[1][0] = 2;    A[1][1] = 4;

         A[2][0] = 4;    A[2][1] = 3;

         A[3][0] = 2;    A[3][1] = 3;

         A[4][0] = 1;    A[4][1] = 3;

 1 2 1 1 2 

1 1 1 2 2 

4

#codingexercise: https://1drv.ms/w/s!Ashlm-Nw-wnWhOw-gkwgI-LEfc-p1A?e=x18YPl


Thursday, April 11, 2024

 This is a continuation of an earlier article on executing Apache Spark code on Azure Machine Learning Workspace.

Additionally, there are some caveats to mention:

 

1. Container registry must be accessible to the workspace and the image build serverless compute. Allowing incoming ip addresses on the container registry and outbound traffic to the container registry on the subnet associated with all the compute in the workspace, are prerequisites.

2. If the workspace is setup with limited public plane connectivity, it will likely impact the reachability to the azure container registry. Allowing unlimited public access is not required but helps with troubleshooting. 

3. If the compute are all provisioned under a specific subnet, associating a NAT gateway with those compute allows for a fixed ip prefix or even address for all outbound traffic from them. This will be helpful to allow list on the dependencies of the Azure Machine Learning Workspace, whether they are container registry, storage account or keyvault.

4. If there are errors authenticating with the container registry and the image build jobs in the workspace fail with the authentication error complaining about incorrect json, the sync-keys command on the workspace will restore the authentication.


Previous articles: IaCResolutionsPart103.docx

#CodingExercise-04-11-2024 https://1drv.ms/w/s!Ashlm-Nw-wnWhOw8Qdf84RkOPsNMuQ?e=7R8m69



Wednesday, April 10, 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, April 9, 2024

 Question: How to execute Apache Spark code in Azure Machine Learning Workspace as jobs on a non-interactive cluster?

Answer: Unlike Compute Instances on Azure Machine Learning workspace, a non-interactive cluster creation does not take initialization or startup scripts to configure the libraries or packages on the instance. Errors encountered on running a sample Spark code as shown here, will likely result in JAVA_HOME-not-set error or JAVA_GATEWAY_EXITED error.

from pyspark import SparkContext 

sc = SparkContext.getOrCreate() 


# check that it really works by running a job

# example from http://spark.apache.org/docs/latest/rdd-programming-guide.html#parallelized-collections

data = range(10000) 

distData = sc.parallelize(data)

result = distData.filter(lambda x: not x&1).take(10)

print(result)

# Out: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

To execute Apache Spark code in non-interactive jobs in Azure Machine Learning Workspace, we build custom environments. Custom environments allow us to specify the necessary dependencies, packages, and configurations required to run your Spark code.

Here's a step-by-step guide on how to build custom environments for executing Apache Spark code in Azure Machine Learning Workspace:

Define the environment: Start by defining the environment dependencies in a conda or pip environment file. Specify the required Python version, Spark version, and any additional packages or libraries needed for your code. For example, in the option to create an environment using an existing curated one, choose mldesigner:23 and customize the Conda specification with:

name: MyCustomEnvironment

channels:

  - conda-forge

  - defaults

dependencies:

  - python=3.8

  - numpy

  - pyspark

  - pip

  - pip:

    - azureml-core

    - ipython

    - ipykernel

    - pyspark


Specify the environment in the job configuration: When submitting a Spark job in Azure Machine Learning Workspace, we specify the custom environment that we created in the job configuration. This ensures that the job executes using the desired environment. Jobs must be submitted as part of an experiment and this helps with organization and locating jobs from their listing. Experiments are different from Environments.

Execute the job: We submit the Spark job using the Azure Machine Learning SDK or Azure portal. The job will be executed in the specified environment, ensuring that all required dependencies are available.

By building custom environments, we ensure that your Spark code runs consistently and reproducibly in Azure Machine Learning Workspace, regardless of the underlying infrastructure or dependencies.

Azure Machine Learning Workspace also provides pre-built environments with popular data science and machine learning frameworks like Spark, TensorFlow, and PyTorch. These environments are optimized and ready to use out of the box and are helpful for training models. Leveraging built-in over custom helps with automatic maintenance.


Monday, April 8, 2024

 There are N points (numbered from 0 to N−1) on a plane. Each point is colored either red ('R') or green ('G'). The K-th point is located at coordinates (X[K], Y[K]) and its color is colors[K]. No point lies on coordinates (0, 0).

We want to draw a circle centered on coordinates (0, 0), such that the number of red points and green points inside the circle is equal. What is the maximum number of points that can lie inside such a circle? Note that it is always possible to draw a circle with no points inside.

Write a function that, given two arrays of integers X, Y and a string colors, returns an integer specifying the maximum number of points inside a circle containing an equal number of red points and green points.

Examples:

1. Given X = [4, 0, 2, −2], Y = [4, 1, 2, −3] and colors = "RGRR", your function should return 2. The circle contains points (0, 1) and (2, 2), but not points (−2, −3) and (4, 4).

class Solution {

    public int solution(int[] X, int[] Y, String colors) {

        // find the maximum

        double max = Double.MIN_VALUE;

        int count = 0;

        for (int i = 0; i < X.length; i++)

        {

            double dist = X[i] * X[i] + Y[i] * Y[i];

            if (dist > max)

            {

                max = dist;

            }

        }

 

        for (double i = Math.sqrt(max) + 1; i > 0; i -= 0.1)

        {

            int r = 0;

            int g = 0;

            for (int j = 0; j < colors.length(); j++)

            {

                if (Math.sqrt(X[j] * X[j] + Y[j] * Y[j]) > i) 

                {

                    continue;

                }

 

                if (colors.substring(j, j+1).equals("R")) {

                    r++;

                }

                else {

                    g++;

                }

            }

            if ( r == g && r > 0) {

                int min = r * 2;

                if (min > count)

                {

                    count = min;

                }

            }

        }

 

        return count; 

    }

}

 

Compilation successful.

 

Example test:   ([4, 0, 2, -2], [4, 1, 2, -3], 'RGRR')

OK

 

Example test:   ([1, 1, -1, -1], [1, -1, 1, -1], 'RGRG')

OK

 

Example test:   ([1, 0, 0], [0, 1, -1], 'GGR')

OK

 

Example test:   ([5, -5, 5], [1, -1, -3], 'GRG')

OK

 

Example test:   ([3000, -3000, 4100, -4100, -3000], [5000, -5000, 4100, -4100, 5000], 'RRGRG')

OK

#another

#codingexercise

Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.

import java.util.*;

import java.lang.Character;

class Solution {

    public int maxLength(List<String> arr) {

      int N  = arr.size();

      int max = Integer.MIN_VALUE;

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

 

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

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

 

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

 

                combination.add(j);

 

              }

 

          }

          int count = getDistinctCount(arr, combination);

          if (count > max){

              max = count;

          }

      }

      return max;

    }

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

        Map<Character, Integer> charMap = new HashMap<>();

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

              String word = A.get(combination.get(i));

              for (int j = 0; j < word.length(); j++) {

                  if (charMap.containsKey(Character.valueOf(word.charAt(j)))) {

                      return 0;

                  }

                  charMap.put(Character.valueOf(word.charAt(j)), 1);

              }

        }

        return charMap.keySet().size();

    }

}

 


Sunday, April 7, 2024

 Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.

import java.util.*;

import java.lang.Character;

class Solution {

    public int maxLength(List<String> arr) {

      int N  = arr.size();

      int max = Integer.MIN_VALUE;

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

 

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

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

 

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

 

                combination.add(j); 

 

              } 

 

          } 

          int count = getDistinctCount(arr, combination);

          if (count > max){

              max = count;

          }

      } 

      return max;

    }

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

        Map<Character, Integer> charMap = new HashMap<>();

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

              String word = A.get(combination.get(i));

              for (int j = 0; j < word.length(); j++) {

                  if (charMap.containsKey(Character.valueOf(word.charAt(j)))) {

                      return 0;

                  }

                  charMap.put(Character.valueOf(word.charAt(j)), 1);

              }

        }

        return charMap.keySet().size();

    }

}

#codingexercise:

BarChartRectangleStreaming.docx