Wednesday, March 20, 2024

 

3 ways to normalize xyz coordinates:

1)       

import pandas as pd

from sklearn import preprocessing

 

# Create a sample dataframe with XYZ coordinates

df = pd.DataFrame.from_dict({

    'X': [10.5, 20.3, 15.7, 8.9],

    'Y': [30.2, 25.1, 18.6, 22.0],

    'Z': [12.0, 14.8, 11.2, 9.5]

})

 

# Normalize the XYZ coordinates using min-max scaling

min_max_scaler = preprocessing.MinMaxScaler()

normalized_xyz = min_max_scaler.fit_transform(df)

 

# Create a new dataframe with the normalized values

normalized_df = pd.DataFrame(normalized_xyz, columns=['X', 'Y', 'Z'])

 

print(normalized_df)

 

2)       

 

import pandas as pd

import numpy as np

 

# Create a sample dataframe with XYZ coordinates

df = pd.DataFrame({

    'X': [10.5, 20.3, 15.7, 8.9],

    'Y': [30.2, 25.1, 18.6, 22.0],

    'Z': [12.0, 14.8, 11.2, 9.5]

})

 

# Calculate L2 norm for each row

row_l2_norms = np.linalg.norm(df[['X', 'Y', 'Z']].values, axis=1)

 

# Normalize the coordinates by dividing each row by its L2 norm

normalized_xyz = df[['X', 'Y', 'Z']].div(row_l2_norms, axis=0)

 

print(normalized_xyz)

 

3)      By hand

def normalize_xyz(coordinates):

    # Find the minimum and maximum values for each coordinate

    min_x = min(coord[0] for coord in coordinates)

    max_x = max(coord[0] for coord in coordinates)

    min_y = min(coord[1] for coord in coordinates)

    max_y = max(coord[1] for coord in coordinates)

    min_z = min(coord[2] for coord in coordinates)

    max_z = max(coord[2] for coord in coordinates)

 

    # Normalize each coordinate

    normalized_coords = []

    for x, y, z in coordinates:

        norm_x = (x - min_x) / (max_x - min_x)

        norm_y = (y - min_y) / (max_y - min_y)

        norm_z = (z - min_z) / (max_z - min_z)

        normalized_coords.append((norm_x, norm_y, norm_z))

 

    return normalized_coords

 

# Example usage

xyz_coordinates = [(10.5, 30.2, 12.0), (20.3, 25.1, 14.8), (15.7, 18.6, 11.2), (8.9, 22.0, 9.5)]

normalized_xyz = normalize_xyz(xyz_coordinates)

 

print(normalized_xyz)


#codingexercise 

Subarray Sum equals K 

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals tok. 

A subarray is a contiguous non-empty sequence of elements within an array. 

Example 1: 

Input: nums = [1,1,1], k = 2 

Output: 2 

Example 2: 

Input: nums = [1,2,3], k = 3 

Output: 2 

Constraints: 

·        1 <= nums.length <= 2 * 104 

·        -1000 <= nums[i] <= 1000 

·        -107 <= k <= 107 

 

class Solution { 

    public int subarraySum(int[] nums, int k) { 

        if (nums == null || nums.length == 0) return -1; 

        int[] sums = new int[nums.length];    

        int sum = 0; 

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

            sum += nums[i]; 

            sums[i] = sum; 

        } 

        int count = 0; 

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

            for (int j = i; j < nums.length; j++) { 

                int current = nums[i] + (sums[j] - sums[i]); 

                if (current == k){ 

                    count += 1; 

                } 

            } 

        } 

        return count; 

    } 

} 

 

[1,3], k=1 => 1 

[1,3], k=3 => 1 

[1,3], k=4 => 1 

[2,2], k=4 => 1 

[2,2], k=2 => 2 

[2,0,2], k=2 => 4 

[0,0,1], k=1=> 3 

[0,1,0], k=1=> 2 

[0,1,1], k=1=> 3 

[1,0,0], k=1=> 3 

[1,0,1], k=1=> 4 

[1,1,0], k=1=> 2 

[1,1,1], k=1=> 3 

[-1,0,1], k=0 => 2 

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

[1,0,-1], k=0 => 2 

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

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

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

 

 

 

 

 


No comments:

Post a Comment