Friday, 26 July 2019

ট্রিগার এর মাধ্যমে ডাটা ইনসার্ট - insert data using Database Trigger (Mysql)




সর্বপ্রথম আমরা প্রবলেমটা বুঝিঃ আমি একটা টেবিলের একটা কলামের ভ্যালুর উপর ডিপেন্ড করে আরেকটা কলামে ডাটা insert করব । এই কাজটা ট্রিগার এর মাধ্যমে খুব সহজেই করা যায় । 

টেবিলের পরিচয়ঃ প্রথমে আমরা  Student_age নামে একটি টেবিল  তৈরি করব । এই টেবিলে সর্বমোট ৪ টি কলাম আছে প্রথম কলাম হচ্ছে Student এর আইডি , ২য়টি হচ্ছে Student এর বয়স , ৩য়টি হচ্ছে Student এর নাম , ৪র্থটি হচ্ছে Student এর টাইপ । টাইপ তিনটি বাচ্চা (KIDS), টিন (TEEN),  প্রাপ্তবয়স্ক(MATURE)    
  1. CREATE TABLE `Student_age` (
  2.       `id` int(11) DEFAULT NULL,
  3.       `age` int(11) DEFAULT NULL,
  4.       `Name` varchar(35) DEFAULT NULL,
  5.       `Student_type` varchar(70) DEFAULT NULL
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ট্রিগার সেটঃ  ট্রিগার নিয়ে কাজ করার পুর্বে ট্রিগার সম্পর্কে জেনে নিতে হবে । ট্রিগারের বেসিক নিতে পারেন এখান থেকে  । এবার আসি আমরা যা করব ট্রিগার নিয়ে ।  আমরা Student_age টেবিলএ একটা ট্রিগার সেট করবো যেটা Student এর age এর উপর ডিপেন্ড করে Student এর টাইপ insert করবে । ট্রিগার যে কাজ করবেঃ
1. Student এর age ১২ এর নিচে থাকলে ট্রিগার Student এর টাইপ insert করবে KIDS
2. Student এর age ১৯ এর নিচে থাকলে ট্রিগার Student এর টাইপ insert করবে TEEN
3. Student এর age ১৯ এর বেশী থাকলে ট্রিগার Student এর টাইপ insert করবে MATURE
কোডঃ

  1.  DELIMITER //
  2.         Create Trigger before_inser_studentage BEFORE INSERT ON student_age FOR EACH ROW
  3.         BEGIN
  4.         IF NEW.age <12 THEN
  5.                      SET NEW.Student_type = "KIDS";
  6.         ELSEIF NEW.age <19  THEN
  7.                     SET NEW.Student_type = 'TEEN';
  8.         ELSEIF NEW.age >19  THEN
  9.                     SET NEW.Student_type = 'MATURE';
  10.         END IF;
  11.         END ;


এবার ডাটা ইনসার্টঃ এবার আসল ডাটা insert এর পালা। আমরা তিনটা কলামে(id, age, Name) ডাটা insert করব । ট্রিগার Student_type কলামে ডাটা insert করবে age কলামের ডাটার উপরে নির্ভর করে । আমরা নিচে তিনটি ডাটা insert করার কমান্ড গুলা run করবঃ



  1. INSERT INTO Student_age(id, age, Name) values(2012331501, 10, 'Rahul');
  2. INSERT INTO Student_age(id, age, Name) values(2012331502, 25, 'TAREQ');
  3. INSERT INTO Student_age(id, age, Name) values(2012331503, 15, 'SHAMIT');


আউটপুট  দেখার পালাঃ  এবার আমরা দেখব আসলে ট্রিগার কাজ করেছে কিনা। সেটা দেখার জন্য আমারা

  1. select * from Student_age
উপরের কমান্ডটি run করব।  Output নিচের picture এ দেওয়া হল । 


ইয়েস 💪ট্রিগার কাজ করেছে।  আমরা insert করেছি id, age, name ট্রিগার automatically - Student_type insert করেছে ।
আরো কিছু প্রবেলেমঃ


1. Student grade update (A+, A, A-, B+, B...etc.) depending on his mark
2. Salary of Employee never goes negative value
......

বানান ভুল আছে মাফ করবেন । 
ধন্যবাদ

Tuesday, 12 March 2019

Wednesday, 5 September 2018

Jaccard similarity and Jaccard distance implementation using node js


To learn more detail about Jaccard similarity and Jaccard  distance please visit Jaccard similarity and Jaccard distance theory
Code:

function union(x,y){
      var z = x.concat(y);// adding two array
      j = [...new Set(z)];//unique elements
     
      return j;
}


function  intersects(a,b){

     return [...new Set(a)].filter(x => new Set(b).has(x));
}



function Jaccard_similarity(x,y){
var u =  union(x,y);// call union function
var i = intersects(x,y); //  call intesects fuction
var x = u.length;
var y = i.length
return y/x;
}

function Jaccard_distance(x,y){
return (1-Jaccard_similarity(x,y))
}

console.log("Jaccard_similarity is "+Jaccard_similarity([0,1,2,5,6],[0,2,3,4,5,7,9]))

console.log("Jaccard_distance is " +Jaccard_distance([0,1,2,5,6],[0,2,3,4,5,7,9]))

Wednesday, 8 August 2018

Dialogflow Custom Payload for Facebook Messenger(Card)

CODE:

{

  "facebook": {

    "attachment": {

      "type": "template",

      "payload": {

        "template_type": "generic",

        "elements": [

          {

            "title": "Tareq chatbot",

            "image_url": "https://image.ibb.co/fUCziz/16716250_1096646177127977_976587736778653479_o.jpg",

            "subtitle": "welcome to tareq blog",

            "default_action": {

              "type": "web_url",

              "url": "https://tarequzzamankhan1994.blogspot.com/"

            },

            "buttons": [

              {

                "title": "Tarequzzaman",

                "type": "postback",

                "payload": "Tareq"

              },

             {

                "type":"web_url",

                "url":"https://www.facebook.com/tareq.khan.5680",

                "title":"Facebook"

              } ,

            {

                "type":"web_url",

                "url":"https://tarequzzamankhan1994.blogspot.com/",

                "title":"Tareq's Blog"

              }   


            ]

          }

        ]

      }

    }

  }

}

Thursday, 26 July 2018

TensorFlow install command





python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.9.0-py3-none-any.whl

Monday, 2 April 2018

Writing data to a text file using Python




Programming Language : Python

list =['a','b','c']
myfile = open('sample.txt', 'w', encoding='utf-8')
for line in list:
    myfile.write(line)
    myfile.write("\n")
myfile.close()

Writing data on a EXCEL Sheet (Using Python)

import xlsxwriter
   
    # Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('spam.xlsx', engine='xlsxwriter',   options={'encoding':'utf-8'})

# Convert the dataframe to an XlsxWriter Excel object.
list.to_excel(writer, sheet_name='Sheet1',encoding='utf-8')
writer.save()
print("done")

Sunday, 21 January 2018

2D transformation On computer graphics implemented using c++



Read Me:
For run this code you must install GLUT in your system
Installation Guide : GLUT Install




Source Code: implemented using C++ and GLUT


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<windows.h>
#include<iostream>
#define PI 3.14159265
using namespace std;
int x1,y1,x2,y2;
int option,m,n;
double sx,sy,x[100],y[100],x_t[100],y_t[100];
int Round(double xx)
{
    return (floor(xx+0.5));
}


void drowAxis()
{
    glBegin(GL_POINTS);
    for(int i=-440; i<=480; i++)
    {
        glVertex2d(0,i);
    }
    for(int i=-520; i<=640; i++)
    {
        glVertex2d(i,0);
    }
    glEnd();
    glFlush();
}

void init()
{
    glClearColor(1.0, 1.0, 1.0,0);
    glColor3f(0.0,0.0,0.0);
    gluOrtho2D(-520,640,-440,480);
}

void line_draw(int x1, int y1,int x2,int y2)
{
    glBegin(GL_LINES);
    glVertex2i(x1,y1);
    glVertex2i(x2,y2);
    glEnd();
    glFlush();
}

void shape()
{
    if(option!=5)
    {
        printf("Choose the shape you want -- \n");
        printf("      Choose 1 for triangle\n");
        printf("      Choose 2 for square\n");
        while(true)
        {
            scanf("%d",&n);
            if(n==1 || n==2)
            {
                break;
            }
            printf("choose 1 or 2\n");
        }
    }
    if(n==1)
    {
        m=3;
        printf("Enter the co-ordinate of the triangle\n");
        for(int i=0; i<3; i++)
        {
            scanf("%lf %lf",&x[i],&y[i]);
        }
    }
    else
    {
        m=4;
        printf("Enter the origin of square\n");
        int temp1,temp2;
        scanf("%d %d",&temp1,&temp2);
        x[0]=temp1;
        y[0]=temp2;

        x[1]=temp1;
        y[1]=temp2+50;

        x[2]=temp1+50;
        y[2]=temp2+50;

        x[3]=temp1+50;
        y[3]=temp2;
    }

}

void translation()
{

    printf("Enter the translation factor tx and ty\n");
    int tx,ty;
    scanf("%d %d",&tx,&ty);
    for(int i=0; i<m; i++)
    {
        x_t[i] = tx + x[i];
        y_t[i] = ty + y[i];/// translation
    }
}

void rotation()
{
    printf("Choose any option of Rotation\n");
    printf("      1- Rotation about the Origin\n");
    printf("      2- Rotation about an Arbitrary Point\n");
    int choosed,pointx=0,pointy=0;
    double angle;
    while(true)
    {
        scanf("%d",&choosed);
        if(choosed==1 || choosed == 2)
        {
            break;
        }
        printf("Enter a valid choose\n");
    }
    if(choosed==2)
    {
        printf("Enter the arbitrary point\n");
        scanf("%d %d",&pointx, &pointy);
    }
    printf("Enter the Angle for Rotation\n");
    scanf("%lf",&angle);
    angle =  (PI / 180)*angle;
    double tempx[100],tempy[100];
    for(int i=0; i<m; i++)
    {
        tempx[i] = x[i]-pointx;
        tempy[i] = y[i]-pointy;
    }

    for(int i=0; i<m; i++)
    {
        x_t[i] = tempx[i]* cos(angle) - tempy[i] * sin(angle);
        y_t[i] = tempx[i]* sin(angle) + tempy[i] * cos(angle);
    }
    for(int i=0; i<m; i++)
    {
        x_t[i] = x_t[i]+pointx;
        y_t[i] = y_t[i]+pointy;
    }
}

void mirror_reflection()
{
    printf("Choose any option of Reflection\n");
    printf("      1- Reflection about the x axis\n");
    printf("      2- Reflection about the y axis\n");
    printf("      3- Reflection about a Line\n");
    int choosed;
    while(true)
    {
        scanf("%d",&choosed);
        if(choosed==1 || choosed == 2|| choosed==3)
        {
            break;
        }
        printf("Enter a valid choose\n");
    }

    if(choosed==1)
    {
        printf("Reflection about the x axis\n");
        for(int i=0; i<m; i++)
        {
            x_t[i] = x[i];
            y_t[i] = y[i]*-1;
        }
    }
    else if(choosed==2)
    {
        printf("Reflection about the y axis\n");
        for(int i=0; i<m; i++)
        {
            x_t[i] = x[i]*-1;
            y_t[i] = y[i];
        }
    }
    else
    {
        printf("Reflection about a Line\n");
        scanf("Enter the start and end points of the line\n");

        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);

        if(x2==x1)
        {
            for(int i=0; i<m; i++)
            {
                x_t[i] = -1*x[i]+x2+x2;
                y_t[i] = y[i];
            }
        }
        else if(y2==y1)
        {
            for(int i=0; i<m; i++)
            {
                x_t[i] =  x[i];
                y_t[i] = -1*y[i]+y2+y2;
            }
        }
        else
        {
            double m_ = (y2-y1)/(x2-x1);
            double b = y1-m*x1;
            for(int i=0; i<m; i++)
            {
                x_t[i] = ((1-(m_*m_))/(m_*m_+1))*x[i] + (2*m_*y[i])/(m_*m_+1) - (2*b*m_)/(m_*m_+1);
                y_t[i] =  (2*m_*x[i])/(m_*m_+1) + ((m_*m_-1)*y[i])/(m_*m_+1) + (2*b)/(m_*m_+1);
            }

        }

    }
}
void scaling()
{
    printf("Choose any option of Scaling\n");
    printf("      1- Scaling about the Origin\n");
    printf("      2- Scaling about a arbitrary point\n");

    int choosed,pointx=0,pointy=0;
    while(true)
    {
        scanf("%d",&choosed);
        if(choosed==1 || choosed == 2)
        {
            break;
        }
        printf("Enter a valid choose\n");
    }
    if(choosed==2)
    {
        printf("Enter the arbitrary point\n");
        scanf("%d %d",&pointx, &pointy);
    }
    printf("Enter the Scaling factor sx and sy\n");
    double sx,sy;
    scanf("%lf %lf",&sx,&sy);
    for(int i=0; i<m; i++)
    {
        x_t[i]= sx * x[i] - sx * pointx + pointx;
        y_t[i]= sy * y[i] - sy * pointy + pointx;
    }
}
void shearing()
{
    printf("Enter the Scaling factor sx and sy\n");
    double sh,sk;
    double tempx[100],tempy[100];
    scanf("%lf %lf",&sh,&sk);
    for(int i=0; i<m; i++)
    {
        tempx[i] = x[i]-x[0];
        tempy[i] = y[i]-y[0];
    }
    for(int i=0; i<m; i++)
    {
        x_t[i] = tempx[i] + sh *  tempy[i];
        y_t[i] = tempy[i] + sk *  tempx[i];/// translation
    }
    for(int i=0; i<m; i++)
    {
        x_t[i] = x_t[i]+x[0];
        y_t[i] = y_t[i]+y[0];
    }
}
void funct()
{
    glClear(GL_COLOR_BUFFER_BIT);
    drowAxis();
    line_draw(x1,y1,x2,y2);
    glColor3f(1.0,0.0,0.0);
    for(int i=0; i<m-1; i++)
    {
        line_draw(round(x[i]), round(y[i]), round(x[i+1]), round(y[i+1]));
    }
    line_draw(round(x[0]), round(y[0]), round(x[m-1]), round(y[m-1]));
    glColor3f(0.0,0.0,1.0);
    for(int i=0; i<m-1; i++)
    {
        line_draw(round(x_t[i]), round(y_t[i]), round(x_t[i+1]), round(y_t[i+1]));
    }
    line_draw(round(x_t[0]), round(y_t[0]), round(x_t[m-1]), round(y_t[m-1]));

}
int main(int argc,char ** argv)
{
    /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    printf("    @@@ 2D Transformation @@@\n");
    printf("Choose The value for transformation\n");
    printf("      1 - for Translation\n");
    printf("      2 - for Rotation\n");
    printf("      3 - for Reflection\n");
    printf("      4 - for Scaling\n");
    printf("      5 - for Shearing\n");
    while(true)
    {
        scanf("%d",&option);
        if(option<0 || option>5)
        {
            printf("Enter a value between 1 to 5\n");
            continue;
        }
        break;
    }
    if(option == 1)
    {
        shape();
        translation();
    }
    else if(option ==2)
    {
        shape();
        rotation();
    }
    else if(option==3)
    {
        shape();
        mirror_reflection();
    }
    else if(option==4)
    {
        shape();
        scaling();
    }
    else if(option==5)
    {
        shape();
        shearing();
    }

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(640,480);
    glutCreateWindow("Translation");
    init();
    glutDisplayFunc(funct);
    glutMainLoop();
}

