Skip to content

Commit 72be019

Browse files
committed
added dbg for flat()
1 parent a569aa3 commit 72be019

File tree

5 files changed

+122
-12
lines changed

5 files changed

+122
-12
lines changed

src/array/search-in-rotated-sorted-array.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function search(nums: number[], target: number): number {
77
return mid;
88
}
99
if(nums[left] <= num){
10-
if(nums[left] <= target && target <= num){
10+
if(target >= nums[left] && target <= num){
1111
right = mid-1;
1212
} else{
1313
left = mid+1;
@@ -21,4 +21,37 @@ function search(nums: number[], target: number): number {
2121
}
2222
}
2323
return -1;
24-
};
24+
};
25+
26+
export function searchDBG() {
27+
const tests = [
28+
{
29+
input: { nums: [4,5,6,7,0,1,2], target: 0 },
30+
result: 4
31+
},
32+
{
33+
input: { nums: [4,5,6,7,0,1,2], target: 3 },
34+
result: -1
35+
},
36+
{
37+
input: { nums: [1], target: 0 },
38+
result: -1
39+
},
40+
{
41+
input: { nums: [1], target: 1 },
42+
result: 0
43+
}
44+
];
45+
46+
tests.forEach((test, index) => {
47+
const result = search(test.input.nums, test.input.target);
48+
if (result === test.result) {
49+
console.log(`${index} success`);
50+
} else {
51+
console.log(`${index} fail`);
52+
console.log(`expected ${test.result}`);
53+
console.log(`got ${result}`);
54+
}
55+
});
56+
}
57+

src/js_core/flatten.ts

+30
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,33 @@ function flat(arr: any[], depth: number): any[] {
1313
flattenHelper(arr, 0);
1414
return result;
1515
}
16+
17+
export function flatDBG() {
18+
const tests = [
19+
{
20+
input: { arr: [1, [2, [3, [4]], 5]], n: 1 },
21+
result: [1, 2, [3, [4]], 5] // Сплющено до глубины 1
22+
},
23+
{
24+
input: { arr: [1, [2, [3, [4]], 5]], n: 2 },
25+
result: [1, 2, 3, [4], 5] // Сплющено до глубины 2
26+
},
27+
{
28+
input: { arr: [1, [2, [3, [4]], 5]], n: 3 },
29+
result: [1, 2, 3, 4, 5] // Сплющено до глубины 3
30+
}
31+
];
32+
33+
tests.forEach((test, index) => {
34+
const result = flat(test.input.arr, test.input.n);
35+
const success = JSON.stringify(result) === JSON.stringify(test.result);
36+
if (success) {
37+
console.log(`${index} success`);
38+
} else {
39+
console.log(`${index} fail`);
40+
console.log(`expected ${test.result}`);
41+
console.log(`got ${result}`);
42+
}
43+
});
44+
}
45+

src/main.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { majorityElementDBG } from "./array/majority-element";
2-
import { maxProfitDBG } from "./array/max-profit";
3-
import { productExceptSelfDBG } from "./array/product-except-self";
4-
import { printTree, TreeNode } from "./bst";
5-
import { lowestCommonAncestorDBG } from "./bst/problems/lowest-common-ancestor";
6-
import { HitCounterDBG } from "./scratch_structure/hit-counter";
7-
import { longestPalindromeDBG } from "./string/longest-palindrome";
1+
import { searchDBG } from "./array/search-in-rotated-sorted-array";
2+
import { flatDBG } from "./js_core/flatten";
3+
import { findAnagramsDBG } from "./string/find-anagrams";
84

9-
maxProfitDBG()
5+
flatDBG()

src/recursion/generate-parenthesis.ts

+25
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,29 @@ function generateParenthesis(n: number): string[] {
1616

1717
backtrack('', 0, 0);
1818
return result;
19+
}
20+
21+
export function generateParenthesisDBG(){
22+
const tests = [
23+
{
24+
input: 3,
25+
result: ["((()))", "(()())", "(())()", "()(())", "()()()"]
26+
},
27+
{
28+
input: 1,
29+
result: ["()"]
30+
}
31+
];
32+
33+
tests.forEach((test, index) => {
34+
const result = generateParenthesis(test.input);
35+
const success = JSON.stringify(result.sort()) === JSON.stringify(test.result.sort());
36+
if (success) {
37+
console.log(`${index} success`);
38+
} else {
39+
console.log(`${index} fail`);
40+
console.log(`expected ${test.result}`);
41+
console.log(`got ${result}`);
42+
}
43+
});
1944
}

src/string/find-anagrams.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ function findAnagrams(s: string, p: string): number[] {
2121
// Скользящее окно по строке s
2222
for (let i = p.length; i < s.length; i++) {
2323
// Добавляем символ в окно
24-
sCount[s.charCodeAt(i) - aCharCode]++;
24+
sCount[s.charCodeAt(i) - aCharCode]++;
2525
// Убираем символ за пределами окна
26-
sCount[s.charCodeAt(i - p.length) - aCharCode]--;
26+
sCount[s.charCodeAt(i - p.length) - aCharCode]--;
2727

2828
// Проверяем текущее окно
2929
if (JSON.stringify(pCount) === JSON.stringify(sCount)) {
@@ -33,3 +33,29 @@ function findAnagrams(s: string, p: string): number[] {
3333

3434
return result;
3535
}
36+
37+
export function findAnagramsDBG() {
38+
const tests = [
39+
{
40+
input: { s: "cbaebabacd", p: "abc" },
41+
result: [0, 6] // Анаграммы "abc" начинаются с индексов 0 и 6
42+
},
43+
{
44+
input: { s: "abab", p: "ab" },
45+
result: [0, 1, 2] // Анаграммы "ab" начинаются с индексов 0, 1, и 2
46+
}
47+
];
48+
49+
tests.forEach((test, index) => {
50+
const result = findAnagrams(test.input.s, test.input.p);
51+
const success = JSON.stringify(result) === JSON.stringify(test.result);
52+
if (success) {
53+
console.log(`${index} success`);
54+
} else {
55+
console.log(`${index} fail`);
56+
console.log(`expected ${test.result}`);
57+
console.log(`got ${result}`);
58+
}
59+
});
60+
}
61+

0 commit comments

Comments
 (0)