1
- const { StdOut } = require ( '../../../libs' )
1
+ const { StdOut } = require ( '../../../../../src/ libs' )
2
2
3
3
/**
4
4
* Exercise 1.1.14
5
5
*/
6
6
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
+
7
43
/**
8
44
* Calculates the largest integer
9
45
* 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>
12
47
* @param {number } n Integer number
13
48
* @returns {number } Integer not larger than `lg_2(n)`
14
49
*/
@@ -27,8 +62,7 @@ class Exercise {
27
62
28
63
/**
29
64
* Alternative to `lg`.
30
- * @desc
31
- * <em>NOTE: I was curious about other implementations.</em>
65
+ * @note I was curious about other implementations.</em>
32
66
* @see {@link https://stackoverflow.com/questions/53972220/calculating-the-largest-int-less-than-the-base-2-log-of-n }
33
67
* @param {number } n Integer number
34
68
* @returns {number } Integer not larger than `lg_2(n)`
@@ -45,57 +79,20 @@ class Exercise {
45
79
46
80
/**
47
81
* `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>
51
83
* I'm doing it for testing purposes only.
52
- * </em>
53
84
* @param {number } n Integer number
54
85
* @returns {number } Integer not larger than `lg_2(n)`
55
86
*/
56
87
static lgMath ( n ) {
57
88
return Math . floor ( Math . log2 ( n ) )
58
89
}
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
- }
95
90
}
96
91
92
+ // Exports
93
+ // ==============================
97
94
module . exports = Exercise
98
95
99
- // Execution
96
+ // Bash Execution
100
97
// ==============================
101
98
if ( require . main === module ) Exercise . solution ( process . argv . slice ( 2 ) )
0 commit comments