Friday, 24 April 2020

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;
    }


Thursday, 20 June 2019

Send Multipart file in resttemplate Java Spring

import java.io.File;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String[] args) throws IOException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
FileSystemResource value = new FileSystemResource(new File("D://test.png"));
map.add("file", value);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("http://localhost/api/v1/users/avatar", HttpMethod.POST, requestEntity, String.class);
}
}

ref: https://gist.github.com/ansidev/5816b8b3108c30b5279f5fec67506798

links for Data Structure

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