@@ -13,20 +13,46 @@ util.inspect.defaultOptions.depth = null; // show full objects
1313import console from 'console' ;
1414const jestConsole = console ;
1515
16- // beforeEach(() => {
17- // global.console = console;
18- // console.log(style.color(255,0,255),'▷',style.reset,style.color(39),expect.getState().currentTestName,style.reset);
19- // // console.log(expect.getState());
20- // });
16+ beforeEach ( ( ) => {
17+ global . console = console ;
18+ console . log ( style . color ( 255 , 0 , 255 ) , '▷' , style . reset , style . color ( 39 ) , expect . getState ( ) . currentTestName , style . reset ) ;
19+ // console.log(expect.getState());
20+ } ) ;
21+
22+ afterEach ( ( ) => {
23+ global . console = jestConsole ;
24+ console . log ( style . color ( 99 ) , style . hr . double , style . reset ) ;
25+ } ) ;
2126
22- // afterEach(( ) => {
23- // global. console = jestConsole ;
24- // console.log(style.color(99), style.hr.double, style.reset );
25- // }) ;
27+ // const logItDescription = (description, callback ) => {
28+ // console.log(style.green, ' ☞ ', style.reset,description) ;
29+ // it(description, callback );
30+ // };
2631
2732const logItDescription = ( description , callback ) => {
28- console . log ( style . green , ' ☞ ' , style . reset , description ) ;
29- it ( description , callback ) ;
33+ it ( description , async ( ) => {
34+ // Array to store captured log messages for this specific test
35+ const capturedLogs = [ ] ;
36+
37+ // Spy on console.log and push messages to our array
38+ const logSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ( ...args ) => {
39+ capturedLogs . push ( args ) ;
40+ } ) ;
41+
42+ // Capture console.log output during the test's execution
43+ try {
44+ await callback ( ) ; // Assuming the callback might be async
45+ } finally {
46+ // Restore the original console.log behavior
47+ logSpy . mockRestore ( ) ;
48+
49+ // Print the custom description and then the captured logs
50+ // console.log(style.green, ' ☞ ', style.reset, description);
51+ capturedLogs . forEach ( logArgs => {
52+ console . log ( ' ↳ ' , ...logArgs ) ;
53+ } ) ;
54+ }
55+ } ) ;
3056} ;
3157
3258const logCategory = ( description , callback ) => {
@@ -39,17 +65,44 @@ const logDescribe = (description, callback) => {
3965 describe ( description , callback ) ;
4066} ;
4167
42- logCategory ( `${ style . bold } binarySearch${ style . reset } ` , ( ) => {
68+ describe ( `${ style . bold } binarySearch${ style . reset } ` , ( ) => {
69+ console . log ( `
70+ ${ style . h1 ( '# Binary Search' ) }
71+ Find an element by comparing the element to the middle element of a sorted
72+ array and--if not found--splitting, discarding sub-arrays until it is.
73+
74+ ${ style . h2 ( '## Time Complexity' ) }
75+ Ο(log N) Ω(1) Θ(log N)
76+
77+ > Because we are cutting the list in half at each interval,
78+ the time complexity is \`O(log N)\`.
79+ > A sorted list of \`64\` elements will take at most \`log2(64) = 6\` comparisons.
80+ > \`Ω(1)\` because the target may be the middle element of the array
81+
82+ ${ style . h2 ( '## Notes' ) }
83+ - For binary search to know which half of the data to discard after each split,
84+ the data source must always be sorted.
85+
86+ ${ style . h2 ( '## Steps' ) }
87+ 1. Check the middle value of the dataset.
88+ - If this value matches our target we can return the index
89+
90+ 2. If the middle value is less than our target
91+ - Start at step 1 using the right half of the list.
92+
93+ 3. If the middle value is greater than our target
94+ - Start at step 1 using the left half of the list.
95+ ` ) ;
4396
4497 // BINARY SEARCH (ITERATIVE)
45- logDescribe ( `${ style . underline } ${ style . italic } binarySearch (Iterative)\n${ style . reset } ` , ( ) => {
98+ describe ( `${ style . underline } ${ style . italic } (Iterative)\n${ style . reset } ` , ( ) => {
4699
47100 logItDescription ( 'should find index of target AT END of a 4 element searchable' , ( ) => {
48101 const searchable = [ 2 , 4 , 6 , 8 ] ;
49102 const targetVal = 8 ;
50103 const expectedIdx = searchable . indexOf ( targetVal ) ;
51104 const receivedIdx = binarySearchIterative ( searchable , targetVal ) ;
52- // console.log('expectedIdx', expectedIdx, 'receivedIdx', receivedIdx);
105+ console . log ( 'expectedIdx' , expectedIdx , 'receivedIdx' , receivedIdx ) ;
53106 assert . equal ( receivedIdx , expectedIdx ) ;
54107 } ) ;
55108
@@ -137,7 +190,7 @@ logCategory(`${style.bold}binarySearch${style.reset}`, () => {
137190 } ) ;
138191
139192 // BINARY SEARCH (RECURSIVE)
140- logDescribe ( `${ style . underline } ${ style . italic } binarySearch (Recursive)\n${ style . reset } ` , ( ) => {
193+ describe ( `${ style . underline } ${ style . italic } binarySearch (Recursive)\n${ style . reset } ` , ( ) => {
141194
142195 logItDescription ( 'should find index of target AT END of a 4 element searchable' , ( ) => {
143196 const searchable = [ 2 , 4 , 6 , 8 ] ;
0 commit comments