Skip to content

Commit d084001

Browse files
author
Isaac Ramírez
authored
Merge pull request #3 from iramirezc/chapter-1
Chapter 1: Fundamentals
2 parents d33020f + a2d33b9 commit d084001

File tree

81 files changed

+5004
-138
lines changed

Some content is hidden

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

81 files changed

+5004
-138
lines changed

README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Personal notes and JavaScript code for the book Algorithms (4th edition) by Robe
44

55
## Chapters' Notes
66

7-
* [Chapter 1. Fundamentals](/docs/chapter-1.md)
7+
* [Chapter 1. Fundamentals](/docs/chapter-1.notes.md)
88
* Chapter 2. Sorting
99
* Chapter 3. Searching
1010
* Chapter 4. Graphs
@@ -14,11 +14,66 @@ Personal notes and JavaScript code for the book Algorithms (4th edition) by Robe
1414
## Standard Libraries for JavaScript
1515

1616
* [In](/src/libs/in/in.js)
17-
* StdDraw
17+
* StdDraw (*not implemented*)
1818
* [StdIn](/src/libs/std-in/std-in.js)
1919
* [StdOut](/src/libs/std-out/std-out.js)
2020
* [StdRandom](/src/libs/std-random/std-random.js)
2121

2222
## Benchmarks
2323

