Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba64b82

Browse files
adding weave queue
1 parent d683f73 commit ba64b82

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

‎weaveQueue/directions.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# --- Directions
2+
3+
* Complete the task in queue.js
4+
* Implement the 'weave' function. Weave
5+
receives two queues as arguments and combines the
6+
contents of each into a new, third queue.
7+
The third queue should contain the *alterating* content
8+
of the two queues. The function should handle
9+
queues of different lengths without inserting
10+
'undefined' into the new one.
11+
*Do not* access the array inside of any queue, only
12+
use the 'add', 'remove', and 'peek' functions.
13+
14+
15+
# --- Example
16+
17+
const queueOne = new Queue();
18+
queueOne.add(1);
19+
queueOne.add(2);
20+
const queueTwo = new Queue();
21+
queueTwo.add('Hi');
22+
queueTwo.add('There');
23+
const q = weave(queueOne, queueTwo);
24+
q.remove() should return 1
25+
q.remove() should return 'Hi'
26+
q.remove() should return 2
27+
q.remove() should return 'There'
28+
29+
30+
# --- Solutions
31+
32+
33+
// Solution
34+
35+
const Queue = require('./queue');
36+
37+
function weave(sourceOne, sourceTwo) {
38+
const q = new Queue();
39+
40+
while(sourceOne.peek() || sourceTwo.peek()) {
41+
if(sourceOne.peek()) {
42+
q.add(sourceOne.remove());
43+
}
44+
45+
if(sourceTwo.peek()) {
46+
q.add(sourceTwo.remove());
47+
}
48+
}
49+
50+
return q;
51+
}

‎weaveQueue/queue.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// --- Directions
2+
// Implement a 'peek' method in this Queue class.
3+
// Peek should return the last element (the next
4+
// one to be returned) from the queue *without*
5+
// removing it.
6+
7+
class Queue {
8+
constructor(){
9+
this.data = [];
10+
}
11+
12+
add(record) {
13+
this.data.unshift(record);
14+
}
15+
16+
remove(){
17+
return this.data.pop();
18+
}
19+
20+
peek() {
21+
return this.data[this.data.length-1];
22+
}
23+
}

‎weaveQueue/weaveQueue.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// // # --- Directions
2+
// * Complete the task in weave/queue.js
3+
// * Implement the 'weave' function. Weave
4+
// receives two queues as arguments and combines the
5+
// contents of each into a new, third queue.
6+
// The third queue should contain the *alterating* content
7+
// of the two queues. The function should handle
8+
// queues of different lengths without inserting
9+
// 'undefined' into the new one.
10+
// *Do not* access the array inside of any queue, only
11+
// use the 'add', 'remove', and 'peek' functions.
12+
13+
14+
// # --- Example
15+
// const queueOne = new Queue();
16+
// queueOne.add(1);
17+
// queueOne.add(2);
18+
// const queueTwo = new Queue();
19+
// queueTwo.add('Hi');
20+
// queueTwo.add('There');
21+
// const q = weave(queueOne, queueTwo);
22+
// q.remove() should return 1
23+
// q.remove() should return 'Hi'
24+
// q.remove() should return 2
25+
// q.remove() should return 'There'
26+
27+
// Solution
28+
29+
const Queue = require('./queue');
30+
31+
function weave(sourceOne, sourceTwo) {
32+
const q = new Queue();
33+
34+
while(sourceOne.peek() || sourceTwo.peek()) {
35+
if(sourceOne.peek()) {
36+
q.add(sourceOne.remove());
37+
}
38+
39+
if(sourceTwo.peek()) {
40+
q.add(sourceTwo.remove());
41+
}
42+
}
43+
44+
return q;
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /