Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C16 - Cedar - Afina Walton #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function balanced(string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💫🌸 Impressive! You didn't need to implement this, but good work. I apologize for the confusion!

// implement stack to check balanced strings
let matches = {
'(': ')',
'{': '}',
'[': ']',
}

let stack = [];
// if open, add to stack
for (let char of string) {
if (char in matches) {
stack.push(char);
continue;
} else {
let popped = stack.pop();
if (matches[popped] === char) continue;
else return false;
}
}

if (stack.length) return false;
return true;
}

function evaluatePostfix(expr) {
let operations = {
'+': add = (a, b) => a + b,
'*': mult = (a, b) => a * b,
'-': subtract = (a, b) => a - b,
'/': divide = (a, b) => a / b,
}

let stack = [];
let newNum = 0;

for (let char of expr) {
if (char in operations) {
if (stack.length > 1) {
let popA = +stack.shift();
let popB = +stack.shift();
newNum = Math.floor(operations[char](popA, popB));
} else if (stack.length === 1) {
let popped = +stack.shift();
newNum = Math.floor(operations[char](newNum, popped));
}
} else {
stack.push(char);
continue;
}
}
if (stack.length === 0) return newNum;
}

module.exports = { balanced, evaluatePostfix }
48 changes: 40 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
class Queue {
#front;
#rear;

constructor() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 While your implementation does work and utilize Javascript arrays effectively, the readme asks you to implement a queue using a circular buffer.

// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new Array(20);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 When implementing a circular buffer, it's often helpful to have a capacity field to enable users to set the size of the buffer (you'd want to set capacity as a parameter which can to default to 20)

Suggested change
this.store = new Array(20);
this.capacity = capacity
this.store = new Array(capacity);

// front and rear are index refs
this.#front = this.#rear = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Curious as to why you chose to set the front and rear fields as private

}

enqueue(element) {
throw new Error("This method has not been implemented!");
enqueue(element) {
if (
(this.#front === 0 && this.#rear === this.size() - 1)
) {
throw 'QueueFullException'
}
else if (this.isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice use of your other methods!

this.store[this.#front] = element;
} else {
this.#rear += 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Make sure you're handling the case where rear wraps around!

Suggested change
this.#rear += 1;
this.#rear = (this.#rear + 1) % this.capacity;

this.store[this.#rear] = element;
}

return;
}

dequeue() {
throw new Error("This method has not been implemented!");
if (this.isEmpty()) {
throw 'QueueEmptyException'
}

const data = this.store[this.#front];
this.store[this.#front] = null;

if (this.#front === this.#rear) {
this.#front = this.#rear = -1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your constructor, you initialize front and rear to zero. Make sure you stay consistent on this

Suggested change
this.#front = this.#rear = -1;
this.#front = this.#rear = 0;

} else if (this.#front === this.size() - 1) {
this.#front = 0;
} else {
this.#front += 1
}

return data;
}

front() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.store[this.#front];
}

size() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.store.length;
}

isEmpty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
if (this.#front === this.#rear && !this.store[this.#front]) return true
return false;
}

toString() {
Expand Down
17 changes: 11 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
const LinkedList = require("./linked-list");

class Stack {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 While your implementation does work and utilize Javascript arrays effectively, unfortunately the readme requested you implement stacks using the included LinkedList class.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new LinkedList();
}

push() {
throw new Error("This method has not been implemented!");
push(element) {
this.store.addLast(element);
return;
}

pop() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
const toRemove = this.store.getLast();
this.store.delete(toRemove);

return toRemove;
}

isEmpty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.store.isEmpty();
}

toString() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 You might consider LinkedList's toString method instead!

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"scripts": {
"test": "jest"
},
"jest": {
"verbose": true
},
"author": "",
"license": "ISC",
"dependencies": {
"jest": "^26.4.2"
"jest": "^27.5.1"
}
}
Loading