diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-list.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-list.english.md index cdee4ae307..80396417bd 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-list.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-list.english.md @@ -11,9 +11,25 @@ An adjacency list is essentially a bulleted list where the left side is the node
Node1: Node2, Node3
Node2: Node1
Node3: Node1
Above is an undirected graph because Node1 is connected to Node2 and Node3, and that information is consistent with the connections Node2 and Node3 show. An adjacency list for a directed graph would mean each row of the list shows direction. If the above was directed, then Node2: Node1 would mean there the directed edge is pointing from Node2 towards Node1. We can represent the undirected graph above as an adjacency list by putting it within a JavaScript object. -
var undirectedG = {
Node1: ["Node2", "Node3"],
Node2: ["Node1"],
Node3: ["Node1"]
};
+ +```js +var undirectedG = { + Node1: ["Node2", "Node3"], + Node2: ["Node1"], + Node3: ["Node1"] +}; +``` + This can also be more simply represented as an array where the nodes just have numbers rather than string labels. -
var undirectedGArr = [
[1, 2], # Node1
[0], # Node2
[0] # Node3
];
+ +```js +var undirectedGArr = [ + [1, 2], // Node1 + [0], // Node2 + [0] // Node3 +]; +``` + ## Instructions diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-matrix.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-matrix.english.md index b2a83b75c5..6a21f6223d 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-matrix.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/adjacency-matrix.english.md @@ -11,10 +11,26 @@ An adjacency matrix is a two-dimensional (2D) array where each nested
1 2 3
------
1 | 0 1 1
2 | 1 0 0
3 | 1 0 0
Above is a very simple, undirected graph where you have three nodes, where the first node is connected to the second and third node. Note: The numbers to the top and left of the matrix are just labels for the nodes. Below is a JavaScript implementation of the same thing. -
var adjMat = [
[0, 1, 1],
[1, 0, 0],
[1, 0, 0]
];
+ +```js +var adjMat = [ + [0, 1, 1], + [1, 0, 0], + [1, 0, 0] +]; +``` + Unlike an adjacency list, each "row" of the matrix has to have the same number of elements as nodes in the graph. Here we have a three by three matrix, which means we have three nodes in our graph. A directed graph would look similar. Below is a graph where the first node has an edge pointing toward the second node, and then the second node has an edge pointing to the third node. -
var adjMatDirected = [
[0, 1, 0],
[0, 0, 1],
[0, 0, 0]
];
+ +```js +var adjMatDirected = [ + [0, 1, 0], + [0, 0, 1], + [0, 0, 0] +]; +``` + Graphs can also have weights on their edges. So far, we have unweighted edges where just the presence and lack of edge is binary (0 or 1). You can have different weights depending on your application. diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md index 8b4546334f..c0a6b015f3 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md @@ -8,7 +8,17 @@ challengeType: 1
In this exercise we are going to create a class named Set to emulate an abstract data structure called "set". A set is like an array, but it cannot contain duplicate values. The typical use for a set is to simply check for the presence of an item. We can see how ES6 set object works in the example below- -
const set1 = new Set([1, 2, 3, 5, 5, 2, 0]);
console.log(set1);
// output: {1, 2, 3, 5, 0}
console.log(set1.has(1));
// output: true
console.log(set1.has(6));
// output: false
+ +```js +const set1 = new Set([1, 2, 3, 5, 5, 2, 0]); +console.log(set1); +// output: {1, 2, 3, 5, 0} +console.log(set1.has(1)); +// output: true +console.log(set1.has(6)); +// output: false +``` + First, we will create an add method that adds a value to our set collection as long as the value does not already exist in the set. Then we will create a remove method that removes a value from the set collection if it already exists. And finally, we will create a size method that returns the number of elements inside the set collection. diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md index 386230baa7..ed726ad1b7 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md @@ -14,9 +14,19 @@ You can create a set with a value: You can create a set with an array: var set = new Set([1, 2, 3]); Once you have created a set, you can add the values you wish using the add method: -
var set = new Set([1, 2, 3]);
set.add([4, 5, 6]);
+ +```js +var set = new Set([1, 2, 3]); +set.add([4, 5, 6]); +``` + As a reminder, a set is a data structure that cannot contain duplicate values: -
var set = new Set([1, 2, 3, 1, 2, 3]);
// set contains [1, 2, 3] only
+ +```js +var set = new Set([1, 2, 3, 1, 2, 3]); +// set contains [1, 2, 3] only +``` +
## Instructions diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/incidence-matrix.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/incidence-matrix.english.md index 8cf310cec4..56d235207d 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/incidence-matrix.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/incidence-matrix.english.md @@ -14,9 +14,27 @@ Each column will represent a unique edge. Also, each edge connects two nodes. To Here is an example of an incidence matrix with 4 edges and 4 nodes. Remember, the columns are the edges and rows are the nodes themselves.
1 2 3 4
--------
1 | 0 1 1 1
2 | 1 1 0 0
3 | 1 0 0 1
4 | 0 0 1 0
Below is a JavaScript implementation of the same thing. -
var incMat = [
[0, 1, 1, 1],
[1, 1, 0, 0],
[1, 0, 0, 1],
[0, 0, 1, 0]
];
+ +```js +var incMat = [ + [0, 1, 1, 1], + [1, 1, 0, 0], + [1, 0, 0, 1], + [0, 0, 1, 0] +]; +``` + To make a directed graph, use -1 for an edge leaving a particular node and 1 for an edge entering a node. -
var incMatDirected = [
[ 0, -1, 1, -1],
[-1, 1, 0, 0],
[ 1, 0, 0, 1],
[ 0, 0, -1, 0]
];
+ +```js +var incMatDirected = [ + [ 0, -1, 1, -1], + [-1, 1, 0, 0], + [ 1, 0, 0, 1], + [ 0, 0, -1, 0] +]; +``` + Graphs can also have weights on their edges. So far, we have unweighted edges where just the presence and lack of edge is binary (0 or 1). You can have different weights depending on your application. A different weight is represented as numbers greater than 1. diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md index 85f54b115e..d0dbe75b09 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md @@ -10,7 +10,12 @@ Let's practice removimg items from an ES6 Set using the delete meth First, create an ES6 Set var set = new Set([1,2,3]); Now remove an item from your Set with the delete method. -
set.delete(1);
console.log([...set]) // should return [ 2, 3 ]
+ +```js +set.delete(1); +console.log([...set]) // should return [ 2, 3 ] +``` + ## Instructions diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/typed-arrays.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/typed-arrays.english.md index 98e59e9a27..d1682d1518 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/typed-arrays.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/typed-arrays.english.md @@ -13,12 +13,33 @@ However, in the world of high performance and different element types, sometimes Typed arrays are the answer to this problem. You are now able to say how much memory you want to give an array. Below is a basic overview of the different types of arrays available and the size in bytes for each element in that array.
TypeEach element size in bytes
Int8Array1
Uint8Array1
Uint8ClampedArray1
Int16Array2
Uint16Array2
Int32Array4
Uint32Array4
Float32Array4
Float64Array8
There are two ways in creating these kind of arrays. One way is to create it directly. Below is how to create a 3 length Int16Array. -
var i8 = new Int16Array(3);
console.log(i8);
// Returns [0, 0, 0]
+ +```js +var i8 = new Int16Array(3); +console.log(i8); +// Returns [0, 0, 0] +``` + You can also create a buffer to assign how much data (in bytes) you want the array to take up. Note
To create typed arrays using buffers, you need to assign the number of bytes to be a multiple of the bytes listed above. -
// Create same Int16Array array differently
var byteSize = 6; // Needs to be multiple of 2
var buffer = new ArrayBuffer(byteSize);
var i8View = new Int16Array(buffer);
buffer.byteLength; // Returns 6
i8View.byteLength; // Returns 6
console.log(i8View); // Returns [0, 0, 0]
+ +```js +// Create same Int16Array array differently +var byteSize = 6; // Needs to be multiple of 2 +var buffer = new ArrayBuffer(byteSize); +var i8View = new Int16Array(buffer); +buffer.byteLength; // Returns 6 +i8View.byteLength; // Returns 6 +console.log(i8View); // Returns [0, 0, 0] +``` + Buffers are general purpose objects that just carry data. You cannot access them normally. To access them, you need to first create a view. -
i8View[0] = 42;
console.log(i8View); // Returns [42, 0, 0]
+ +```js +i8View[0] = 42; +console.log(i8View); // Returns [42, 0, 0] +``` + Note
Typed arrays do not have some of the methods traditional arrays have such as .pop() or .push(). Typed arrays also fail Array.isArray() that checks if something is an array. Although simpler, this can be an advantage for less-sophisticated JavaScript engines to implement them. diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md index ef4ffb9337..fe5a518804 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md @@ -9,7 +9,13 @@ challengeType: 1 Do you remember the ES6 spread operator ...? ... can take iterable objects in ES6 and turn them into arrays. Let's create a Set, and check out the spread function. -
var set = new Set([1,2,3]);
var setToArr = [...set]
console.log(setToArr) // returns [ 1, 2, 3 ]
+ +```js +var set = new Set([1,2,3]); +var setToArr = [...set] +console.log(setToArr) // returns [ 1, 2, 3 ] +``` + ## Instructions