Friday, 1 December 2017

Difference between Logistic regression and Neural networks


   
1.     1. Usually people use gradient descent in logistic regression to minimize the cost function and on each iteration better values to fit their data. But in Neural Networks you have a different (but not that different) way, we use back propagation to find the parameters (or weights) to our inputs.

2.     2. Simple logistic regression is not appropriate for large complex system (too many feature) but neural networks are much better for a complex nonlinear hypothesis even when feature space is huge. 

Tuesday, 7 November 2017

Cohent Sadalend Line clipping algorithm(Opengl) implementation using c++


Read Me:
For run this code you must install GLUT in your system
Installation Guide : GLUT Install

Algorithm:Cohen Sutherland Line Clipping

Source code :

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<windows.h>
#include<iostream>
#include <chrono>
#include <thread>
#include <bitset>

using namespace std;

int wx1,wx2,wy1,wy2,line_no;
struct points
{
    int x_start;
    int y_start;
    int x_end;
    int y_end;
    int visible;
};
points line_arr[100];
void bit_generate(int a)
{
     bitset<4>binary(a);
     cout << binary << endl;
}
int visible_test(int x,int y)
{
    int res=0;
    if(x < wx1)
    {
        res+=1;
    }
    if(x > wx2)
    {
        res+=2;
    }
    if(y < wy1)
    {
        res+=4;
    }
    if(y > wy2)
    {
        res+=8;
    }
    return res;
}
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */

