Skip to content

Commit cc797b1

Browse files
author
Isaac Ramirez
committed
- major refactor changes:
- move exercises to a root folder - move solution method at the top - move paras after examples - change path of algs4-data - change desc for note - minor updates in comments
1 parent 0befb1d commit cc797b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+723
-658
lines changed

docs/samples/ex-x.x.x.js

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class C1S1E1 {
4141

4242
// Exports
4343
// ==============================
44-
4544
module.exports = C1S1E1
4645

4746
// Bash Execution
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
const { StdOut } = require('../../../libs')
1+
const { StdOut } = require('../../../../../src/libs')
22

33
/**
44
* Exercise 1.1.1
55
*/
66
class Exercise {
7-
static a () {
8-
return (0 + 15) / 2
9-
}
10-
11-
static b () {
12-
return 2.0e-6 * 100000000.1
13-
}
14-
15-
static c () {
16-
return true && false || true && true // eslint-disable-line
17-
}
18-
197
/**
208
* Solution Exercise 1.1.1
219
* @example
@@ -31,10 +19,24 @@ class Exercise {
3119
StdOut.println(`b) ${Exercise.b()}`)
3220
StdOut.println(`c) ${Exercise.c()}`)
3321
}
22+
23+
static a () {
24+
return (0 + 15) / 2
25+
}
26+
27+
static b () {
28+
return 2.0e-6 * 100000000.1
29+
}
30+
31+
static c () {
32+
return true && false || true && true // eslint-disable-line
33+
}
3434
}
3535

36+
// Exports
37+
// ==============================
3638
module.exports = Exercise
3739

38-
// Execution
40+
// Bash Execution
3941
// ==============================
4042
if (require.main === module) Exercise.solution()

src/exercises/chap-1/section-1/ex-1.1.10.js renamed to exercises/chapter/01/section/01/ex-1.1.10.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { StdOut } = require('../../../libs')
1+
const { StdOut } = require('../../../../../src/libs')
22

33
/**
44
* Exercise 1.1.10
@@ -23,8 +23,10 @@ class Exercise {
2323
}
2424
}
2525

26+
// Exports
27+
// ==============================
2628
module.exports = Exercise
2729

28-
// Execution
30+
// Bash Execution
2931
// ==============================
3032
if (require.main === module) Exercise.solution()

src/exercises/chap-1/section-1/ex-1.1.11.js renamed to exercises/chapter/01/section/01/ex-1.1.11.js

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
const { StdOut, StdRandom } = require('../../../libs')
1+
const { StdOut, StdRandom } = require('../../../../../src/libs')
22

33
/**
44
* Exercise 1.1.11
55
*/
66
class Exercise {
7+
/**
8+
* Solution Exercise 1.1.11
9+
* @example
10+
* ```shell
11+
* $ node ex-1.1.11.js 5 5
12+
* 1 2 3 4 5
13+
* 1 * *
14+
* 2 * *
15+
* 3 * * * *
16+
* 4 * * *
17+
* 5 * * *
18+
* ```
19+
* @param {[]} args [x, y] Cols, Rows
20+
*/
21+
static solution (args) {
22+
const x = parseInt(args[0], 10)
23+
const y = parseInt(args[1], 10)
24+
25+
const m = this.createMatrix(x, y)
26+
const sm = this.matrixToString(m)
27+
28+
StdOut.println(sm)
29+
}
30+
731
/**
832
* Returns a matrix of `x` columns
933
* and `y` rows with random boolean values.
@@ -55,34 +79,12 @@ class Exercise {
5579

5680
return cols + s
5781
}
58-
59-
/**
60-
* Solution Exercise 1.1.11
61-
* @param {[]} args [x, y] Cols, Rows
62-
* @example
63-
* ```shell
64-
* $ node ex-1.1.11.js 5 5
65-
* 1 2 3 4 5
66-
* 1 * *
67-
* 2 * *
68-
* 3 * * * *
69-
* 4 * * *
70-
* 5 * * *
71-
* ```
72-
*/
73-
static solution (args) {
74-
const x = parseInt(args[0], 10)
75-
const y = parseInt(args[1], 10)
76-
77-
const m = this.createMatrix(x, y)
78-
const sm = this.matrixToString(m)
79-
80-
StdOut.println(sm)
81-
}
8282
}
8383

84+
// Exports
85+
// ==============================
8486
module.exports = Exercise
8587

86-
// Execution
88+
// Bash Execution
8789
// ==============================
8890
if (require.main === module) Exercise.solution(process.argv.slice(2))

src/exercises/chap-1/section-1/ex-1.1.12.js renamed to exercises/chapter/01/section/01/ex-1.1.12.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { StdOut } = require('../../../libs')
1+
const { StdOut } = require('../../../../../src/libs')
22

33
/**
44
* Exercise 1.1.12
@@ -27,8 +27,10 @@ class Exercise {
2727
}
2828
}
2929

30+
// Exports
31+
// ==============================
3032
module.exports = Exercise
3133

32-
// Execution
34+
// Bash Execution
3335
// ==============================
3436
if (require.main === module) Exercise.solution()
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
const { StdOut, StdRandom, Matrix } = require('../../../libs')
2-
const { newArrayOf } = require('../../../utils')
1+
const { StdOut, StdRandom, Matrix } = require('../../../../../src/libs')
2+
const { newArrayOf } = require('../../../../../src/utils')
33

44
/**
55
* Exercise 1.1.13
66
*/
77
class Exercise {
8-
/**
9-
* Creates a matrix of `x` · `y`
10-
* of random integer numbers.
11-
* @param {number} x Number of Cols
12-
* @param {number} y Number of Rows
13-
* @returns {[[]]} A matrix of `x` columns and `y` rows
14-
*/
15-
static createMatrix (x, y) {
16-
return newArrayOf(y, () => {
17-
return newArrayOf(x, () => StdRandom.uniform(Math.max(x, y)))
18-
})
19-
}
20-
218
/**
229
* Solution Exercise 1.1.13
23-
* @param {[]} args [x, y] Cols, Rows
2410
* @example
2511
* ```shell
2612
* $ node ex-1.1.13.js 4 3
@@ -35,6 +21,7 @@ class Exercise {
3521
* 2,0,0
3622
* 3,2,1
3723
* ```
24+
* @param {[]} args [x, y] Cols, Rows
3825
*/
3926
static solution (args) {
4027
const x = parseInt(args[0], 10)
@@ -50,10 +37,25 @@ class Exercise {
5037
StdOut.println('[after]')
5138
StdOut.println(t.join('\n'))
5239
}
40+
41+
/**
42+
* Creates a matrix of `x` · `y`
43+
* of random integer numbers.
44+
* @param {number} x Number of Cols
45+
* @param {number} y Number of Rows
46+
* @returns {[[]]} A matrix of `x` columns and `y` rows
47+
*/
48+
static createMatrix (x, y) {
49+
return newArrayOf(y, () => {
50+
return newArrayOf(x, () => StdRandom.uniform(Math.max(x, y)))
51+
})
52+
}
5353
}
5454

55+
// Exports
56+
// ==============================
5557
module.exports = Exercise
5658

57-
// Execution
59+
// Bash Execution
5860
// ==============================
5961
if (require.main === module) Exercise.solution(process.argv.slice(2))
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
1-
const { StdOut } = require('../../../libs')
1+
const { StdOut } = require('../../../../../src/libs')
22

33
/**
44
* Exercise 1.1.14
55
*/
66
class Exercise {
7+
/**
8+
* Solution Exercise 1.1.14
9+
* @example <caption>my initial implementation</caption>
10+
* ```shell
11+
* $ node ex-1.1.14.js 123456789
12+
* 123456789 -> 26
13+
* ```
14+
* @example <caption>alternative implementation</caption>
15+
* ```shell
16+
* $ node ex-1.1.14.js 123456789 -v2
17+
* [v2]
18+
* 123456789 -> 26
19+
* ```
20+
* @example <caption>math implementation</caption>
21+
* ```shell
22+
* $ node ex-1.1.14.js 123456789 -math
23+
* [math]
24+
* 123456789 -> 26
25+
* ```
26+
* @param {[]} args [n, -v] Integer number, version
27+
*/
28+
static solution (args) {
29+
const n = parseInt(args[0], 10)
30+
const version = args[1]
31+
32+
if (version === '-v2') {
33+
StdOut.println('[v2]')
34+
StdOut.println('%d -> %d', n, this.lgV2(n))
35+
} else if (version === '-math') {
36+
StdOut.println('[math]')
37+
StdOut.println('%d -> %d', n, this.lgMath(n))
38+
} else {
39+
StdOut.println('%d -> %d', n, this.lg(n))
40+
}
41+
}
42+
743
/**
844
* Calculates the largest integer
945
* not larger than the `base-2` logarithm of `n`.
10-
* @desc
11-
* <em>NOTE: this is my original implementation.</em>
46+
* @note This is my original implementation.</em>
1247
* @param {number} n Integer number
1348
* @returns {number} Integer not larger than `lg_2(n)`
1449
*/
@@ -27,8 +62,7 @@ class Exercise {
2762

2863
/**
2964
* Alternative to `lg`.
30-
* @desc
31-
* <em>NOTE: I was curious about other implementations.</em>
65+
* @note I was curious about other implementations.</em>
3266
* @see {@link https://stackoverflow.com/questions/53972220/calculating-the-largest-int-less-than-the-base-2-log-of-n}
3367
* @param {number} n Integer number
3468
* @returns {number} Integer not larger than `lg_2(n)`
@@ -45,57 +79,20 @@ class Exercise {
4579

4680
/**
4781
* `lg` implementation using `Math`
48-
* @desc
49-
* <em>
50-
* NOTE: The book states not to use the `Math` library!.<br>
82+
* @note The book states not to use the `Math` library!.<br>
5183
* I'm doing it for testing purposes only.
52-
* </em>
5384
* @param {number} n Integer number
5485
* @returns {number} Integer not larger than `lg_2(n)`
5586
*/
5687
static lgMath (n) {
5788
return Math.floor(Math.log2(n))
5889
}
59-
60-
/**
61-
* Solution Exercise 1.1.14
62-
* @param {[]} args [n, -v] Integer number, version
63-
* @example <caption>my initial implementation</caption>
64-
* ```shell
65-
* $ node ex-1.1.14.js 123456789
66-
* 123456789 -> 26
67-
* ```
68-
* @example <caption>alternative implementation</caption>
69-
* ```shell
70-
* $ node ex-1.1.14.js 123456789 -v2
71-
* [v2]
72-
* 123456789 -> 26
73-
* ```
74-
* @example <caption>math implementation</caption>
75-
* ```shell
76-
* $ node ex-1.1.14.js 123456789 -math
77-
* [math]
78-
* 123456789 -> 26
79-
* ```
80-
*/
81-
static solution (args) {
82-
const n = parseInt(args[0], 10)
83-
const version = args[1]
84-
85-
if (version === '-v2') {
86-
StdOut.println('[v2]')
87-
StdOut.println('%d -> %d', n, this.lgV2(n))
88-
} else if (version === '-math') {
89-
StdOut.println('[math]')
90-
StdOut.println('%d -> %d', n, this.lgMath(n))
91-
} else {
92-
StdOut.println('%d -> %d', n, this.lg(n))
93-
}
94-
}
9590
}
9691

92+
// Exports
93+
// ==============================
9794
module.exports = Exercise
9895

99-
// Execution
96+
// Bash Execution
10097
// ==============================
10198
if (require.main === module) Exercise.solution(process.argv.slice(2))

0 commit comments

Comments
 (0)