From d50606dcb102c20631a5d12a390e98355bf00584 Mon Sep 17 00:00:00 2001 From: aikhelis Date: Sat, 1 Feb 2025 14:27:38 +0000 Subject: [PATCH] ts-1 ts-2: naming aligned with language standard --- .../geometric_sequence_triplets.ts | 20 +++++------ .../longest_chain_of_consecutive_numbers.ts | 16 ++++----- ...hain_of_consecutive_numbers_brute_force.ts | 18 +++++----- .../Hash Maps and Sets/pair_sum_unsorted.ts | 12 +++---- .../pair_sum_unsorted_two_pass.ts | 14 ++++---- .../Hash Maps and Sets/verify_sudoku_board.ts | 20 +++++------ .../Hash Maps and Sets/zero_striping.ts | 14 ++++---- .../zero_striping_hash_sets.ts | 12 +++---- .../Two Pointers/is_palindrome_valid.ts | 22 ++++++------ typescript/Two Pointers/largest_container.ts | 18 +++++----- .../largest_container_brute_force.ts | 12 +++---- .../next_lexicographical_sequence.ts | 14 ++++---- typescript/Two Pointers/pair_sum_sorted.ts | 14 ++++---- .../pair_sum_sorted_brute_force.ts | 6 ++-- .../Two Pointers/shift_zeros_to_the_end.ts | 8 ++--- .../shift_zeros_to_the_end_naive.ts | 12 +++---- typescript/Two Pointers/triplet_sum.ts | 36 +++++++++---------- .../Two Pointers/triplet_sum_brute_force.ts | 8 ++--- 18 files changed, 138 insertions(+), 138 deletions(-) diff --git a/typescript/Hash Maps and Sets/geometric_sequence_triplets.ts b/typescript/Hash Maps and Sets/geometric_sequence_triplets.ts index aa8ede8..7725a28 100644 --- a/typescript/Hash Maps and Sets/geometric_sequence_triplets.ts +++ b/typescript/Hash Maps and Sets/geometric_sequence_triplets.ts @@ -1,20 +1,20 @@ -function geometric_sequence_triplets(nums: number[], r: number): number { - const left_map: { [key: number]: number } = {}; - const right_map: { [key: number]: number } = {}; +function geometricSequenceTriplets(nums: number[], r: number): number { + const leftMap: { [key: number]: number } = {}; + const rightMap: { [key: number]: number } = {}; let count = 0; - // Populate 'right_map' with the frequency of each element in the array. + // Populate 'rightMap' with the frequency of each element in the array. for (const x of nums) - right_map[x] = (right_map[x] ?? 0) + 1; + rightMap[x] = (rightMap[x] ?? 0) + 1; // Search for geometric triplets that have x as the center. for (const x of nums) { - // Decrement the frequency of x in right_map since x is now being + // Decrement the frequency of x in rightMap since x is now being // processed and is no longer to the right. - right_map[x] -= 1; + rightMap[x] -= 1; if (x % r === 0) - count += (left_map[x / r] ?? 0) * (right_map[x * r] ?? 0); - // Increment the frequency of x in left_map since it'll be a part of the + count += (leftMap[x / r] ?? 0) * (rightMap[x * r] ?? 0); + // Increment the frequency of x in leftMap since it'll be a part of the // left side of the array once we iterate to the next value of x. - left_map[x] = (left_map[x] ?? 0) + 1; + leftMap[x] = (leftMap[x] ?? 0) + 1; } return count; } \ No newline at end of file diff --git a/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers.ts b/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers.ts index e015097..f57b076 100644 --- a/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers.ts +++ b/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers.ts @@ -1,21 +1,21 @@ -function longest_chain_of_consecutive_numbers(nums: number[]): number { +function longestChainOfConsecutiveNumbers(nums: number[]): number { if (nums.length === 0) return 0; - const num_set = new Set(nums); - let longest_chain = 0; - for (const num of num_set) { + const numSet = new Set(nums); + let longestChain = 0; + for (const num of numSet) { // If the current number is the smallest number in its chain, search for // the length of its chain. - if (!num_set.has(num - 1)) { + if (!numSet.has(num - 1)) { let currentNum = num; let currentChain = 1; // Continue to find the next consecutive numbers in the chain. - while (num_set.has(currentNum + 1)) { + while (numSet.has(currentNum + 1)) { currentNum += 1; currentChain += 1; } - longest_chain = Math.max(longest_chain, currentChain); + longestChain = Math.max(longestChain, currentChain); } } - return longest_chain; + return longestChain; } \ No newline at end of file diff --git a/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.ts b/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.ts index fbb8efe..ac4ca9d 100644 --- a/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.ts +++ b/typescript/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.ts @@ -1,17 +1,17 @@ -function longest_chain_of_consecutive_numbers_brute_force(nums: number[]): number { +function longestChainOfConsecutiveNumbersBruteForce(nums: number[]): number { if (nums.length === 0) return 0; - let longest_chain = 0; + let longestChain = 0; // Look for chains of consecutive numbers that start from each number. for (const num of nums) { - let current_num = num; - let current_chain = 1; + let currentNum = num; + let currentChain = 1; // Continue to find the next consecutive numbers in the chain. - while (nums.includes(current_num + 1)) { - current_num += 1; - current_chain += 1; + while (nums.includes(currentNum + 1)) { + currentNum += 1; + currentChain += 1; } - longest_chain = Math.max(longest_chain, current_chain); + longestChain = Math.max(longestChain, currentChain); } - return longest_chain; + return longestChain; } \ No newline at end of file diff --git a/typescript/Hash Maps and Sets/pair_sum_unsorted.ts b/typescript/Hash Maps and Sets/pair_sum_unsorted.ts index 7fbbeb3..8fcaeb5 100644 --- a/typescript/Hash Maps and Sets/pair_sum_unsorted.ts +++ b/typescript/Hash Maps and Sets/pair_sum_unsorted.ts @@ -1,9 +1,9 @@ -function pair_sum_unsorted(nums: number[], target: number): number[] { - const hashmap = {} +function pairSumUnsorted(nums: number[], target: number): number[] { + const hashMap: { [key: number]: number } = {}; for (let i = 0; i < nums.length; i++) { - if (hashmap[target - nums[i]] !== undefined) - return [hashmap[target - nums[i]], i] - hashmap[nums[i]] = i + if (hashMap[target - nums[i]] !== undefined) + return [hashMap[target - nums[i]], i]; + hashMap[nums[i]] = i; } - return [] + return []; } diff --git a/typescript/Hash Maps and Sets/pair_sum_unsorted_two_pass.ts b/typescript/Hash Maps and Sets/pair_sum_unsorted_two_pass.ts index ec36b9d..b4edd4c 100644 --- a/typescript/Hash Maps and Sets/pair_sum_unsorted_two_pass.ts +++ b/typescript/Hash Maps and Sets/pair_sum_unsorted_two_pass.ts @@ -1,14 +1,14 @@ -function pair_sum_unsorted_two_pass(nums: number[], target: number): number[] { - const num_map = {} +function pairSumUnsortedTwoPass(nums: number[], target: number): number[] { + const numMap: { [key: number]: number } = {}; // First pass: Populate the hash map with each number and its // index. for (let i = 0; i < nums.length; i++) - num_map[nums[i]] = i + numMap[nums[i]] = i; // Second pass: Check for each number's complement in the hash map. for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i] - if (num_map[complement] !== undefined && num_map[complement] !== i) - return [i, num_map[complement]] + const complement = target - nums[i]; + if (numMap[complement] !== undefined && numMap[complement] !== i) + return [i, numMap[complement]]; } - return [] + return []; } diff --git a/typescript/Hash Maps and Sets/verify_sudoku_board.ts b/typescript/Hash Maps and Sets/verify_sudoku_board.ts index 4f1dca7..bbcd5f8 100644 --- a/typescript/Hash Maps and Sets/verify_sudoku_board.ts +++ b/typescript/Hash Maps and Sets/verify_sudoku_board.ts @@ -1,10 +1,10 @@ -function verify_sudoku_board(board: number[][]): boolean { +function verifySudokuBoard(board: number[][]): boolean { // Create hash sets for each row, column, and subgrid to keep // track of numbers previously seen on any given row, column, or // subgrid. - const row_sets: Set[] = Array.from({ length: 9 }, () => new Set()); - const column_sets: Set[] = Array.from({ length: 9 }, () => new Set()); - const subgrid_sets: Set[][] = Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => new Set())); + const rowSets: Set[] = Array.from({ length: 9 }, () => new Set()); + const columnSets: Set[] = Array.from({ length: 9 }, () => new Set()); + const subgridSets: Set[][] = Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => new Set())); for (let r = 0; r < 9; r++) { for (let c = 0; c < 9; c++) { const num = board[r][c]; @@ -12,17 +12,17 @@ function verify_sudoku_board(board: number[][]): boolean { continue; // Check if 'num' has been seen in the current row, // column, or subgrid. - if (row_sets[r].has(num)) + if (rowSets[r].has(num)) return false; - if (column_sets[c].has(num)) + if (columnSets[c].has(num)) return false; - if (subgrid_sets[Math.floor(r / 3)][Math.floor(c / 3)].has(num)) + if (subgridSets[Math.floor(r / 3)][Math.floor(c / 3)].has(num)) return false; // If we passed the above checks, mark this value as seen // by adding it to its corresponding hash sets. - row_sets[r].add(num); - column_sets[c].add(num); - subgrid_sets[Math.floor(r / 3)][Math.floor(c / 3)].add(num); + rowSets[r].add(num); + columnSets[c].add(num); + subgridSets[Math.floor(r / 3)][Math.floor(c / 3)].add(num); } } return true; diff --git a/typescript/Hash Maps and Sets/zero_striping.ts b/typescript/Hash Maps and Sets/zero_striping.ts index ac92317..79f86b8 100644 --- a/typescript/Hash Maps and Sets/zero_striping.ts +++ b/typescript/Hash Maps and Sets/zero_striping.ts @@ -1,20 +1,20 @@ -function zero_striping(matrix: number[][]): void { +function zeroStriping(matrix: number[][]): void { if (!matrix || !matrix[0]) return; const m = matrix.length, n = matrix[0].length; // Check if the first row initially contains a zero. - let first_row_has_zero = false; + let firstRowHasZero = false; for (let c = 0; c < n; c++) { if (matrix[0][c] === 0) { - first_row_has_zero = true; + firstRowHasZero = true; break; } } // Check if the first column initially contains a zero. - let first_col_has_zero = false; + let firstColHasZero = false; for (let r = 0; r < m; r++) { if (matrix[r][0] === 0) { - first_col_has_zero = true; + firstColHasZero = true; break; } } @@ -40,14 +40,14 @@ function zero_striping(matrix: number[][]): void { } // If the first row had a zero initially, set all elements in the // first row to zero. - if (first_row_has_zero) { + if (firstRowHasZero) { for (let c = 0; c < n; c++) { matrix[0][c] = 0; } } // If the first column had a zero initially, set all elements in // the first column to zero. - if (first_col_has_zero) { + if (firstColHasZero) { for (let r = 0; r < m; r++) { matrix[r][0] = 0; } diff --git a/typescript/Hash Maps and Sets/zero_striping_hash_sets.ts b/typescript/Hash Maps and Sets/zero_striping_hash_sets.ts index dde4523..9350e55 100644 --- a/typescript/Hash Maps and Sets/zero_striping_hash_sets.ts +++ b/typescript/Hash Maps and Sets/zero_striping_hash_sets.ts @@ -1,24 +1,24 @@ -function zero_striping_hash_sets(matrix: number[][]): void { +function zeroStripingHashSets(matrix: number[][]): void { if (!matrix || !matrix[0]) return; const m = matrix.length, n = matrix[0].length; - const zero_rows = new Set(), zero_cols = new Set(); + const zeroRows = new Set(), zeroCols = new Set(); // Pass 1: Traverse through the matrix to identify the rows and // columns containing zeros and store their indexes in the // appropriate hash sets. for (let r = 0; r < m; r++) { for (let c = 0; c < n; c++) { if (matrix[r][c] === 0) { - zero_rows.add(r); - zero_cols.add(c); + zeroRows.add(r); + zeroCols.add(c); } } } // Pass 2: Set any cell in the matrix to zero if its row index is - // in 'zero_rows' or its column index is in 'zero_cols'. + // in 'zeroRows' or its column index is in 'zeroCols'. for (let r = 0; r < m; r++) { for (let c = 0; c < n; c++) { - if (zero_rows.has(r) || zero_cols.has(c)) { + if (zeroRows.has(r) || zeroCols.has(c)) { matrix[r][c] = 0; } } diff --git a/typescript/Two Pointers/is_palindrome_valid.ts b/typescript/Two Pointers/is_palindrome_valid.ts index 4d33a5f..270d2b8 100644 --- a/typescript/Two Pointers/is_palindrome_valid.ts +++ b/typescript/Two Pointers/is_palindrome_valid.ts @@ -1,20 +1,20 @@ -function is_palindrome_valid(s: string): boolean { - let left = 0, right = s.length - 1 +function isPalindromeValid(s: string): boolean { + let left = 0, right = s.length - 1; while (left < right) { // Skip non-alphanumeric characters from the left. - while (left < right && !isalnum(s[left])) - left++ + while (left < right && !isAlphaNumeric(s[left])) + left++; // Skip non-alphanumeric characters from the right. - while (left < right && !isalnum(s[right])) - right-- + while (left < right && !isAlphaNumeric(s[right])) + right--; // If the characters at the left and right pointers don't // match, the string is not a palindrome. if (s[left] !== s[right]) - return false - left++ - right-- + return false; + left++; + right--; } - return true + return true; } -const isalnum = (c: string): boolean => /^[a-z0-9]+$/i.test(c); +const isAlphaNumeric = (c: string): boolean => /^[a-z0-9]+$/i.test(c); diff --git a/typescript/Two Pointers/largest_container.ts b/typescript/Two Pointers/largest_container.ts index 03bd298..86d4732 100644 --- a/typescript/Two Pointers/largest_container.ts +++ b/typescript/Two Pointers/largest_container.ts @@ -1,20 +1,20 @@ -function largest_container(heights: number[]): number { - let max_water = 0 - let left = 0, right = heights.length - 1 +function largestContainer(heights: number[]): number { + let maxWater = 0; + let left = 0, right = heights.length - 1; while (left < right) { // Calculate the water contained between the current pair of // lines. - let water = Math.min(heights[left], heights[right]) * (right - left) - max_water = Math.max(max_water, water) + let water = Math.min(heights[left], heights[right]) * (right - left); + maxWater = Math.max(maxWater, water); // Move the pointers inward, always moving the pointer at the // shorter line. If both lines have the same height, move both // pointers inward. if (heights[left] < heights[right]) - left++ + left++; else if (heights[left] > heights[right]) - right-- + right--; else - left++, right-- + left++, right--; } - return max_water + return maxWater; } diff --git a/typescript/Two Pointers/largest_container_brute_force.ts b/typescript/Two Pointers/largest_container_brute_force.ts index ab5d3ed..b1a8703 100644 --- a/typescript/Two Pointers/largest_container_brute_force.ts +++ b/typescript/Two Pointers/largest_container_brute_force.ts @@ -1,13 +1,13 @@ -function largest_container_brute_force(heights: number[]): number { - let n = heights.length - let max_water = 0 +function largestContainerBruteForce(heights: number[]): number { + let n = heights.length; + let maxWater = 0; // Find the maximum amount of water stored between all pairs of // lines. for (let i = 0; i < n - 1; i++) { for (let j = i + 1; j < n; j++) { - let water = Math.min(heights[i], heights[j]) * (j - i) - max_water = Math.max(max_water, water) + let water = Math.min(heights[i], heights[j]) * (j - i); + maxWater = Math.max(maxWater, water); } } - return max_water + return maxWater; } diff --git a/typescript/Two Pointers/next_lexicographical_sequence.ts b/typescript/Two Pointers/next_lexicographical_sequence.ts index 002474b..388f3c4 100644 --- a/typescript/Two Pointers/next_lexicographical_sequence.ts +++ b/typescript/Two Pointers/next_lexicographical_sequence.ts @@ -1,5 +1,5 @@ -function next_lexicographical_sequence(s: string): string { - let letters = [...s]; +function nextLexicographicalSequence(s: string): string { + let letters: string[] = [...s]; // Locate the pivot, which is the first character from the right that breaks // non-increasing order. Start searching from the second-to-last position. let pivot = letters.length - 2; @@ -10,14 +10,14 @@ function next_lexicographical_sequence(s: string): string { if (pivot === -1) return letters.reverse().join(''); // Find the rightmost successor to the pivot. - let rightmost_successor = letters.length - 1; - while (letters[rightmost_successor] <= letters[pivot]) - rightmost_successor--; + let rightmostSuccessor = letters.length - 1; + while (letters[rightmostSuccessor] <= letters[pivot]) + rightmostSuccessor--; // Swap the rightmost successor with the pivot to increase the lexicographical // order of the suffix. - [letters[pivot], letters[rightmost_successor]] = [letters[rightmost_successor], letters[pivot]]; + [letters[pivot], letters[rightmostSuccessor]] = [letters[rightmostSuccessor], letters[pivot]]; // Reverse the suffix after the pivot to minimize its permutation. const suffix = letters.slice(pivot + 1).reverse(); letters.splice(pivot + 1, suffix.length, ...suffix); return letters.join(''); -} +} diff --git a/typescript/Two Pointers/pair_sum_sorted.ts b/typescript/Two Pointers/pair_sum_sorted.ts index eabcd1d..d137243 100644 --- a/typescript/Two Pointers/pair_sum_sorted.ts +++ b/typescript/Two Pointers/pair_sum_sorted.ts @@ -1,18 +1,18 @@ -function pair_sum_sorted(nums: number[], target: number): number[] { - let left = 0, right = nums.length - 1 +function pairSumSorted(nums: number[], target: number): number[] { + let left = 0, right = nums.length - 1; while (left < right) { - let sum = nums[left] + nums[right] + let sum = nums[left] + nums[right]; // If the sum is smaller, increment the left pointer, aiming // to increase the sum toward the target value. if (sum < target) - left++ + left++; // If the sum is larger, decrement the right pointer, aiming // to decrease the sum toward the target value. else if (sum > target) - right-- + right--; // If the target pair is found, return its indexes. else - return [left, right] + return [left, right]; } - return [] + return []; } diff --git a/typescript/Two Pointers/pair_sum_sorted_brute_force.ts b/typescript/Two Pointers/pair_sum_sorted_brute_force.ts index 01eafe8..5006807 100644 --- a/typescript/Two Pointers/pair_sum_sorted_brute_force.ts +++ b/typescript/Two Pointers/pair_sum_sorted_brute_force.ts @@ -1,10 +1,10 @@ -function pair_sum_sorted_brute_force(nums: number[], target: number): number[] { +function pairSumSortedBruteForce(nums: number[], target: number): number[] { const n = nums.length; for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { if (nums[i] + nums[j] === target) - return [i, j] + return [i, j]; } } - return [] + return []; } diff --git a/typescript/Two Pointers/shift_zeros_to_the_end.ts b/typescript/Two Pointers/shift_zeros_to_the_end.ts index f5d08c3..caf7c5e 100644 --- a/typescript/Two Pointers/shift_zeros_to_the_end.ts +++ b/typescript/Two Pointers/shift_zeros_to_the_end.ts @@ -1,14 +1,14 @@ -function shift_zeros_to_the_end(nums: number[]): void { +function shiftZerosToTheEnd(nums: number[]): void { // The 'left' pointer is used to position non-zero elements. - let left = 0 + let left = 0; // Iterate through the array using a 'right' pointer to locate non-zero // elements. for (let right = 0; right < nums.length; right++) { if (nums[right] !== 0) { - [nums[left], nums[right]] = [nums[right], nums[left]] + [nums[left], nums[right]] = [nums[right], nums[left]]; // Increment 'left' since it now points to a position already occupied // by a non-zero element. - left++ + left++; } } } diff --git a/typescript/Two Pointers/shift_zeros_to_the_end_naive.ts b/typescript/Two Pointers/shift_zeros_to_the_end_naive.ts index fde25b3..282002c 100644 --- a/typescript/Two Pointers/shift_zeros_to_the_end_naive.ts +++ b/typescript/Two Pointers/shift_zeros_to_the_end_naive.ts @@ -1,15 +1,15 @@ -function shift_zeros_to_the_end_naive(nums: number[]): void { - let temp = Array(nums.length).fill(0); - let i = 0 +function shiftZerosToTheEndNaive(nums: number[]): void { + let temp: number[] = Array(nums.length).fill(0); + let i = 0; // Add all non-zero elements to the left of 'temp'. for (let number of nums) { if (number !== 0) { - temp[i] = number - i++ + temp[i] = number; + i++; } } // Set 'nums' to 'temp'. for (let j = 0; j < nums.length; j++) { - nums[j] = temp[j] + nums[j] = temp[j]; } } diff --git a/typescript/Two Pointers/triplet_sum.ts b/typescript/Two Pointers/triplet_sum.ts index ad4c0f9..d009846 100644 --- a/typescript/Two Pointers/triplet_sum.ts +++ b/typescript/Two Pointers/triplet_sum.ts @@ -1,41 +1,41 @@ -function triplet_sum(nums: number[]): number[][] { - const triplets = [] - nums.sort() +function tripletSum(nums: number[]): number[][] { + const triplets: Array<[number, number, number]> = []; + nums.sort(); for (let i = 0; i < nums.length; i++) { // Optimization: triplets consisting of only positive numbers // will never sum to 0. if (nums[i] > 0 ) - break + break; // To avoid duplicate triplets, skip 'a' if it's the same as // the previous number. if (i > 0 && nums[i] === nums[i - 1]) - continue + continue; // Find all pairs that sum to a target of '-a' (-nums[i]). - const pairs = pair_sum_sorted_all_pairs(nums, i + 1, -nums[i]) + const pairs = pairSumSortedAllPairs(nums, i + 1, -nums[i]); for (const pair of pairs) { - triplets.push([nums[i]].concat(pair)) + triplets.push([nums[i], pair[0], pair[1]]); } } - return triplets + return triplets; } -function pair_sum_sorted_all_pairs(nums: number[], start: number, target: number): number[]{ - const pairs = [] - let left = start, right = nums.length - 1 +function pairSumSortedAllPairs(nums: number[], start: number, target: number): number[][] { + const pairs: Array<[number, number]> = []; + let left = start, right = nums.length - 1; while (left < right) { - const sum = nums[left] + nums[right] + const sum = nums[left] + nums[right]; if (sum === target) { - pairs.push([nums[left], nums[right]]) - left++ + pairs.push([nums[left], nums[right]]); + left++; // To avoid duplicate '[b, c]' pairs, skip 'b' if it's the // same as the previous number. while (left < right && nums[left] === nums[left - 1]) - left++ + left++; } else if (sum < target) { - left++ + left++; } else { - right-- + right--; } } - return pairs + return pairs; } diff --git a/typescript/Two Pointers/triplet_sum_brute_force.ts b/typescript/Two Pointers/triplet_sum_brute_force.ts index fb9dcfc..659a795 100644 --- a/typescript/Two Pointers/triplet_sum_brute_force.ts +++ b/typescript/Two Pointers/triplet_sum_brute_force.ts @@ -1,7 +1,7 @@ -function triplet_sum_brute_force(nums: number[]): number[][] { - let n = nums.length +function tripletSumBruteForce(nums: number[]): number[][] { + let n = nums.length; // Use a hash set to ensure we don't add duplicate triplets. - let triplets = new Set() + let triplets: Set = new Set(); // Iterate through the indexes of all triplets. for (let i = 0; i < n - 2; i++) { for (let j = i + 1; j < n - 1; j++) { @@ -16,5 +16,5 @@ function triplet_sum_brute_force(nums: number[]): number[][] { } } // [javascript] convert the Set back into an array of triplets. - return Array.from(triplets).map(JSON.parse) + return Array.from(triplets).map(JSON.parse); }