24-
* [linear-vs-binary-search](/src/benchmarks/searching/linear-vs-binary-search.js)
24+
* [Linear Search vs. Binary Search](/src/benchmarks/searching/linear-vs-binary-search.js)
25+
26+
## Algorithms & Data Structures
27+
28+
_For a list similar to the book site see this [algorithms list](/docs/algorithms-list.md)._
29+
30+
### Chapter 1. Fundamentals
31+
32+
**Algorithms:**
33+
34+
* [Euclid's - p. 4](/src/algorithms/euclidean/euclidean.js)
35+
* [BinarySearch - p. 47](/src/algorithms/binary-search/binary-search.js)
36+
* [LinearSearch - p. 48](/src/algorithms/linear-search/linear-search.js)
37+
* [Dijkstra's Two-Stack: Expression Evaluation - p. 129](/src/algorithms/dijkstra-two-stack/dijkstra-two-stack.js)
38+
* [ThreeSum - p. 173](/src/algorithms/three-sum/three-sum.js)
39+
* [TwoSumFast - p. 189](/src/algorithms/two-sum-fast/two-sum-fast.js)
40+
* [ThreeSumFast - p. 190](/src/algorithms/three-sum-fast/three-sum-fast.js)
41+
* [QuickFindUF - p. 221](/src/algorithms/union-find/union-find.js)
42+
* [QuickUnionUF - p. 224](/src/algorithms/union-find-quick/union-find-quick.js)
43+
* [WeightedQuickUnionUF - p. 228](/src/algorithms/union-find-weighted/union-find-weighted.js)
44+
45+
**ADTs:**
46+
47+
* [BasicDate - p. 79](/src/adts/basic-date/basic-date.js)
48+
* [SmallDate - p. 79](/src/adts/small-date/small-date.js)
49+
* [Transaction - p. 79](/src/adts/transaction/transaction.js)
50+
* [Counter - p. 85](/src/adts/counter/counter.js)
51+
* [Accumulator - p. 93](/src/adts/accumulator/accumulator.js)
52+
* [FixedCapacityStack - p. 135](/src/adts/fixed-capacity-stack/fixed-capacity-stack.js)
53+
* [ResizingArrayStack - p. 141](/src/adts/resizable-array-stack/resizable-array-stack.js)
54+
* [Node - p. 142](/src/adts/node/node.js)
55+
* [Stack - p. 149](/src/adts/stack/stack.js)
56+
* [Queue - p. 151](/src/adts/queue/queue.js)
57+
* [Bag - p. 155](/src/adts/bag/bag.js)
58+
* [StopWatch - p. 175](/src/adts/stop-watch/stop-watch.js)
59+
60+
### Chapter 2. Sorting
61+
62+
**Algorithms:**
63+
64+
> TODO
65+
66+
**ADTs:**
67+
68+
> TODO
69+
70+
## Exercise Solutions
71+
72+
:warning: _Do **NOT** copy any of these solutions. These are for my reference only. You should have attempted to solve any of these exercises by your own after at least a few days before even trying to look for the solution. Finally, my solutions can be wrong too._
73+
74+
* [Chapter 1. Fundamentals](/src/exercises/index.md)
75+
* Chapter 2. Sorting
76+
* Chapter 3. Searching
77+
* Chapter 4. Graphs
78+
* Chapter 5. Strings
79+
* Chapter 6. Context

docs/algorithms-list.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Algorithms (4th Edition)
2+
3+
## List of Algorithms and Abstract Data Types
4+
5+
This is the extracted list from the book site [Java Algorithms & Clients](https://algs4.cs.princeton.edu/code/).
6+
7+
Every algorithm or ADT has a link pointing to the original Java implementation and my JavaScript implementation.
8+
9+
### Chapter 1. Fundamentals
10+
11+
* Binary Search | [js](/src/algorithms/binary-search/binary-search.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/BinarySearch.java.html) |
12+
* RandomSeq | [js](/src/examples/test-clients/random-seq.client.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/RandomSeq.java.html) |
13+
* Average | [js](/src/examples/test-clients/average.client.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Average.java.html) |
14+
* Cat | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Cat.java.html) |
15+
* Knuth | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Knuth.java.html) |
16+
* Counter | [js](/src/adts/counter/counter.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Counter.java.html) |
17+
* StaticSETofInts | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/StaticSETofInts.java.html) |
18+
* AllowList | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Allowlist.java.html) |
19+
* Vector | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Vector.java.html) |
20+
* Date | [js](/src/adts/basic-date/basic-date.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Date.java.html) |
21+
* Transaction | [js](/src/adts/transaction/transaction.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Transaction.java.html) |
22+
* Point2D | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Point2D.java.html) |
23+
* RectHV | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/RectHV.java.html) |
24+
* Interval1D | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Interval1D.java.html) |
25+
* Interval2D | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Interval2D.java.html) |
26+
* Accumulator | [js](/src/adts/accumulator/accumulator.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Accumulator.java.html) |
27+
* ResizingArrayStack | [js](/src/adts/resizable-array-stack/resizable-array-stack.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/ResizingArrayStack.java.html) |
28+
* LinkedStack | [js](/src/adts/stack/stack.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/LinkedStack.java.html) |
29+
* Stack | [js](/src/adts/stack/stack.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Stack.java.html) |
30+
* ResizingArrayQueue | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/ResizingArrayQueue.java.html) |
31+
* LinkedQueue | [js](/src/adts/queue/queue.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/LinkedQueue.java.html) |
32+
* Queue | [js](/src/adts/queue/queue.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Queue.java.html) |
33+
* ResizingArrayBag | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/ResizingArrayBag.java.html) |
34+
* LinkedBag | [js](/src/adts/bag/bag.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/LinkedBag.java.html) |
35+
* Bag | [js](/src/adts/bag/bag.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Bag.java.html) |
36+
* Stopwatch | [js](/src/adts/stop-watch/stop-watch.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Stopwatch.java.html) |
37+
* StopwatchCPU | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/StopwatchCPU.java.html) |
38+
* LinearRegression | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/LinearRegression.java.html) |
39+
* ThreeSum | [js](/src/algorithms/three-sum/three-sum.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/ThreeSum.java.html) |
40+
* ThreeSumFast | [js](/src/algorithms/three-sum-fast/three-sum-fast.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/ThreeSumFast.java.html) |
41+
* DoublingTest | [js](/src/examples/experiments/doubling-test.experiment.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/DoublingTest.java.html) |
42+
* DoublingRatio | [js](/src/examples/experiments/doubling-ratio.experiment.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/DoublingRatio.java.html) |
43+
* QuickFindUF | [js](/src/algorithms/union-find/union-find.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/QuickFindUF.java.html) |
44+
* QuickUnionUF | [js](/src/algorithms/union-find-quick/union-find-quick.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/QuickUnionUF.java.html) |
45+
* WeightedQuickUnionUF | [js](/src/algorithms/union-find-weighted/union-find-weighted.js) | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/WeightedQuickUnionUF.java.html) |
46+
* UF | js | [java](https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/UF.java.html) |

