Skip to content
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
64 changes: 64 additions & 0 deletions Feb26.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This problem was asked by Facebook.

// Write a function that rotates a list by k elements.
// For example, [1, 2, 3, 4, 5, 6] rotated by two
// becomes [3, 4, 5, 6, 1, 2].

// Try solving this without creating a copy of the list.
// How many swap or move operations do you need?

const rotateList = function (listArray, k){ // input list( array ) tp be rotated by k elements
// first, verify input
if(!Array.isArray(listArray) || listArray?.length<=0){
console.log( "list provided was not an array, or is empty")
return
}
if(!Number.isInteger(k)) {
console.log( "variable k for number of rotations must be an integer")
return
}

// now to do this ' inplace ' without making a copy of the ListArray we will remove ( shift) the first indexed value, cache it and push it on the back and do this k times
// but what if k is negative ? we should rotate the other way! currently it wont rotate at all
// we could further optimise by reducing the number of rotations if it is more than the length of the array...
if(Math.abs(k)>=listArray.length){
k = k%listArray.length // k is now the remainder remaining after being divided by the length, so if the length is 9 and k is 9 or 81 we shouldn't rotate 9/9 && 81/9 don't have any remainder.
// and edge case for modulo is when called on negative numbers HOWEVER: JS % operator is actually a remainder function instead of true modulo and therefore returns negative numbers as desired for our use-case!
// console.log( `number of rotations requested is inefficient, reducing required number to most efficient int`)
}
if( k > 0 ){
console.log(
`the list to rotate is ${listArray}, which will be rotated ${k} times`
)
for (let i = 0; i < k; i++) {
listArray.push(listArray.shift())
}
} else if( k < 0 ) {
console.log(
`the list to rotate is ${listArray}, which will be rotated ${k} times`
)
for (let i = 0; i < -k; i++) {
listArray.unshift(listArray.pop())
}
} // if k = 0 we dont do any rotations before returning the array

console.log(`The newly rotated list is: ${listArray}, which was rotated ${k} times`)
return listArray
}

let bug = NaN
let setBug = new Set([1,2,3,4])
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], 1) // The newly rotated list is: 2,3,4,5,6,7,8,9,1, which was rotated 1 times
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], 0) // k = 0
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], 9) // k = 0
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], 100) // k => 1
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], -4) // 6,7,8,9,1,2,3,4,5, which was rotated -4 times
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], -400) // k => -4
rotateList([], 0) // error, array is empty
rotateList([1, 2, 3, 4, 5, 6, 7, 8, 9], bug) // k is not a number
rotateList({randomArray:[0,1,2]}, 1) // error, is not an array
rotateList(setBug, 1) // error, is not an array
rotateList( [ [1],[2],[3]], 2) // The newly rotated list is: 3,1,2, which was rotated 2 times
rotateList( ['a','b','c','d','e'], 2) // The newly rotated list is: c,d,e,a,b, which was rotated 2 times
rotateList(['identityBug?'], 5) // The newly rotated list is: identityBug, which was rotated 0 times
rotateList(['identityBug?'], -5) // The newly rotated list is: identityBug?, which was rotated 0 times
127 changes: 127 additions & 0 deletions Feb27.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Let's represent an integer in a linked list format by having each node represent a digit in the number. The nodes make up the number in reversed order.

// For example, the following linked list:

// 1 -> 2 -> 3 -> 4 -> 5
// is the number 54321.

// Given two linked lists in this format, return their sum in the same linked list format.

// For example, given

// 9 -> 9
// 5 -> 2
// return 124 (99 + 25) as:

// 4 -> 2 -> 1
///////////////////////////////////////////////
// DEMO SLL implementation
///////////////////////////////////////////////
class Node {
// constructor
constructor(element)
{
this.element = element;
this.next = null
}
}


// linkedlist class
class LinkedList {
constructor() {
this.head = null
this.size = 0
}

// functions to be implemented
// add(element)
// adds an element at the end
// of list
add(element) {
// creates a new node
var node = new Node(element)

// to store current node
var current

// if list is Empty add the
// element and make it head
if (this.head == null) this.head = node
else {
current = this.head

// iterate to the end of the
// list
while (current.next) {
current = current.next
}

// add node
current.next = node
}
this.size++
}
// insertAt(element, location)
// removeFrom(location)
// removeElement(element)

// Helper Methods
// isEmpty
// size_Of_List
// PrintList
}

///////////////////////////////////////////////
const sumLinkedLists = function (list1, list2) {

let num1Array = []
let num2Array = []
console.log( list1, list2)
let tempNode = list1.head
for(let i=0; i<list1.size; i++){
num1Array.unshift(tempNode.element)
tempNode = tempNode.next
}
console.log(num1Array)
tempNode = list2.head
for (let j = 0; j < list2.size; j++) {
num2Array.unshift(tempNode.element)
tempNode = tempNode.next
}
console.log(num2Array)

let num1 = parseInt(num1Array.join('')) // now a number parsed form joint array string digits
let num2 = parseInt(num2Array.join('')) // now a number
let sum = num1 + num2 //number
console.log(`num1 ${num1} + num2 ${num2} = ${sum} ? - check`)
// theres a bette way of doing this... could enumerate over the number instead of the array we are building off of the number.... ?
let tempString = sum.toString() // number to string
let num3Array = tempString.split('') // string to array
let digits = num3Array.length
let list3 = new LinkedList // make new linkedList from num3Array starting with the lowest digit and going up.
for ( let k=0; k<digits; k++) {
list3.add( num3Array[digits-k-1] )
}
console.log(list3)
return(list3)
}

let sixtySix = new LinkedList
sixtySix.add(6)
sixtySix.add(6)
let hundredFifteen = new LinkedList
hundredFifteen.add(5)
hundredFifteen.add(1)
hundredFifteen.add(1)
let ninetyNine = new LinkedList
ninetyNine.add(9)
ninetyNine.add(9)
let seven = new LinkedList
seven.add(7)
let five = new LinkedList
five.add(5)

sumLinkedLists( ninetyNine, seven)
sumLinkedLists( sixtySix, hundredFifteen)
sumLinkedLists( seven, five)