About Me

About Me : I have been working as a Software Engineer for various international companies for four years.Currently, I am working as a full stack Javascript developer in Petronas(Malaysia).

Skills

Skills • Javascript •Typescript •Python •C •Java •ReactJs • Redux • VueJs • NestJs • React Testing Library • Django• PostgreSQL • MySQL • NodeJs • Git • Docker • Jira • Visual Studio Code • Slack

বৃহস্পতিবার, ১০ নভেম্বর, ২০২২

Optional chaining (?.) in Javascript

 

The optional chaining ?. is a safe way to access nested object properties, even if a mid   property doesn’t exist.

As an example, let’s say we have person objects and most of person have addresses in person.address property, with the street person.address.street, but some did not provide them.

In such case, when we attempt to get person.address.street without having street ,we get an error:

 

let person = {

}

 

console.log(person.address.street)

//error

 

 

As there is no address in person, an attempt to get person.address.street fails with an error.

But we if use optional chaining , we will get undefined instead of error.

 

console.log(person?.address?.street)  // undefined

 

In many practical cases we’d prefer to get undefined instead of an error here (meaning “no street”).

 

One case would be like this ,

 

let street = person?.address?.street ?? ' No street' ;

 

 

 

শুক্রবার, ২২ এপ্রিল, ২০২২

Top View of Binary Tree using JavaScript

class Solution

{

    //Function to return a list of nodes visible from the top view 

    //from left to right in Binary Tree.

    topView(root)

    {

        //your code here

        let map = {} ;

        function f(root, hdis, level){

            if(root === null) return ;

            if(map[hdis] === undefined) {

                map[hdis] = [level, root.data] ;

            }else{

                if(level<map[hdis][0]){

                    map[hdis] = [level, root.data]

                }

            }

            f(root.left, hdis-1, level+1);

            f(root.right, hdis +1, level+1);

        }

        f(root, 0, 0);

       // console.log(map)

        let arr = [] , ans = [];

        for(let [key, value] of Object.entries(map)){

           // console.log(value)

            arr.push(key);

        }

        

        arr.sort((a,b)=>a-b);

       // console.log(arr)

        for(let i = 0;i<arr.length;i++){

            ans.push(map[arr[i]][1])

        }

        return ans;

    }

}

রবিবার, ১৭ সেপ্টেম্বর, ২০১৭

UVA 539 SOLUTION

#include<stdio.h>
#define READ freopen("input.txt","r",stdin)
#define WRITE freopen("output.txt","w",stdout)
#define ll long long
#define MAX(a,b)  a>b?a:b
#define siz 102
using namespace std ;

int node,edge,mx,path;
int adj[50][50] ;

void dfs(int cur)
{

    for(int i = 0; i< node; i++)
    {

        if(adj[cur][i]==1)
        {
            path++;
            adj[cur][i]= 0 ;
            adj[i][cur]= 0 ;
            dfs(i);
            adj[cur][i]= 1 ;
            adj[i][cur]= 1 ;
            path--;

        }
    }
       mx = MAX(mx,path) ;
}


int main()
{

  //  READ;
    while(scanf("%d %d",&node,&edge)==2)
    {
        if(node==0 && edge==0) break ;

        for(int i = 0 ; i<32; i++)
            for(int j = 0; j<32; j++) adj[i][j] = 0 ;

        // for(int i =0 ; i<=node; i++) mark[i] = false ;
        for(int i = 0; i<edge; i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            adj[u][v] = 1 ;
            adj[v][u] = 1 ;
        }
        int ans = 0 ;
        for(int i = 0 ; i<node; i++)
        {
            mx = 0;
            dfs(i);
            ans = MAX(ans, mx) ;
        }
        printf("%d\n",ans) ;
    }

    return 0 ;
}


সোমবার, ২১ আগস্ট, ২০১৭

uva 260 solution

#include<stdio.h>
#define READ freopen("input.txt","r",stdin)
#define WRITE freopen("output.txt","w",stdout)
#define ll long long
#define Max(a,b) (a>b?a:b)
int x[]=  {-1,0,-1,0,1,1};
int y[] = {0,1,-1,-1,0,1};

char str[201][201];
bool visited[201][201];
int row ;
bool black ;

void dfs(int r,int c)
{
    if(r == 0) black  =true ;
    for(int i=0; i<6; i++)
    {
        int R = r+x[i];
        int C = c+y[i];
        if(R>=0 && R<row && C>=0 && C<row && visited[R][C]==false&&str[R][C]=='b')
        {
            visited[R][C] =true ;
            dfs(R,C);
        }
    }

}
void init()
{
    for(int i=0; i<201; i++)
        for(int j=0; j<201; j++) visited[i][j]= false ;
    black=false;
}
int main()
{
  //  READ ;
    int cnt = 0 ;
    while(scanf("%d",&row)==1 && row!=0)
    {
        init() ;
        for(int i = 0; i< row ; i++) scanf("%s",str[i]);

        for(int i=0; i<row; i++)
        {
            if(str[row-1][i]=='b')
            {
                dfs(row-1,i);
            }
        }
        if(black)printf("%d %c\n",++cnt,'B');
        else printf("%d %c\n",++cnt,'W');

    }


    return 0 ;
}

বৃহস্পতিবার, ১৭ আগস্ট, ২০১৭

