😃Practice DSA

This page involves the order in which I prepared

This page is collection of all the resources I followed in order to prep for the DSA

Recursion

1. Print the string but using recursion

function printRecursive(str) {

     if(str.length === 0){
          return ""
     }

     return str.charAt(0)  + printRecursive(str.substring(1))    
}

printRecursive("Sandeep")

2. Reverse a string

function reverseStr(str) {

     if(str.length === 0){
          return ""
     }

     return reverseStr(str.substring(1)) +  str.charAt(0)    
}

reverseStr("Sandeep")

//const sub = str.substring(1) -> gives string from index mentioned to end


// p
// F("p")p    + e
// F(ep)   pe  + e
// F(eep)   pee + d
// F(deep) peed  + n
// F(ndeep) peedn  + a
// F(andeep)peedna + S

// peednas

3. String Palindrome

function isPal(str) {

    if(str.length===0 || str.length===1)return true


    if(str[0] === (str[str.length-1])){
        return isPal(str.substring(1,str.length-1))
    }
 
    return false
}


isPal("loocl")
// isPal("malayalal")

3. Decimal to Binary -> Example Convert 25 = 1 1 1 0 1 0 0 1

function decToBin(num, bin) {

  if(num === 0){
      return bin
  }

  bin += num % 2
  return decToBin(Math.floor(num/2), bin)

}

decToBin(5,"")

4. Sum of n input numbers

function sumOfNums(num){
    if(num === 1) {
        return 1
    }

    return num + sumOfNums(num-1)
    
}

sumOfNums(5)

/*

1              -> 1
2  + F(sum(1)) -> 2 + 1 = 3
3  + F(sum(2)) -> 3 + 3 = 6
4  + F(sum(3))
5  + F(sum(4))
6  + F(sum(5))
7  + F(sum(6))
9  + F(sum(8))
10 + F(sum(9))

*/ 

5. Fibonacci series -> recursion

function fib(number) {

    if(number === 0 || number === 1) return number

    return fib(number - 1) + fib(number - 2)
}


fib(6)

//f(0 1 2 3 4 5 6)

// 0 1 1 2 3 5 8

/*

fib(0) = 0
fib(1) = 1

fib(2) = fib(0) + fib(1)

fib(4) = fib(3)           +      fib(2)
       = fib(2) + fib(1)  +    fib(1) + fib(0)
       = fib(1) + fib(0)  + fib(1) + fib(1) +fib(0)
       = 1+0+1+1+0


**F(0) -> 0

*F(1) -> 1

*F(2) -> *F(1)=1 + **F(0)=0

*F(3) -> *F(2) + F(1)

F(4) -> *F(3) + F(2) 
*/ 
//

Last updated