int Round(double x)
{
    return (floor(x+0.5));
}
void dda(double x_start, double y_start, double x_end,double y_end)
{
    double dx=(x_end-x_start);
    double dy=(y_end-y_start);
    double steps;
    float xInc,yInc,x_=x_start,y_=y_start;

    /* Find out whether to increment x or y */
    steps= max(abs(dx),abs(dy));

    xInc=dx/(float)steps;
    yInc=dy/(float)steps;



    /* Plot the first point */
    glBegin(GL_POINTS);
    glVertex2d(x_,y_);
    glEnd();
    glFlush();
    int k;

    /* For every step, find an intermediate vertex */

    for(k=0; k<steps; k++)
    {
        x_+=xInc;
        y_+=yInc;
        /* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
        glBegin(GL_POINTS);
        glVertex2d(Round(x_), Round(y_));
        glEnd();
        glFlush();
    }
}
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
void dda1(double x_start, double y_start, double x_end,double y_end)
{
    double dx=(x_end-x_start);
    double dy=(y_end-y_start);
    double steps;
    float xInc,yInc,x_=x_start,y_=y_start;

    /* Find out whether to increment x or y */
    steps= max(abs(dx),abs(dy));

    xInc=dx/(float)steps;
    yInc=dy/(float)steps;



    /* Plot the first point */

    int k;

    /* For every step, find an intermediate vertex */

    for(k=0; k<steps; k++)
    {

        /* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
        int xx = round(x_);
        int yy = round(y_);
        int a = visible_test(xx,yy);
        if(a != 0)
        {
            glBegin(GL_POINTS);
            glVertex2d(xx, yy);
            glEnd();
            glFlush();
        }
        x_+=xInc;
        y_+=yInc;
    }
}
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
void init()
{
    glClearColor(1.0, 1.0, 1.0,0);
    glColor3f(0.0,0.0,0.0);
    gluOrtho2D(-520,640,-440,480);
}

void input()
{
    printf("Enter the window co-ordinate x_min and y_min\n");
    scanf("%d %d",&wx1,&wy1);
    printf("Enter the window co-ordinate x_max and y_max\n");
    while(true)
    {
        scanf("%d %d",&wx2,&wy2);
        if(wx1>=wx2 || wy1>=wy2)
        {
            printf("Enter valid end points\n");
        }
        else
        {
            break;
        }
    }
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    printf("How many line\n");
    scanf("%d",&line_no);
    printf("x1 y1 x2 y2\n");
    for(int i=0; i<line_no; i++)
    {
        scanf("%d %d",&line_arr[i].x_start, &line_arr[i].y_start);

        int a = visible_test(line_arr[i].x_start,line_arr[i].y_start);
         bit_generate(a);
        scanf("%d %d",&line_arr[i].x_end, &line_arr[i].y_end);

        int b = visible_test(line_arr[i].x_end,line_arr[i].y_end);
        bit_generate(b);
        int p = a&b;
        if(a==0 && b==0)
        {
            line_arr[i].visible = 2;///completely inside
        }
        else if(p==0)
        {
            line_arr[i].visible = 1;///clipping candidate
        }
        else
        {
            line_arr[i].visible = 0;///completely outside
        }
    }
}
void line_clip()
{
    glClear(GL_COLOR_BUFFER_BIT);

    dda(wx1,wy1,wx1,wy2);
    dda(wx1,wy2,wx2,wy2);
    dda(wx2,wy2,wx2,wy1);
    dda(wx2,wy1,wx1,wy1);


    for(int i=0; i<line_no; i++)
    {
        if(line_arr[i].visible==0)
        {
            glColor3f(1.0,0.0,0.0);///set red color which is completely outside
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end); /// line drawing Algorithm

        }
        else  if(line_arr[i].visible==1)
        {
            glColor3f(0.0,0.0,1.0);/// set blue color for clipping candidate
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);

        }
        else
        {
            glColor3f(0.0,1.0,0.0);
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);

        }
    }
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */

    for(int i=0; i<line_no; i++)
    {
        if(line_arr[i].visible==0)///completely outside line
        {
            glColor3f(1.0,1.0,1.0);///set white color
             std::this_thread::sleep_for(std::chrono::milliseconds(1000));///sleep for 1 sec

            dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);

        }
    }

    for(int i=0; i<line_no; i++)
    {
        if(line_arr[i].visible==1)/// clipping candidate
        {
            glColor3f(1.0,1.0,1.0);///set white color
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            dda1(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);
        }
    }
   glColor3f(0.0,0.0,0.0);///set default black color

}

int main(int argc,char ** argv)
{
    /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    glutInit(&argc,argv);
    input();
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(640,480);
    glutCreateWindow("Cohen-Sutherland Line Clipping");
    init();
    glutDisplayFunc(line_clip);
    glutMainLoop();
     /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
}

Saturday, 21 October 2017

UVA 417 - Word Index c++ implementation


Problem Type: map (Java TreeMap)
Problem Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=358&mosmsg=Submission+received+with+ID+20216008

Source code :

 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int>mp;
void load()
{
    queue<string> q;
    string s = "";
    int cou =1;
    for(int i=97; i<=122; i++)
    {
        char c = i;
        s+=c;
        mp[s]=cou;
        q.push(s);
        cou ++;
        s="";
    }
    while(!q.empty())
    {
        string s = q.front();
        q.pop();
        for(int i=97; i<=122; i++)
        {
            char c = i;
            int len = s.length();
            if(s[len-1]<c)
            {
                string m = s+c;
                if(m.length()>5)
                    break;
                mp[m]=cou;
                q.push(m);
                cou ++;
            }
        }
    }
}
int main()
{
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
    load();
    string s ;
    while(cin >>s)
    {
     cout << mp[s] << endl;
    }
}

Friday, 20 October 2017

11988 - Broken Keyboard (a.k.a. Beiju Text) c++ implementation

Problem Type: C++ STL list (Java LinkedList)


Implementation:

#include<bits/stdc++.h>
using namespace std;
 /***
    Tarequzzaman Khan
    CSE'12
    2012331514
    */
int main()
{
    string s;
    while(cin >>s)
    {

        int tag =0;
        string sub="",output="";
        for(int i=0; i<s.length(); i++)
        {
            if(s[i]=='[')
            {
                tag =1;
                if(sub.compare("")!=0)
                {
                    output=sub + output;
                    sub ="";
                }
            }
            else if(s[i]==']')
            {
                tag = 0;
                if(sub.compare("")!=0)
                {
                    output=sub + output;
                    sub ="";
                }
            }
            else if(tag==1)
            {
                sub += s[i];
            }
            else
            {
                output += s[i];
            }
        }
        if(sub.compare("")!=0)
        {
         output = sub + output;
        }
        cout << output << endl;
    }
}

Monday, 16 October 2017

11362 - Phone List(C++ implementation)

Problem Type: Trie Data Structure


#include<bits/stdc++.h>
using namespace std;

struct Node
{
    map<char, Node*> a;
    int flag;

    Node()
    {
        flag = 0;
    }
};

Node* root;
void insert_text(Node *x, string s)
{
    for(int i = 0; i < s.size(); i++)
    {
        if(x->a.count(s[i]) == 0)
        {
            x->a[ s[i] ] = new Node;
        }
        x = x->a[ s[i] ];
    }
    x->flag++;
}

int search_text(Node* x, string s)
{
    int len = s.length();
    int i=0;
    int flag_count =0;
    while (i<len)
    {
        char c = s[i];
        if(x->a.count(s[i]) != 0)
        {
            x = x->a[s[i]];
            if(x->flag==1)
            {
                flag_count++;
            }
            else if(x->flag>1)
            {
                return 0;
            }
        }
     
        i++;
    }
    if(flag_count>1)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main()
{
    int test;
    scanf("%d",&test);
    for(int i=0; i<test; i++)
    {
        int how_number,k=0;
        string str[10003];
        root = new Node;
        scanf("%d",&how_number);
        int j;
        for(j=0; j<how_number; j++)
        {
            string number;
            cin >> number;
            insert_text(root,number);
            str[j]=number;

        }
        int mm=0;
        for(int k=0; k<j; k++)
        {
            int p = search_text(root,str[k]);
            if(p==0)
            {
                mm=1;
                break;
            }

        }
        if(mm==0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }

}

Tuesday, 15 August 2017

10878 - Decode the tape C++ implementation

#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
    string s ;
    getline(cin,s);
    while(getline(cin,s))
    {
        if(s[0]=='-')
            break;
        int m=0;
        int sum=1;
        for(int i=9; i>=1; i--)
        {
            if (s[i]!='.')
            {
                if (s[i]=='o')
                {
                    m += sum;
                }
                sum *= 2;
            }
        }
        printf("%c",m);
    }
}

Thursday, 27 July 2017

Bubble Sort using link list in java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

/**
 *
 * @author Tarequzzaman
 */

import java.util.Scanner;

public class Link_Sort
{
    node head,current;

    class node
    {
        int data;
        node next;
        node(int data)
        {
            this.data = data;
            next = null;
        }
    }


    void insert(int data)
    {
        node new_node = new node(data);
        if(head==null)
        {
            head = new_node;
            current = new_node;
        }
        else
        {
            current.next = new_node;
            current = current.next;
        }

    }

    void display()
    {
        node tnode = head;
        while (tnode != null)
        {
            System.out.print(tnode.data + " ");
            tnode = tnode.next;
        }
        System.out.println("");
    }


    void sort(int count)
    {
        node current = head;
        for(int i=0; i<count; i++)
        {
            node next_node= current.next;
            for(int j=i; j<count-1; j++)
            {
                if(current.data > next_node.data)
                {
                    int temp = current.data;
                    current.data = next_node.data;
                    next_node.data = temp;
                }
                next_node = next_node.next;
            }
            current= current.next;
        }
    }


    public static void main(String args[])
    {
        int count=0;
        Link_Sort s = new Link_Sort();
        System.out.println("Give input enter 0 to break");
        while (true)
        {
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            if (i == 0)
            {
                break;
            }
            count++;
            s.insert(i);
        }
        System.out.println("Current Output");
        s.display();
        System.out.println("Sorted Output");
        s.sort(count);
        s.display();
    }

}

Friday, 14 July 2017

Reading data from a binary file (c++) implementation

#include<bits/stdc++.h>
#include <fstream>
#include <vector>

using namespace std;


int main()
{
    vector<unsigned char> bytes;

    ifstream file1("C:\\Users\\Tarequzzaman\\Desktop\\decoded.bin", ios_base::in | ios_base::binary);
    unsigned char ch = file1.get();
    while (file1.good())
    {
        bytes.push_back(ch);
        ch = file1.get();
    }
    size_t size = bytes.size();
    cout  << size << endl;
    return 0;
}

Friday, 5 May 2017

A* search implementation in c++





CODE :
#include<bits/stdc++.h>
#define mx 1000
using namespace std;
map <string,int>mymap;
map<int,string>mymap1;
int N,E,mat[mx][mx],start,end1,par[mx],cost[mx];


struct node
{
    int node_no ;

    int huristic_value;

    bool operator<(const node& target) const
    {
        return huristic_value > target.huristic_value;
    }
};
node arr[mx];



void  Printpath(int V)
{
    if(start==V)
    {
        string s = mymap1[start];
        cout << s;
        return;
    }
    else if(par[V]==0)
    {
        printf("No path exist\n");
    }
    else
    {
        Printpath(par[V]);
        string s = mymap1[V];
        cout <<"-->" << s ;
    }
}

int GBFS(int ux)
{
    node current;
    priority_queue<node>q;
    q.push(arr[ux]);
    par[ux]=0;
    cost[ux] = 0;

    while(!q.empty())
    {
        current = q.top();
        int vx = current.node_no;
        q.pop();
        if(vx == end1)
            return vx;
        for(int i=1; i<=N; i++)
        {


            if(mat[vx][i]!=-1)
            {
                cost[i]= cost[vx] + mat[vx][i];
                arr[i].huristic_value+=cost[i];
                q.push(arr[i]);
                if(par[vx]!=i)
                    par[i] = vx;

            }
        }
    }
}
int main()
{
    string s;
    int hv;
    cout <<"Enter the number of Node : ";
    cin >> N;

    cout << "N H" << endl;///N for node ;H for huristic value
    for(int i=1; i<=N; i++)
    {
        cin >> s >> hv;
        mymap[s] = i;
        mymap1[i] = s;
        arr[i].node_no = i;
        arr[i].huristic_value = hv;
    }

    cout <<"Enter the number of Edge : " ;
    cin >> E ;


    string source, des;
    int g_n;


    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            mat[i][j] = -1;                   ///Matrix initialization
        }
    }


    for(int i = 1; i <= E; i++)
    {
        cin >> source >> des >> g_n;
        int x = mymap[source];               ///Input the edges :)
        int y = mymap[des];
        mat[x][y] = g_n;
        mat[y][x] = g_n;
    }


    for(int i=1; i<=N; i++)
    {
        par[i]=0;
    }

    cout << "enter the start and goal node" <<endl;
    cin >> source >> des;
    start = mymap[source];
    end1 = mymap[des];
    int path =   GBFS(start);
    Printpath(end1);
    cout << endl << "Path cost : "<<cost[path] << endl;

}