UVA 167

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define READ freopen("input.txt","r",stdin)
#define ll long long
int ary[9][9];
int chk[9][9];
int mx ;

int MAX(int a,int b)
{
    return a>b?a:b;
}

bool issafe(int row,int col)
{
    for(int i=0;i<8;i++)
    {
        if(chk[row][i]==1) return false ;
    }
    for(int i=0;i<=row;i++)
        if(chk[i][col]==1) return false;
    for(int i=row,j=col;i>=0 && j>=0;i--,j--)
    {
        if(chk[i][j]==1) return false;
    }
    for(int i=row,j=col;i>=0&& j<8;i--,j++)
    {
        if(chk[i][j]==1) return false;
    }
    return true;
}
void NQ(int r,int sum)
{
    if(r==8)
    {
        mx = MAX(mx,sum);
        return ;
    }
    for(int c = 0; c<8; c++)
    {
        if(issafe(r,c))
        {
            chk[r][c] =1;
            NQ(r+1,sum+ary[r][c]);
            chk[r][c] =0;
        }
    }
}
int main()
{
    READ ;
    int test ;
    scanf("%d",&test);
    while(test--)
    {
        for(int i=0; i<8; i++)
        {
            for(int j=0; j<8; j++) scanf("%d",&ary[i][j]);
        }
        mx = -1 ;
        //clear();
        NQ(0,0);
        printf("%5d\n",mx);
    }
    return 0;
}

সোমবার, ১৪ আগস্ট, ২০১৭

UVA 280 SOLUTION

#include<stdio.h>
#define READ freopen("Text.txt","r",stdin)
#define ll long long
int node;
int graph[102][102];
int visited[102];
void clear(int node)
{
 for (int i = 0; i <= node; i++) visited[i] = 0;
}
void init_Graph()
{
 for (int i = 0; i < 101; i++)
  for (int j = 0; j < 102; j++) graph[i][j] = 0;
}
void dfs(int n)
{
 for (int i = 1; i <= node; i++)
 {
  if (graph[n][i] == 1 && visited[i] == 0)
  {
   visited[i] = 1;
   dfs(i);
  }
 }
 return;
}
int main()
{
 // READ;
 while (scanf("%d", &node) == 1 && node != 0)
 {
  int start, n, q, s;
  init_Graph();
  while (scanf("%d", &start) == 1 && start != 0)
  {
   while (scanf("%d", &n) && n != 0)
   {
    graph[start][n] = 1;
   }
  }
  scanf("%d", &q);
  while (q--)
  {
   scanf("%d", &s);
   int ans = 0;
   clear(node);
   dfs(s);
   for (int i = 1; i <= node; i++) if (visited[i] == 0) ans++;
   printf("%d", ans);
   for (int i = 1; i <= node; i++) if (visited[i] == 0)  printf(" %d", i);
   puts("");
  }
 }
 return 0;
}

uva 118 solution

#include<stdio.h>
#define ll long long
int row, col;
int ary[100][100];
char d[10], str[102];
int main()
{
 //freopen("Text.txt", "r", stdin);
 scanf("%d %d", &row, &col);

 for (int i = 0; i <= row; i++)
  for (int j = 0; j <= col; j++) ary[i][j] = 0;
 int r, c;
 while (scanf("%d %d %s", &r, &c, d) == 3)
 {
  scanf("%s", str);
  char dir = d[0];
  bool flag = true;
  for (int i = 0; str[i] != '\0'; i++)
  {
   if (str[i] == 'R' || str[i] == 'L')
   {
    //printf("%d\n",i+1);
    if (dir == 'E')
    {
     str[i] == 'R' ? dir = 'S' : dir = 'N';
    }
    else if (dir == 'N')
    {
     str[i] == 'R' ? dir = 'E' : dir = 'W';
    }
    else if (dir == 'S')
    {
     str[i] == 'R' ? dir = 'W' : dir = 'E';
    }
    else
    {
     str[i] == 'R' ? dir = 'N' : dir = 'S';
    }
    //printf("---%c----", dir);
   }
   else
   {
    if (dir == 'E')
    {
     if (r + 1 <= row) r += 1;
     else
     {
      if (ary[r][c] == 0)
      {
       ary[r][c] = 1;
       printf("%d %d %c LOST\n", r, c, dir);
       flag = false;
       break;
      }
      else continue;
     }
    }

    if (dir == 'N')
    {
     if (c + 1 <= col) c += 1;
     else
     {
      if (ary[r][c] == 0)
      {
       ary[r][c] = 1;
       printf("%d %d %c LOST\n", r, c, dir);
       flag = false;
       break;
      }
      else continue;
     }
    }

    if (dir == 'S')
    {
     if (c - 1 >= 0) c -= 1;
     else
     {
      if (ary[r][c] == 0)
      {
       ary[r][c] = 1;
       printf("%d %d %c LOST\n", r, c, dir);
       flag = false;
       break;
      }
      else continue;
     }
    }

    if (dir == 'W')
    {
     if (r - 1 >= 0) r -= 1;
     else
     {
      if (ary[r][c] == 0)
      {
       ary[r][c] = 1;
       printf("%d %d %c LOST\n", r, c, dir);
       flag = false;
       break;
      }
      else continue;
     }
    }

    //printf("%d %d\n", r, c);
   }
   // printf("%c\n",dir);
  }
  if (flag) printf("%d %d %c\n", r, c, dir);
 }

 return 0;
}