Spiga

Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Playing Around With Frequently Changing String Tokenizer


Playing Around With Frequently Changing String Tokenizer

Recently I was working on one of my Project Module. I had to use some data from the other module and they were sending this in the form of String Tokens, like "element_1;element_2;element_3;element_4;". So, as normal I did the normal tokenize code and break this tokens to use the values. Now they changed the token order, so I would have to do all the rework.
After that I came up with some solutions so that my code rework can be minimised even if the  token orders are changed. So I did the following:
1) Add all the tokens in the List. Let’s say, in the tokenizer parser method, just add all the token in the List.
2) Do not write any core logic in the token parser method.
3) Now create a enum or any such structure which will hold the token position. In my case I use the enum.
                e.g.
public enum ElementPositionEnum {
       ELEMENT_1(0), ELEMENT_2(1), ELEMENT_3(2), ELEMENT_4(3);
       private int position;
       ElementPositionEnum(int position) {
              this.position = position;
       }
       public int getPosition() {
              return this.position;
      }
}
4) Now while creating the object or setting the value to my object, just fetch the elements from the List based upon the Enum position.
                E.g. If I have to fetch the value,  I will do it like this:
                String myVal = List.get(MyElementPositionEnum.ELEMENT_1.getPosition());
5) So, even if the tokens sequence is changed, then  we only need to change the enum's value. Rest of the code will be same.
Java Code for Same :
public class TokenizerParser {
       public TokenElementTO parseToken(String token)
       {
              List listOfTOken = new ArrayList();
              String[] tokens = token.split(";");
              for(String val : tokens)
              {
                     listOfTOken.add(val);
              }
              return createTokenElementObject(listOfTOken);
       }
        private TokenElementTO createTokenElementObject(List listOfTOken)           {
              TokenElementTO to = new TokenElementTO();
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_1.getPosition())
                     to.setElement_1(listOfTOken.getElementPositionEnum.ELEMENT_1.getPosition()));
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_2.getPosition())
                     to.setElement_2(listOfTOken.get(ElementPositionEnum.ELEMENT_2.getPosition()));
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_3.getPosition())
                     to.setElement_3(listOfTOken.get(ElementPositionEnum.ELEMENT_3.getPosition()));
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_4.getPosition())
                     to.setElement_4(listOfTOken.get(ElementPositionEnum.ELEMENT_4.getPosition()));
               return to;
       }
 
       public static void main(String[] args)
       {
              TokenizerParser main = new TokenizerParser();
              String tokens = "AAA;BBB;CCC;DDDD";
              main.parseToken(tokens);
       }
}
 
public class TokenElementTO {
 
       private String element_1;
       private String element_2;
       private String element_3;
       private String element_4;
 
       public String getElement_1() {
              return element_1;
       }
       public void setElement_1(String element_1) {
              this.element_1 = element_1;
       }
       public String getElement_2() {
              return element_2;
       }
       public void setElement_2(String element_2) {
              this.element_2 = element_2;
       }
       public String getElement_3() {
              return element_3;
       }
       public void setElement_3(String element_3) {
              this.element_3 = element_3;
       }
       public String getElement_4() {
              return element_4;
       }
       public void setElement_4(String element_4) {
              this.element_4 = element_4;
       }
}


ALGORITHM TO DETERMINE STRING CONTAINS UNIQUE CHARACTER

Implement an algorithm to determine if a string has all unique characters. With and without using extra additional data structures

/**
* Implement an algorithm to determine if a string has all unique characters.
* With and without using extra additional data structures?
*
@author Sonu Mishra
*/
public class StringContainsUniqueCharacter {
/**
* Without using extra DS.
*  Ignoring int variable. Time complexity is
* O(n), where n is the length of the string, and space complexity is O(n).
* We can reduce our space usage a little bit by using a bit vector.We will
* assume, in the below code, that the string is only lower case ‘a’ through
* ‘z’.This will allow us to use just a single int
*
@param str: String to check
@return : true/false
*/

Swapping the Variables Without Using Third Variables

Swapping the Variables Without Using Third Variables Using Java

In this post, we will see, how to swap the variables. There are different ways to swap the variables. The major swap algorithms are:
1) Swapping using the third varaibles.
2) Swapping using the Bitwise Operator. This is only for the Integer. This doesn't use the third variables.
3) Swapping using the Arithmetic Operations. This is also, only for the Integer. This also, doesn't use the
    third variables.
4) Swapping the Strings without using the third variables. This is little bit tricky. The logic is similar to the
    above one, i.e. Arithmetic Operations. But the Strings are immutable in the Java, so, externally we are not
    using the third variables, but internally, this uses the third variables.

Below, we have the Java Program to discuss all the above methods in details.

/**
* This class will reverse the two variables.
* Here we will see how to swap the variables using or without using the third variables.
* @author Sonu Mishra
*/
public class SwapVariables {
/**
* Swapping using the third variable and without the third variables.
* Swap two variables using the temporary variables or the third variable.
* @param a The first variable.
* @param b The Second variable.
*/

Basic TreeNode Structure in Java using Generics

Basic TreeNode Structure Using Generics

This is the basic block for creating the binary tree. We generally call this block or the structure as a Node. So this is how a single node is created. This in itself is a tree and when we pile the number of nodes, it become a big tree.

Here is the code, to create the TreeNode.

/**
* Basic Structure of the Tree. Or the Single unit of a tree.
* @author Sonu Mishra
* @param Type of the Node for the tree. Like. String, Integer
*/
TreeNode< E extends Comparable<? super E>>
{
public TreeNode< E > left;
public TreeNode< E > right;
E value;
public TreeNode(TreeNode< E > left, TreeNode< E > right, E value) {
this.left = left;
this.right = right;
this.value = value;
}
/**
* If a node doesn't have left and right child, it is leaf node.
* @param node check whether this node is leaf node or not.
* @return either true or false
*/

JAVA WAY TO WRITE TREE

JAVA WAY TO WRITE BINARY SEARCH TREE

A binary search tree (BST),is a node-based data structure in which each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node.BST is also known as an ordered binary tree.
BST
Here is the Code to create a binary search tree in java.

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Stack;
/**
* Creates a Binary Search Tree.
* @author Sonu Mishra
*/
public class BinarySearchTree<E extends Comparable<? super E>>
{
private TreeNode<E> root = null;
public BinarySearchTree() {
root = new TreeNode(null, null, null);
}
public BinarySearchTree(E value) {
root = new TreeNode(null, null, value);
}
public void add(E value) {
addBefore(value, this.root);
}