INPUT : 
8
A 336
T 329
S 253
R 193
F 176
C 160
P 100
B 0
9
A T 118
A S 140
S R 80
S F 99
R C 146
R P 97
F B 211
C P 138
P B 101
A B

OUTPUT :
A-->S-->R-->P-->B
Path cost : 418

Greedy Best First Search(GBST) c++



CODE :

#include<bits/stdc++.h>
#define mx 1000
using namespace std;
map <string,int>mymap;
map<int,string>mymap1;
int N,E,mat[mx][mx],start,end1,par[mx],cost[mx];

struct node
{
    int node_no ;
    int huristic_value;

    bool operator<(const node& target) const
    {
        return huristic_value > target.huristic_value;
    }
};
node arr[mx];

void  Printpath(int V)
{
    if(start==V)
    {
        string s = mymap1[start];
        cout << s;
        return;
    }
    else if(par[V]==0)
    {
        printf("No path exist\n");
    }
    else
    {
        Printpath(par[V]);
        string s = mymap1[V];
        cout <<"-->" << s ;
    }
}

int GBFS(int ux)
{
    node current;
    priority_queue<node>q;
    q.push(arr[ux]);
    par[ux]=0;
    cost[ux] = 0;

    while(!q.empty())
    {
        current = q.top();
        int vx = current.node_no;
        q.pop();
        for(int i=1; i<=N; i++)
        {
            if(mat[vx][i]!=-1)
            {
                cost[i]= cost[vx] + mat[vx][i];
                q.push(arr[i]);
                if(par[vx]!=i)
                    par[i]=vx;
                if(i == end1)
                {
                    return cost[i];
                }
            }
        }
    }
}
int main()
{
    string s;
    int hv;
    cout <<"Enter the number of Node : ";
    cin >> N;

    cout << "N H" << endl;///N for node ;H for huristic value
    for(int i=1; i<=N; i++)
    {
        cin >> s >> hv;
        mymap[s] = i;
        mymap1[i] = s;
        arr[i].node_no = i;
        arr[i].huristic_value = hv;
    }

    cout <<"Enter the number of Edge : " ;
    cin >> E ;

    string source, des;
    int g_n;

    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            mat[i][j] = -1;                   ///Matrix initialization
        }
    }

    for(int i = 1; i <= E; i++)
    {
        cin >> source >> des >> g_n;
        int x = mymap[source];               ///Input the edges :)
        int y = mymap[des];
        mat[x][y] = g_n;
        mat[y][x] = g_n;
    }


    for(int i=1; i<=N; i++)
    {
        par[i]=0;
    }

    cout << "enter the start and goal node" <<endl;
    cin >> source >> des;
    start = mymap[source];
    end1 = mymap[des];

    int path=   GBFS(start);
    Printpath(end1);
    cout << endl << "Path cost : "<<path << endl;

}


INPUT : 
8
A 336
T 329
S 253
R 193
F 176
C 160
P 100
B 0
9
A T 118
A S 140
S R 80
S F 99
R C 146
R P 97
F B 211
C P 138
P B 101
A B

OUTPUT :
A-->S-->F-->B
Path cost : 450

ট্রিগার এর মাধ্যমে ডাটা ইনসার্ট - insert data using Database Trigger (Mysql)

সর্বপ্রথম আমরা প্রবলেমটা বুঝিঃ আমি একটা টেবিলের একটা কলামের ভ্যালুর উপর ডিপেন্ড করে আরেকটা কলামে ডাটা insert করব । এই কাজটা ট্রি...