Thursday, July 2, 2015

Finding the Creation Date of a file in Linux


Creation date of a file is not directly available using the popular 'ls' command . ls displays the last modified date of the file instead of creation date/time.

Then How to get the creation date of a file. Here is the solution.



  • Get the Inode value of the File using the command below

               ls -i filename


  • Use 'mount' command to find out the disk where the file resides



  • Use the Command below with above details to get File system Struct information


        debugfs -R 'stat <1045343>' /dev/mapper/lg_volume_2


Example output is given below:


Tuesday, June 30, 2015

Check whether two strings are Anagrams

Anagrams, Any word or phrase that exactly reproduces the letters in another order is an anagram (Wiki).

Following programs shows two methods for checking two words are Anagrams or not.

Approach


  • Check strings for equality after sorting the strings in char Array.
  • Iteratively check each Character in string1 is present in string2




Example :

Input : magic, gicma
Output : true

Input : magician, gicma
Output : false



Java Source



package string.ops;


import java.util.Arrays;




public class AnagramChecker {



public static boolean sortedChecker(String str1, String str2) {

if(str1.length() != str2.length())
return false;

char[] string1 = str1.toLowerCase().toCharArray();
char[] string2 = str2.toLowerCase().toCharArray();

Arrays.sort(string1);
Arrays.sort(string2);

return Arrays.equals(string1, string2);



}


public static boolean iterativeChecker(final String str1, final String str2) {

if(str1.length() != str2.length())
return false;

char[] string1 = str1.toLowerCase().toCharArray();
StringBuilder string2 = new StringBuilder(str2.toLowerCase());

for(char ch: string1) {

int index = string2.indexOf(String.valueOf(ch));
if(index==-1)
return false;
string2.deleteCharAt(index);
}

return true;
}


public static void main(String[] args) {

System.out.println("Iteative Checking:");

System.out.println(iterativeChecker("abcs","abcd"));
System.out.println(iterativeChecker("","abcd"));
System.out.println(iterativeChecker("dcba","abcd"));
System.out.println(iterativeChecker("",""));

System.out.println("Sorted Checking:");

System.out.println(sortedChecker("",""));
System.out.println(sortedChecker("magic","gicma"));
System.out.println(sortedChecker("magician","gicma"));
}


}

Sunday, June 28, 2015

Draw triangle of stars for the given number

Draw triangle with star/asterisk symbol and base of triangle is supplied as input

Input: 10

Output:
           *
          * *
         * * *
        * * * *
       * * * * *
      * * * * * *
     * * * * * * *
    * * * * * * * *
   * * * * * * * * *
  * * * * * * * * * *


Solution


public class StarTriangle {

private static void drawStarPyramid(int number) {

for(int i=1;i<=number;i++) {

for(int j=number;j>=i;j--) {
System.out.print(" ");
}

for(int k=1;k<=i;k++) {
System.out.print(" *");
}
System.out.println();

}
}

public static void main(String[] args) {

drawStarPyramid(10);

}


}

Tuesday, April 28, 2015

Highest prime number up to the given number

Find the highest prime number up to the number.

Input: 20

Output: 19 [highest prime number below 20]



Solution: 



public class HighestPrime {


public static int HighestPrime(int number) {
int value = 1;

if (number <= 2)
return 1;
for (int i = 2; i <= number; i++) {
if (i == number) {
value = number;
break;
}
if (number % i == 0) {
i = 2;
number--;
}
}

return value;

}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(HighestPrime(20));
}

}