Skip to content

TypeScript: Chapters 1-2 naming aligned with language standard #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions typescript/Hash Maps and Sets/geometric_sequence_triplets.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 6 additions & 6 deletions typescript/Hash Maps and Sets/pair_sum_unsorted.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}
14 changes: 7 additions & 7 deletions typescript/Hash Maps and Sets/pair_sum_unsorted_two_pass.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}
20 changes: 10 additions & 10 deletions typescript/Hash Maps and Sets/verify_sudoku_board.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
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<number>[] = Array.from({ length: 9 }, () => new Set());
const column_sets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
const subgrid_sets: Set<number>[][] = Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => new Set()));
const rowSets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
const columnSets: Set<number>[] = Array.from({ length: 9 }, () => new Set());
const subgridSets: Set<number>[][] = 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];
if (num === 0)
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;
Expand Down
14 changes: 7 additions & 7 deletions typescript/Hash Maps and Sets/zero_striping.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions typescript/Hash Maps and Sets/zero_striping_hash_sets.ts
Original file line number Diff line number Diff line change
@@ -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<number>(), zero_cols = new Set<number>();
const zeroRows = new Set<number>(), zeroCols = new Set<number>();
// 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;
}
}
Expand Down
22 changes: 11 additions & 11 deletions typescript/Two Pointers/is_palindrome_valid.ts
Original file line number Diff line number Diff line change
@@ -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);
18 changes: 9 additions & 9 deletions typescript/Two Pointers/largest_container.ts
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 6 additions & 6 deletions typescript/Two Pointers/largest_container_brute_force.ts
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 7 additions & 7 deletions typescript/Two Pointers/next_lexicographical_sequence.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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('');
}
}
14 changes: 7 additions & 7 deletions typescript/Two Pointers/pair_sum_sorted.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}
6 changes: 3 additions & 3 deletions typescript/Two Pointers/pair_sum_sorted_brute_force.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}
Loading