Skip to content

Commit 4d249f1

Browse files
authored
Merge pull request #61 from aikhelis/typescript_solutions_naming_convention_refactor
TypeScript: Chapters 1-2 naming aligned with language standard
2 parents 729224f + d50606d commit 4d249f1

18 files changed

+138
-138
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
function geometric_sequence_triplets(nums: number[], r: number): number {
2-
const left_map: { [key: number]: number } = {};
3-
const right_map: { [key: number]: number } = {};
1+
function geometricSequenceTriplets(nums: number[], r: number): number {
2+
const leftMap: { [key: number]: number } = {};
3+
const rightMap: { [key: number]: number } = {};
44
let count = 0;
5-
// Populate 'right_map' with the frequency of each element in the array.
5+
// Populate 'rightMap' with the frequency of each element in the array.
66
for (const x of nums)
7-
right_map[x] = (right_map[x] ?? 0) + 1;
7+
rightMap[x] = (rightMap[x] ?? 0) + 1;
88
// Search for geometric triplets that have x as the center.
99
for (const x of nums) {
10-
// Decrement the frequency of x in right_map since x is now being
10+
// Decrement the frequency of x in rightMap since x is now being
1111
// processed and is no longer to the right.
12-
right_map[x] -= 1;
12+
rightMap[x] -= 1;
1313
if (x % r === 0)
14-
count += (left_map[x / r] ?? 0) * (right_map[x * r] ?? 0);
15-
// Increment the frequency of x in left_map since it'll be a part of the
14+
count += (leftMap[x / r] ?? 0) * (rightMap[x * r] ?? 0);
15+
// Increment the frequency of x in leftMap since it'll be a part of the
1616
// left side of the array once we iterate to the next value of x.
17-
left_map[x] = (left_map[x] ?? 0) + 1;
17+
leftMap[x] = (leftMap[x] ?? 0) + 1;
1818
}
1919
return count;
2020
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
function longest_chain_of_consecutive_numbers(nums: number[]): number {
1+
function longestChainOfConsecutiveNumbers(nums: number[]): number {
22
if (nums.length === 0)
33
return 0;
4-
const num_set = new Set(nums);
5-
let longest_chain = 0;
6-
for (const num of num_set) {
4+
const numSet = new Set(nums);
5+
let longestChain = 0;
6+
for (const num of numSet) {
77
// If the current number is the smallest number in its chain, search for
88
// the length of its chain.
9-
if (!num_set.has(num - 1)) {
9+
if (!numSet.has(num - 1)) {
1010
let currentNum = num;
1111
let currentChain = 1;
1212
// Continue to find the next consecutive numbers in the chain.
13-
while (num_set.has(currentNum + 1)) {
13+
while (numSet.has(currentNum + 1)) {
1414
currentNum += 1;
1515
currentChain += 1;
1616
}
17-
longest_chain = Math.max(longest_chain, currentChain);
17+
longestChain = Math.max(longestChain, currentChain);
1818
}
1919
}
20-
return longest_chain;
20+
return longestChain;
2121
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
function longest_chain_of_consecutive_numbers_brute_force(nums: number[]): number {
1+
function longestChainOfConsecutiveNumbersBruteForce(nums: number[]): number {
22
if (nums.length === 0)
33
return 0;
4-
let longest_chain = 0;
4+
let longestChain = 0;
55
// Look for chains of consecutive numbers that start from each number.
66
for (const num of nums) {
7-
let current_num = num;
8-
let current_chain = 1;
7+
let currentNum = num;
8+
let currentChain = 1;
99
// Continue to find the next consecutive numbers in the chain.
10-
while (nums.includes(current_num + 1)) {
11-
current_num += 1;
12-
current_chain += 1;
10+
while (nums.includes(currentNum + 1)) {
11+
currentNum += 1;
12+
currentChain += 1;
1313
}
14-
longest_chain = Math.max(longest_chain, current_chain);
14+
longestChain = Math.max(longestChain, currentChain);
1515
}
16-
return longest_chain;
16+
return longestChain;
1717
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
function pair_sum_unsorted(nums: number[], target: number): number[] {
2-
const hashmap = {}
1+
function pairSumUnsorted(nums: number[], target: number): number[] {
2+
const hashMap: { [key: number]: number } = {};
33
for (let i = 0; i < nums.length; i++) {
4-
if (hashmap[target - nums[i]] !== undefined)
5-
return [hashmap[target - nums[i]], i]
6-
hashmap[nums[i]] = i
4+
if (hashMap[target - nums[i]] !== undefined)
5+
return [hashMap[target - nums[i]], i];
6+
hashMap[nums[i]] = i;
77
}
8-
return []
8+
return [];
99
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
function pair_sum_unsorted_two_pass(nums: number[], target: number): number[] {
2-
const num_map = {}
1+
function pairSumUnsortedTwoPass(nums: number[], target: number): number[] {
2+
const numMap: { [key: number]: number } = {};
33
// First pass: Populate the hash map with each number and its
44
// index.
55
for (let i = 0; i < nums.length; i++)
6-
num_map[nums[i]] = i
6+
numMap[nums[i]] = i;
77
// Second pass: Check for each number's complement in the hash map.
88
for (let i = 0; i < nums.length; i++) {
9-
const complement = target - nums[i]
10-
if (num_map[complement] !== undefined && num_map[complement] !== i)
11-
return [i, num_map[complement]]
9+
const complement = target - nums[i];
10+
if (numMap[complement] !== undefined && numMap[complement] !== i)
11+
return [i, numMap[complement]];
1212
}
13-
return []
13+
return [];
1414
}

typescript/Hash Maps and Sets/verify_sudoku_board.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
function verify_sudoku_board(board: number[][]): boolean {
1+
function verifySudokuBoard(board: number[][]): boolean {
22
// Create hash sets for each row, column, and subgrid to keep
33
// track of numbers previously seen on any given row, column, or
44
// subgrid.
5-
const row_sets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
6-
const column_sets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
7-
const subgrid_sets: Set<number>[][] = Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => new Set()));
5+
const rowSets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
6+
const columnSets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
7+
const subgridSets: Set<number>[][] = Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => new Set()));
88
for (let r = 0; r < 9; r++) {
99
for (let c = 0; c < 9; c++) {
1010
const num = board[r][c];
1111
if (num === 0)
1212
continue;
1313
// Check if 'num' has been seen in the current row,
1414
// column, or subgrid.
15-
if (row_sets[r].has(num))
15+
if (rowSets[r].has(num))
1616
return false;
17-
if (column_sets[c].has(num))
17+
if (columnSets[c].has(num))
1818
return false;
19-
if (subgrid_sets[Math.floor(r / 3)][Math.floor(c / 3)].has(num))
19+
if (subgridSets[Math.floor(r / 3)][Math.floor(c / 3)].has(num))
2020
return false;
2121
// If we passed the above checks, mark this value as seen
2222
// by adding it to its corresponding hash sets.
23-
row_sets[r].add(num);
24-
column_sets[c].add(num);
25-
subgrid_sets[Math.floor(r / 3)][Math.floor(c / 3)].add(num);
23+
rowSets[r].add(num);
24+
columnSets[c].add(num);
25+
subgridSets[Math.floor(r / 3)][Math.floor(c / 3)].add(num);
2626
}
2727
}
2828
return true;

typescript/Hash Maps and Sets/zero_striping.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
function zero_striping(matrix: number[][]): void {
1+
function zeroStriping(matrix: number[][]): void {
22
if (!matrix || !matrix[0])
33
return;
44
const m = matrix.length, n = matrix[0].length;
55
// Check if the first row initially contains a zero.
6-
let first_row_has_zero = false;
6+
let firstRowHasZero = false;
77
for (let c = 0; c < n; c++) {
88
if (matrix[0][c] === 0) {
9-
first_row_has_zero = true;
9+
firstRowHasZero = true;
1010
break;
1111
}
1212
}
1313
// Check if the first column initially contains a zero.
14-
let first_col_has_zero = false;
14+
let firstColHasZero = false;
1515
for (let r = 0; r < m; r++) {
1616
if (matrix[r][0] === 0) {
17-
first_col_has_zero = true;
17+
firstColHasZero = true;
1818
break;
1919
}
2020
}
@@ -40,14 +40,14 @@ function zero_striping(matrix: number[][]): void {
4040
}
4141
// If the first row had a zero initially, set all elements in the
4242
// first row to zero.
43-
if (first_row_has_zero) {
43+
if (firstRowHasZero) {
4444
for (let c = 0; c < n; c++) {
4545
matrix[0][c] = 0;
4646
}
4747
}
4848
// If the first column had a zero initially, set all elements in
4949
// the first column to zero.
50-
if (first_col_has_zero) {
50+
if (firstColHasZero) {
5151
for (let r = 0; r < m; r++) {
5252
matrix[r][0] = 0;
5353
}

typescript/Hash Maps and Sets/zero_striping_hash_sets.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
function zero_striping_hash_sets(matrix: number[][]): void {
1+
function zeroStripingHashSets(matrix: number[][]): void {
22
if (!matrix || !matrix[0])
33
return;
44
const m = matrix.length, n = matrix[0].length;
5-
const zero_rows = new Set<number>(), zero_cols = new Set<number>();
5+
const zeroRows = new Set<number>(), zeroCols = new Set<number>();
66
// Pass 1: Traverse through the matrix to identify the rows and
77
// columns containing zeros and store their indexes in the
88
// appropriate hash sets.
99
for (let r = 0; r < m; r++) {
1010
for (let c = 0; c < n; c++) {
1111
if (matrix[r][c] === 0) {
12-
zero_rows.add(r);
13-
zero_cols.add(c);
12+
zeroRows.add(r);
13+
zeroCols.add(c);
1414
}
1515
}
1616
}
1717
// Pass 2: Set any cell in the matrix to zero if its row index is
18-
// in 'zero_rows' or its column index is in 'zero_cols'.
18+
// in 'zeroRows' or its column index is in 'zeroCols'.
1919
for (let r = 0; r < m; r++) {
2020
for (let c = 0; c < n; c++) {
21-
if (zero_rows.has(r) || zero_cols.has(c)) {
21+
if (zeroRows.has(r) || zeroCols.has(c)) {
2222
matrix[r][c] = 0;
2323
}
2424
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
function is_palindrome_valid(s: string): boolean {
2-
let left = 0, right = s.length - 1
1+
function isPalindromeValid(s: string): boolean {
2+
let left = 0, right = s.length - 1;
33
while (left < right) {
44
// Skip non-alphanumeric characters from the left.
5-
while (left < right && !isalnum(s[left]))
6-
left++
5+
while (left < right && !isAlphaNumeric(s[left]))
6+
left++;
77
// Skip non-alphanumeric characters from the right.
8-
while (left < right && !isalnum(s[right]))
9-
right--
8+
while (left < right && !isAlphaNumeric(s[right]))
9+
right--;
1010
// If the characters at the left and right pointers don't
1111
// match, the string is not a palindrome.
1212
if (s[left] !== s[right])
13-
return false
14-
left++
15-
right--
13+
return false;
14+
left++;
15+
right--;
1616
}
17-
return true
17+
return true;
1818
}
1919

20-
const isalnum = (c: string): boolean => /^[a-z0-9]+$/i.test(c);
20+
const isAlphaNumeric = (c: string): boolean => /^[a-z0-9]+$/i.test(c);
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
function largest_container(heights: number[]): number {
2-
let max_water = 0
3-
let left = 0, right = heights.length - 1
1+
function largestContainer(heights: number[]): number {
2+
let maxWater = 0;
3+
let left = 0, right = heights.length - 1;
44
while (left < right) {
55
// Calculate the water contained between the current pair of
66
// lines.
7-
let water = Math.min(heights[left], heights[right]) * (right - left)
8-
max_water = Math.max(max_water, water)
7+
let water = Math.min(heights[left], heights[right]) * (right - left);
8+
maxWater = Math.max(maxWater, water);
99
// Move the pointers inward, always moving the pointer at the
1010
// shorter line. If both lines have the same height, move both
1111
// pointers inward.
1212
if (heights[left] < heights[right])
13-
left++
13+
left++;
1414
else if (heights[left] > heights[right])
15-
right--
15+
right--;
1616
else
17-
left++, right--
17+
left++, right--;
1818
}
19-
return max_water
19+
return maxWater;
2020
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
function largest_container_brute_force(heights: number[]): number {
2-
let n = heights.length
3-
let max_water = 0
1+
function largestContainerBruteForce(heights: number[]): number {
2+
let n = heights.length;
3+
let maxWater = 0;
44
// Find the maximum amount of water stored between all pairs of
55
// lines.
66
for (let i = 0; i < n - 1; i++) {
77
for (let j = i + 1; j < n; j++) {
8-
let water = Math.min(heights[i], heights[j]) * (j - i)
9-
max_water = Math.max(max_water, water)
8+
let water = Math.min(heights[i], heights[j]) * (j - i);
9+
maxWater = Math.max(maxWater, water);
1010
}
1111
}
12-
return max_water
12+
return maxWater;
1313
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function next_lexicographical_sequence(s: string): string {
2-
let letters = [...s];
1+
function nextLexicographicalSequence(s: string): string {
2+
let letters: string[] = [...s];
33
// Locate the pivot, which is the first character from the right that breaks
44
// non-increasing order. Start searching from the second-to-last position.
55
let pivot = letters.length - 2;
@@ -10,14 +10,14 @@ function next_lexicographical_sequence(s: string): string {
1010
if (pivot === -1)
1111
return letters.reverse().join('');
1212
// Find the rightmost successor to the pivot.
13-
let rightmost_successor = letters.length - 1;
14-
while (letters[rightmost_successor] <= letters[pivot])
15-
rightmost_successor--;
13+
let rightmostSuccessor = letters.length - 1;
14+
while (letters[rightmostSuccessor] <= letters[pivot])
15+
rightmostSuccessor--;
1616
// Swap the rightmost successor with the pivot to increase the lexicographical
1717
// order of the suffix.
18-
[letters[pivot], letters[rightmost_successor]] = [letters[rightmost_successor], letters[pivot]];
18+
[letters[pivot], letters[rightmostSuccessor]] = [letters[rightmostSuccessor], letters[pivot]];
1919
// Reverse the suffix after the pivot to minimize its permutation.
2020
const suffix = letters.slice(pivot + 1).reverse();
2121
letters.splice(pivot + 1, suffix.length, ...suffix);
2222
return letters.join('');
23-
}
23+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
function pair_sum_sorted(nums: number[], target: number): number[] {
2-
let left = 0, right = nums.length - 1
1+
function pairSumSorted(nums: number[], target: number): number[] {
2+
let left = 0, right = nums.length - 1;
33
while (left < right) {
4-
let sum = nums[left] + nums[right]
4+
let sum = nums[left] + nums[right];
55
// If the sum is smaller, increment the left pointer, aiming
66
// to increase the sum toward the target value.
77
if (sum < target)
8-
left++
8+
left++;
99
// If the sum is larger, decrement the right pointer, aiming
1010
// to decrease the sum toward the target value.
1111
else if (sum > target)
12-
right--
12+
right--;
1313
// If the target pair is found, return its indexes.
1414
else
15-
return [left, right]
15+
return [left, right];
1616
}
17-
return []
17+
return [];
1818
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
function pair_sum_sorted_brute_force(nums: number[], target: number): number[] {
1+
function pairSumSortedBruteForce(nums: number[], target: number): number[] {
22
const n = nums.length;
33
for (let i = 0; i < n; i++) {
44
for (let j = i + 1; j < n; j++) {
55
if (nums[i] + nums[j] === target)
6-
return [i, j]
6+
return [i, j];
77
}
88
}
9-
return []
9+
return [];
1010
}

0 commit comments

Comments
 (0)