Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- hello
- ll
- */
- // var strStr = function (string, subString) {
- // if (subString === '') return 0
- // for (let i = 0; i <= string.length - subString.length; i++) {
- // let j = 0
- // for (; j < subString.length; j++) {
- // if (string[i + j] !== subString[j]) break
- // }
- // if (j === subString.length) return i
- // }
- // return -1
- // }
- // console.log(strStr('hello', 'll'))
- /*
- building A prefix suffix table
- aabaaab
- aabaabaaab
- aabaaab
- 0101223
- abceabcd
- abcd
- aabaaab
- i
- j
- table = [0, 1, 0, 1, 2, 2, 3 ]
- */
- // var strStr = function (string, subString) {
- // if (subString === '') return 0
- // const prefixTable = buildPrefixTable(subString)
- // let i = 0
- // let j = 0
- // while (i < string.length && j < subString.length) {
- // if (string[i] === subString[j]) {
- // i++
- // j++
- // } else if (j > 0) {
- // j = prefixTable[j - 1]
- // } else {
- // i++
- // }
- // }
- // return j === subString.length ? i - j : -1
- // }
- // console.log(strStr('abceabcd', 'abcd'))
- // function buildPrefixTable(s) {
- // const table = [0]
- // let i = 1
- // let j = 0
- // while (i < s.length) {
- // if (s[i] === s[j]) {
- // j += 1
- // table[i] = j
- // i++
- // } else if (j > 0) {
- // j = table[j - 1]
- // } else {
- // table[i] = 0
- // i++
- // }
- // }
- // return table
- // }
- //Recursion
- //iterative solution
- // function countDown(num) {
- // for (let i = num; i >= 0; i--) {
- // console.log(i)
- // }
- // }
- // countDown(10)
- //recursive solution
- // function countDown(num) {
- // //Must have a base case
- // if (num < 0) return
- // //must return and something properly
- // console.log(num)
- // //input must be changed in some sort of fashion in recursive function
- // countDown(num - 1)
- // }
- // countDown(10)
- // function sumRange(num){
- // let sum = 0
- // for(let i = num; i > 0; i--){
- // console.log(i)
- // sum += i
- // }
- // console.log(sum)
- // }
- // sumRange(10)
- //recursive solution
- /*
- 10 + SumRange(9)
- 9 + sumRange(8)
- 8 + sumRange(7)
- 7 + sumRange(6)
- 6 + sumRange(5)
- 5 + sumRange(4)
- 4 + sumRange(3)
- 3 + sumRange(2)
- 2 + sumRange(1)
- 1 + sumRange(0)
- 0
- */
- // function sumRange(num){
- // if(num === 0) return 0
- // console.log(num)
- // return num + sumRange(num -1)
- // }
- // sumRange(10)
- //iterative solution
- //factorial 5! = 5 * 4 * 3 * 2 * 1
- // function factorial(num){
- // let result = 1
- // for(let i = num; i >= 1; i--){
- // console.log(i)
- // result *= i
- // }
- // console.log(result)
- // }
- // factorial(1)
- //recursive solution
- //factorial 5! = 5 * 4 * 3 * 2 * 1
- // function factorial(num){
- // //must have a base case
- // if(num === 0) return 1
- // //must return and console right thing in right time(What you need!)
- // //change function argument some sort of fashion
- // return num * factorial(num-1)
- // }
- // console.log(factorial(5))
- //impure recursive function
- // function createRange(num1, num2) {
- // let rangeArr = []
- // function helper(helperInput, targetNum = 0) {
- // //modify the outerScoped variable
- // rangeArr.push(helperInput)
- // console.log(helperInput)
- // if (helperInput === targetNum) return helperInput
- // helper(helperInput - 1, targetNum)
- // }
- // helper(10, num1)
- // console.log(rangeArr)
- // }
- //pure recursive function
- // function createRange(num1, num2) {
- // }
- // createRange(5, 10)
- // function findOdds(arr) {
- // const result = []
- // function helper(helperInput) {
- // //base case
- // console.log(helperInput)
- // if (helperInput.length === 0) return
- // if (helperInput[0] % 2 !== 0) {
- // result.push(helperInput[0])
- // }
- // helper(helperInput.slice(1))
- // }
- // helper(arr)
- // }
- // findOdds([10, 11, 12, 13, 14, 15])
- function findOdds(arr) {
- let result = []
- if (arr.length === 0) return result
- if (arr[0] % 2 !== 0) {
- result.push(arr[0])
- }
- result = result.concat(findOdds(arr.slice(1)))
- return result
- }
- console.log(findOdds([10, 11, 12, 13, 14, 15]))
- function power(base, exponent) {
- //iterative
- // return base ** exponent
- //recursive
- if (exponent === 0) return 1
- return base * power(base, exponent - 1)
- }
- console.log(power(2, 3))
- /*
- 2 * power(2, 2)
- 2 * power(2, 1)
- 2 * power(2, 0)
- 1
- */
- //product of array
- // function productOfArray(arr) {
- // if (arr.length === 0) {
- // return 1
- // }
- // return arr[0] * productOfArray(arr.slice(1))
- // }
- // console.log(productOfArray([1, 2, 3, 4]))
- //fibronaci series
- // FIBONACCI SOLUTION
- // 1, 2, 3, 5, 8, 13, 21, 34, 55
- // function fib(n){
- // if (n <= 2) return 1;
- // return fib(n-1) + fib(n-2);
- // }
- // fib(4) + fib(3)
- // fib(2) + fib(1)
- // 10 * 9
- // 10 * 9
- // 0 1
- // console.log(fib(5))
- // (0, 1, 1, 2, 3, 5)
- // function fib(n) {
- // // const result = [0, 1]
- // // for (let i = 2; i <= n; i++) {
- // // result.push(result[i - 1] + result[i - 2])
- // // }
- // // console.log(result[n])
- // if (n <= 2) return 1
- // const result = fib(n - 1) + fib(n - 2)
- // console.log(result)
- // return result
- // }
- // console.log(fib(10))
- // //iterative solution
- // function flatten(arr) {
- // let newArr = []
- // let i = 0
- // while (i < arr.length) {
- // if (!Array.isArray(arr[i])) {
- // newArr.push(arr[i])
- // console.log(newArr)
- // } else {
- // while (Array.isArray(arr[i])) {
- // console.log(arr[i])
- // }
- // }
- // console.log(arr[i])
- // i++
- // }
- // // newArr.push(arr[i])
- // console.log(newArr)
- // }
- function flatten(arr) {
- var i = 0
- if (!Array.isArray(arr)) {
- /* return non-array inputs immediately to avoid errors */
- return arr
- }
- while (i < arr.length) {
- if (Array.isArray(arr[i])) {
- arr.splice(i, 1, ...arr[i])
- } else {
- i++
- }
- }
- return arr
- }
- console.log(flatten([1, 2, 3, [4, [6], [5]]]))
- //binary search
- var searchInsert = function (nums, target) {
- let lo = 0
- let hi = nums.length - 1
- let mid
- while (lo <= hi) {
- mid = Math.floor((lo + hi) / 2)
- console.log(mid)
- const midVal = nums[mid]
- if (midVal === target) return mid
- if (target > midVal) {
- lo = mid + 1
- } else {
- hi = mid - 1
- }
- }
- return -1
- }
- console.log(searchInsert([1, 3, 5, 6, 7, 8], 8))
- // console.log(searchInsert([1, 3, 5, 6], 2))
- /*
- we are looking for 9
- [1, 2, 3, 4, 5, 6, 7, 8] 0+7 = 7/2 = 3.5 = 3
- l m h
- [1, 2, 3, 4, 5, 6, 7, 8] 7 + 4 = 11/2 = 5.5 = 5
- l m h
- [1, 2, 3, 4, 5, 6 , 7, 8] 7 + 6 = 13/2 = 6
- l
- h
- m
- [1, 2, 3, 4, 5, 6 , 7, 8] 7 + 6 = 13/2 = 6
- l
- h
- m
- */
- //6 3
- // log₂³² = 5
- // 2 * 2 * 2 * 2 * 2 = 32
- var strStr = function (haystack, needle) {
- if (needle === '') return 0
- for (let i = 0; i <= haystack.length - needle.length; i++) {
- let j = 0
- for (; j < needle.length; j++) {
- console.log(haystack[i +j], needle[j])
- if (haystack[i + j] !== needle[j]) break
- }
- console.log(j)
- if (j === needle.length) return i
- }
- return -1
- }
- console.log(strStr('mississippi', 'mississippi'))
- // --- Directions
- // Write a function that accepts a positive number N.
- // The function should console log a step shape
- // with N levels using the # character. Make sure the
- // step has spaces on the right hand side!
- // --- Examples
- // steps(2)
- // '# '
- // '##'
- // steps(3)
- // '# '
- // '## '
- // '###'
- // steps(4)
- // '# '
- // '## '
- // '### '
- // '####'
- // function steps(n) {
- // for (let row = 0; row < n; row++) {
- // let stair = ''
- // for (let column = 0; column < n; column++) {
- // if (column <= row) {
- // stair += '#'
- // } else {
- // stair += ' '
- // }
- // }
- // console.log(stair)
- // }
- // }
- // --- Directions
- // Write a function that accepts a positive number N.
- // The function should console log a pyramid shape
- // with N levels using the # character. Make sure the
- // pyramid has spaces on both the left *and* right hand sides
- // --- Examples
- // pyramid(1)
- // '#'
- // pyramid(2)
- // ' # '
- // '###'
- // pyramid(3)
- // ' # '
- // ' ### '
- // '#####'
- // function pyramid(n) {
- // //midpoint of proposed column
- // const midPoint = Math.floor((2 * n - 1) / 2)
- // for (let row = 0; row < n; row++) {
- // let level = ''
- // for (let column = 0; column < 2 * n - 1; column++) {
- // if (midPoint - row <= column && midPoint + row >= column) {
- // level += '#'
- // } else {
- // level += ' '
- // }
- // }
- // console.log(level)
- // }
- // console.log(midPoint)
- // }
- // matrix(3)
- // [[1, 2, 3],
- // [8, 9, 4],
- // [7, 6, 5]]
- // matrix(4)
- // [[1, 2, 3, 4],
- // [12, 13, 14, 5],
- // [11, 16, 15, 6],
- // [10, 9, 8, 7]]
- function matrix(n) {
- const results = []
- for (let i = 0; i < n; i++) {
- results.push([])
- }
- console.log(results)
- let counter = 1
- let startColumn = 0
- let endColumn = n - 1
- let startRow = 0
- let endRow = n - 1
- while (startColumn <= endColumn && startRow <= endRow) {
- //top row
- for (let i = startColumn; i <= endColumn; i++) {
- results[startRow][i] = counter
- counter++
- }
- startRow++
- //right column
- for (let i = startRow; i <= endRow; i++) {
- results[i][endColumn] = counter
- console.log(results[i][endColumn])
- counter++
- }
- endColumn--
- console.log(results)
- //Bottom row
- for (let i = endColumn; i >= startColumn; i--) {
- results[endRow][i] = counter
- counter++
- }
- endRow--
- console.log(endRow)
- //start column
- for (let i = endRow; i >= startRow; i--) {
- results[i][startColumn] = counter
- counter++
- }
- startColumn++
- // console.log(str)
- // if (startColumn === endColumn && startRow === endRow) {
- // results[startColumn][startRow] = counter
- // }
- console.log(startColumn, endColumn, startRow, endRow)
- console.log(results)
- }
- }
- matrix(3)
Add Comment
Please, Sign In to add comment