docs/chapter-1.md renamed to docs/chapter-1.notes.md

+21-14
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
## Chapter 1. Fundamentals
44

5-
### Algorithms
6-
7-
* [Euclid's - Algorithm - p. 4](/src/algorithms/euclidean/euclidean.js)
8-
* [BinarySearch - Algorithm - p.47](/src/algorithms/binary-search/binary-search.js)
9-
* [LinearSearch - Algorithm - p.48](/src/algorithms/linear-search/linear-search.js)
10-
11-
### Examples & Code Snippets
12-
13-
* [StdRandom - TestClient - p. 32](/src/examples/test-clients/std-random.client.js)
14-
* [RandomSeq - TestClient - p. 37](/src/examples/test-clients/random-seq.client.js)
15-
* [Average - TestClient - p. 39](/src/examples/test-clients/average.client.js)
16-
* [BinarySearch - TestClient - p.47](/src/examples/test-clients/binary-search.client.js)
17-
* [LinearSearch - TestClient - p.48](/src/examples/test-clients/linear-search.client.js)
18-
195
### Key Concepts
206

217
#### Algorithm
@@ -85,3 +71,24 @@
8571
#### Data Abstraction
8672

8773
> Extends encapsulation and reuse to allow defining non-primitive data types, supporting Object-Oriented programming.
74+
75+
#### Data Structures
76+
77+
> Two ways of representing object collections:
78+
>
79+
> * Arrays aka *Sequential Allocation*
80+
> * Linked Lists aka *Linked Allocation*
81+
82+
#### The Scientific Method
83+
84+
> * Observe
85+
> * Hypothesize
86+
> * Predict
87+
> * Verify
88+
> * Validate
89+
90+
#### Analysis of Algorithms
91+
92+
> _Premature optimization is the root of all evil_ - C. A. R. Hoare
93+
>
94+
> _Premature optimization is the root of all evil (or at least most of it) in programming_ - Knuth
File renamed without changes.

docs/templates.md renamed to docs/samples/templates.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Client.main(process.argv.slice(2))
7171

7272
### Exercise Answer
7373

74-
### [Exercise 1.1.x](./ex-1.1.x.js)
74+
### [Exercise 1.1.x](./samples/ex-1.1.x.js)
7575

7676
:pencil2: **Answer**
7777

File renamed without changes.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"describe",
2222
"it",
2323
"expect",
24+
"beforeAll",
2425
"beforeEach",
2526
"afterEach",
2627
"TestCase"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* ArrayIterator
3+
* @classdesc Iterable interface for Array elements
4+
* @abstract
5+
* @see p. 135 (This is my custom implementation), 141
6+
*/
7+
class ArrayIterator {
8+
constructor (array, n) {
9+
this._current = 0
10+
this._a = [...array]
11+
this._n = n
12+
13+
Object.seal(this)
14+
}
15+
16+
/**
17+
* Returns if there are
18+
* more elements in the Iterator.
19+
*/
20+
hasNext () {
21+
return this._current < this._n
22+
}
23+
24+
/**
25+
* Is best not to modify Data Structures
26+
* when iterating.
27+
* @see p. 139
28+
*/
29+
remove () {
30+
/* do nothing */
31+
}
32+
33+
/**
34+
* Returns an object implementing the Iterator protocol.
35+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
36+
* @returns {object} iterator protocol
37+
*/
38+
next () {
39+
if (this.hasNext()) {
40+
const item = this._a[this._current++]
41+
42+
return {
43+
value: item,
44+
done: false
45+
}
46+
} else {
47+
return {
48+
done: true
49+
}
50+
}
51+
}
52+
}
53+
54+
module.exports = ArrayIterator
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const ArrayIterator = require('./array-iterator')
2+
3+
describe('Unit Tests: Abstract ArrayIterator Interface', () => {
4+
describe('instance', () => {
5+
beforeEach(() => {
6+
this.array = [1, 2]
7+
this.n = 2
8+
this.iterator = new ArrayIterator(this.array, this.n)
9+
})
10+
11+
describe('when initialized:', () => {
12+
it('should have a prop `_current` equal to `0`', () => {
13+
expect(this.iterator._current).toBe(0)
14+
})
15+
16+
it('should have a prop `_a` equal to the array provided', () => {
17+
expect(this.iterator._a).toEqual(this.array)
18+
expect(this.iterator._a).not.toBe(this.array)
19+
})
20+
21+
it('should have a prop `_n` equal to the n provided', () => {
22+
expect(this.iterator._n).toEqual(this.n)
23+
})
24+
25+
it('should not be extensible', () => {
26+
const expectedProps = ['_current', '_a', '_n']
27+
28+
this.iterator.newProp = null
29+
30+
const actualProps = Object.getOwnPropertyNames(this.iterator)
31+
expect(actualProps).toEqual(expectedProps)
32+
expect(this.iterator.newProp).toBeUndefined()
33+
})
34+
})
35+
36+
describe('hasNext() method', () => {
37+
it('should return false when _current is 0', () => {
38+
this.iterator = new ArrayIterator([], 0)
39+
40+
const result = this.iterator.hasNext()
41+
42+
expect(result).toBeFalse()
43+
})
44+
45+
it('should return true when there is an element', () => {
46+
const result = this.iterator.hasNext()
47+
48+
expect(result).toBeTrue()
49+
})
50+
51+
it('should return false when there are no more elements to iterate', () => {
52+
this.iterator._current = this.iterator._n
53+
54+
const result = this.iterator.hasNext()
55+
56+
expect(result).toBeFalse()
57+
})
58+
})
59+
60+
describe('next() method', () => {
61+
it('should return an object with the item value of the _current element', () => {
62+
const result = this.iterator.next()
63+
64+
expect(result).toEqual({
65+
value: this.array[0],
66+
done: false
67+
})
68+
})
69+
70+
it('should increment _current on each call to next()', () => {
71+
this.iterator.next()
72+
73+
expect(this.iterator._current).toBe(1)
74+
75+
this.iterator.next()
76+
77+
expect(this.iterator._current).toBe(2)
78+
})
79+
80+
it('should return an object with `done` false when there are no more elements', () => {
81+
this.iterator.next()
82+
this.iterator.next()
83+
84+
expect(this.iterator.next()).toEqual({ done: true })
85+
})
86+
})
87+
})
88+
})

src/abstracts/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
ArrayIterator: require('./array-iterator/array-iterator'),
3+
Iterator: require('./iterator/iterator'),
4+
ReversedArrayIterator: require('./reversed-array-iterator/reversed-array-iterator')
5+
}

src/abstracts/iterator/iterator.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Iterator
3+
* @classdesc Iterable interface for Node elements
4+
* @abstract
5+
* @see p. 155
6+
*/
7+
class Iterator {
8+
constructor (element = null) {
9+
this._current = element
10+
11+
Object.seal(this)
12+
}
13+
14+
/**
15+
* Returns if there are
16+
* more elements in the Iterator.
17+
*/
18+
hasNext () {
19+
return this._current !== null
20+
}
21+
22+
/**
23+
* Is best not to modify Data Structures
24+
* when iterating.
25+
* @see p. 139
26+
*/
27+
remove () {
28+
/* do nothing */
29+
}
30+
31+
/**
32+
* Returns an object implementing the Iterator protocol.
33+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
34+
* @returns {object} iterator protocol
35+
*/
36+
next () {
37+
if (this.hasNext()) {
38+
const item = this._current._item
39+
40+
this._current = this._current._next
41+
42+
return {
43+
value: item,
44+
done: false
45+
}
46+
} else {
47+
return {
48+
done: true
49+
}
50+
}
51+
}
52+
}
53+
54+
module.exports = Iterator

0 commit comments

Comments
 (0)