Friday, 24 April 2020

Breadth First Search

import java.util.*;

public class BFS {

    public BFS() {

    }

    void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
         adj.get(v).add(u);
     }

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<4;i++){
            adj.add(new ArrayList<>());
        }
        BFS g = new BFS();
        g.addEdge(adj,0, 1);
        g.addEdge(adj,0, 2);
        g.addEdge(adj,1, 2);
        g.addEdge(adj,2, 0);
        g.addEdge(adj,2, 3);
        g.addEdge(adj,3, 3);
        g.BFS(2,adj);

    }

    void BFS(int v,ArrayList<ArrayList<Integer>> adj){
        LinkedList<Integer> queue = new LinkedList<>();
        queue.add(v);

        boolean[] visited = new boolean[adj.size()];
        visited[v]=true;

        while (!queue.isEmpty()){
            Integer val = queue.poll();
            System.out.println(val);
            ArrayList<Integer> list = adj.get(val);
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext()){
                Integer i = iterator.next();
                if(!visited[i]){
                    visited[i] = true;
                    queue.add(i);
                }
            }
        }
    }
}

Adjacent Matrix Graph

import java.util.ArrayList;

public class AdjecentMatrix {
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<5;i++){
            adj.add(new ArrayList<>());
        }

        addEdge(adj, 0, 1);
        addEdge(adj, 0, 4);
        addEdge(adj, 1, 2);
        addEdge(adj, 1, 3);
        addEdge(adj, 1, 4);
        addEdge(adj, 2, 3);
        addEdge(adj, 3, 4);
        printGraph(adj);
    }

    static void addEdge(ArrayList<ArrayList<Integer>> adj,int u, int v){
        adj.get(u).add(v);
        adj.get(v).add(u);
    }

    static void printGraph(ArrayList<ArrayList<Integer>> adj){
        for(int i=0;i<adj.size();i++){
            System.out.println("adjency list of vertex");
            for(int j=0;j<adj.get(i).size();j++){
                System.out.print("->"+adj.get(i).get(j));
            }
            System.out.println();
        }
    }
}
import java.util.ArrayList;

public class AdjecentMatrix {
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<5;i++){
            adj.add(new ArrayList<>());
        }

        addEdge(adj, 0, 1);
        addEdge(adj, 0, 4);
        addEdge(adj, 1, 2);
        addEdge(adj, 1, 3);
        addEdge(adj, 1, 4);
        addEdge(adj, 2, 3);
        addEdge(adj, 3, 4);
        printGraph(adj);
    }

    static void addEdge(ArrayList<ArrayList<Integer>> adj,int u, int v){
        adj.get(u).add(v);
        adj.get(v).add(u);
    }

    static void printGraph(ArrayList<ArrayList<Integer>> adj){
        for(int i=0;i<adj.size();i++){
            System.out.println("adjency list of vertex");
            for(int j=0;j<adj.get(i).size();j++){
                System.out.print("->"+adj.get(i).get(j));
            }
            System.out.println();
        }
    }
}

Monday, 2 March 2020

Max Sum Contiguous Subarray

public class Solution {
    public int maxSubArray(final List<Integer> A) {
        int tempSum=0;
        int maxSum = Integer.MIN_VALUE;
            for(int j=0;j<A.size();j++){
                tempSum +=A.get(j);
                if(tempSum>maxSum){
                    maxSum = tempSum;
                }
                if(tempSum<0){
                    tempSum=0;
                }
               
            }
            return maxSum;
    }
}

Monday, 17 February 2020

NoRepositoryBean annotation spring boot

import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
 
import java.io.Serializable;
import java.util.Optional;
 
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
 
    Optional<T> deleteById(ID id);
}


Annotate the created interface with the @NoRepositoryBean annotation. This ensures that Spring Data JPA won’t try to create an implementation for the BaseRepository interface.

Thursday, 16 January 2020

Winston logger with file name and time stamp nodejs

logger.js

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;
const path = require('path');

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} [${label}] ${level}: ${message}`;
});
const getLabel = function(callingModule) {
    const parts = callingModule.filename.split('/');
    return path.join(parts[parts.length - 2], parts.pop());
  };

const logger = (callingModule)=> createLogger({
  format: combine(
    label({ label: getLabel(callingModule) }),
    timestamp(),
    myFormat
  ),
  transports: [new transports.Console()]
});

module.exports = {
    logger:logger
}

use as below:

const logger = require('../util/logger').logger(module);
logger.info('logger file..');

//util/logger is my file path of logger.js

output:

2020-01-16T08:28:41.893Z [controller/shop.js] info: logger file..

Monday, 25 November 2019

Generic function for Post Data with some headers in spring

 public <T,K> K postApiCall(T t, HttpHeaders headers, String url, Class<K> resposne){
        try {
            RestTemplate restTemplate = new RestTemplate();
            headers.add("Content-Type","application/json");
            HttpEntity<T> entity = new HttpEntity<T>(t, headers);
            ResponseEntity<K> result = restTemplate.exchange(url, HttpMethod.POST, entity, resposne);
            System.out.println(result.getBody());
            return result.getBody();
        }catch (Exception ee){
            ee.printStackTrace();
        }
        return null;
    }

Thursday, 15 August 2019

Check linkedList palindrom


 Steps:
1. break list in 2 parts
2. reverse second list
3. compare list

void checkLinkedListpalindrom(Node node) {
        Node p = node;
        Node q = node;
        Node start = node;
        Node secondstart=null;
        while(true) {
            p = p.next.next;
            if(p==null) {
                secondstart = q.next;
                q.next = null;
                break;
            }
            if(p.next==null) {
                secondstart = q.next.next;
                q.next = null;
                break;
            }
           
            q = q.next;
       
        }
        secondstart =  reverse(secondstart);
        System.out.println(compareList(start, secondstart));
       
    }
   
    boolean compareList(Node n1, Node n2) {
        boolean flag = true;
        while(null!=n1 || null!=n2) {
            if(n1.value==n2.value) {
                n1 = n1.next;
                n2 = n2.next;
            }else {
                flag = false;
                break;
            }
        }
        return flag;
    }
   
    Node reverse(Node node) {
        Node pre = null;
        Node current =node;
        while(null!=current) {
            Node temp = current.next;
            current.next = pre;
            pre = current;
            current = temp;
           
        }
        return pre;
    }


links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...