Skip to content

Algos #7

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

Open
wants to merge 10 commits into
base: main
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
function selectionSort(arr) {
// type your code here
}
newArr = []

while (arr.length !== 0) {
minny = Math.min(...arr)
index = arr.indexOf(minny)

newArr.push(minny)
arr.splice(index, 1)
console.log(newArr)

}

}




if (require.main === module) {
// add your own tests in here
Expand All @@ -20,4 +34,13 @@ if (require.main === module) {
module.exports = selectionSort;

// Please add your pseudocode to this file

//create new array to push things into
//create a while loop that goes until arr is 0
//find smallest value in array
//find index of smallest value
//push min value to new array
//remove that value using slice
//some elm = Math.min, somewhere?

// And a written explanation of your solution
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function recursiveCount(num = 0) {
// type your code here
if (num <= 9) {
console.log(num)


recursiveCount(num + 1)
}
}

if (require.main === module) {
Expand All @@ -11,3 +16,7 @@ module.exports = recursiveCount;
// OPTIONAL
// Please add your pseudocode to this file
// And a written explanation of your solution

//I make an if statement to establish the base count.
//I then console.log(num) to print num as it returns up the stack
//I make the recursive call of recursiveCount(num + 1)
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function recursiveSearch(arr, target) {
// type your code here
if (arr.length === 0) {
return false
}

if (arr[0] === target) {
return true
}

return recursiveSearch(arr.slice(1), target)


}

if (require.main === module) {
Expand All @@ -15,5 +25,22 @@ if (require.main === module) {

module.exports = recursiveSearch;









// Please add your pseudocode to this file

//if array.length = 0
//return false
//if element[i] in array = target
//return true
//someVariable = array.slice
//recursiveSearch(someVariable, target)


// And a written explanation of your solution
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function fibonacci(n) {
// type your code here
if (n < 2) {
return n
}

return fibonacci(n - 1) + fibonacci(n - 2)

}

if (require.main === module) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function findShortestStringRecursive(arr) {
// type your code here
if (arr.length === 1) {
return arr[0]
}

const result = findShortestStringRecursive(arr.slice(1))

return arr[0] <= result.length ? arr[0] : result

}

if (require.main === module) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function selectionSortRecursive(arr) {
// type your code here
if (arr.length < 1) {
return []
}

const smallestNum = Math.min(...arr)
const index = arr.indexOf(smallestNum)
arr.splice(index, 1)
const result = selectionSortRecursive(arr)
result.unshift(smallestNum)
return result

}

if (require.main === module) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,58 @@ class Stack {
// add item to top of stack if not full
// if full throw error
push(item) {

if (!this.isFull()) {
this.stack.push(item)
} else {
throw new Error("Stack is full!")
}
}

// remove item from top of stack and return it
pop() {

return this.stack.pop()
}

// return item at top of stack without removing it
peek() {

return this.stack[this.size() -1]
}

// return true if stack is empty, otherwise false
isEmpty() {

return this.size() === 0
}

// return true if stack is full, otherwise false
isFull() {

return this.size() === this.limit
}

// return number of items in stack
size() {

return this.stack.length
}

// return -1 if item not in stack, otherwise integer representing
// how far it is from the top
search(target) {
for (let i = -1; i >= -this.size(); --i) {
if (this.stack[this.size() + i] === target) {
return Math.abs(i) - 1;
}
}

return -1;
}


// print contents of stack: do not return the stack itself!
print() {

console.log(this.stack.join(' <- '))
}
}


if (require.main === module) {
// add your own tests in here
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,59 @@ class Queue {
// add item to rear of queue if not full
// if full throw error
enqueue(item) {
if (!this.isFull()) {
this.queue.push(item)
} else {
throw new Error("Queue is full!")
}

}


// remove item from front of queue and return it
dequeue() {

return this.queue.shift()
}


// return item at front of queue without removing it
peek() {

return this.queue[0]
}

// return true if queue is empty, otherwise false
isEmpty() {

if (this.size() === 0) {
return true
} else {
return false
}
}

// return true if queue is full, otherwise false
isFull() {

return this.size() === this.limit
}

// return number of items in queue
size() {

return this.queue.length
}

// return -1 if item not in queue, otherwise integer representing
// how far it is from the front
search(target) {

if( this.queue.indexOf(target) > -1) {
return this.queue.indexOf(target)
} else {
return -1
}

}

// print contents of queue: do not return the queue itself!
print() {

console.log(this.queue.join(' <- '))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,61 @@ class MySet {
// if an iterable is provided only its unique values should be in data
// strings and arrays will need to be broken down by their elements/characters
constructor(iterable) {
if (!(iterable === undefined ||
Array.isArray(iterable) ||
typeof iterable === "string")) {
throw new Error("MySet only accepts iterable or nothing on initialization")
}



this.data = {};

if (iterable) {
for (const e of iterable) {
this.data[e] = true
}
}



}

// return number of elements in MySet
size() {

return this.entries().length
}

// add an item to MySet as is
// don't worry about arrays here!
// return the MySet instance
add(item) {

this.data[item] = true
return this
}

// delete an item from MySet
// don't worry about arrays here!
// return true if successful, otherwise false
delete(item) {
if (this.has(item)) {
delete this.data[item]
return true
} else {
return false
}

}

// return true if in MySet, otherwise false
// don't worry about arrays here!
has(item) {

return !!this.data[item]
}

// return data as an array
// don't worry about arrays here!
entries() {

return Object.keys(this.data)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
class Node {
constructor() {
// add your Node class code
constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}
}

// list = [1, 4, 7]
function oneToSeven() {
// manually create the BST
// then return the root node
const left = new Node(1);
const right = new Node(7);

return new Node(4, left, right);
}

// list = [10, 40, 45, 46, 50]
function tenToFifty() {

const tenNode = new Node(10);
const fortyNode = new Node(40, tenNode);
const fortySixNode = new Node(46);
const fiftyNode = new Node(50, fortySixNode);

return new Node(45, fortyNode, fiftyNode);
}

// list = [-20, -19, -17, -15, 0, 1, 2, 10]
function negativeToPositive() {

const nTwenty = new Node(-20);
const nSeventeen = new Node(-17);
const nNineteen = new Node(-19, nTwenty, nSeventeen);
const one = new Node(1);
const zero = new Node(0, null, one);
const ten = new Node(10);
const two = new Node(2, zero, ten);

return new Node(-15, nNineteen, two);
}

if (require.main === module) {
Expand Down