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

}

Thursday, December 4, 2014

Find Largest Palindrome in the given string

Find the largest palindrom present in the string

Input: abcdefgmalayalam

Output: malayalam


Solution


public class LongestPalindrome {

private static boolean isPalindrome(String str) {

int length = str.length();

for (int i = 0; i < ((length / 2) + 1); i++) {

if (str.charAt(i) != str.charAt(length - i - 1))
return false;

}
return true;
}

private static String largestPalindrome(String str) {

int length = str.length();

String largestPalindrome = "";

for (int i = 0; i < length - 1; i++) {

for (int j = length - 1; j >= 0 && j != i; j--) {

String subStr = str.substring(i, j + 1);

if (isPalindrome(subStr)) {
largestPalindrome = largestPalindrome.length() < subStr
.length() ? subStr : largestPalindrome;
}
}

}

return largestPalindrome;

}

public static void main(String[] args) {
// Check for Palindrome
System.out.println(isPalindrome("dog"));
System.out.println(isPalindrome("malayalam"));
System.out.println(isPalindrome("aaaabbb"));

// Check largestPalindrome

System.out.println(largestPalindrome("abcdefgmalayalam"));

}


}

Wednesday, October 22, 2014

Find the missing elements in a sequence using XOR

Find the missing number in a sequence using XOR method.

Input: 1,2,3,4,5,6,7,8,9,11,12,13,14,15

Output: 10


Solution: 

Java




int[] arr = new int[]{1,2,3,4,5,6,7,8,9,11,12,13,14,15};
int sum = arr.length*(arr.length+1)/2;
// sum=120;
//System.out.println(sum);
int val = sum;
for(int i=0;i val = val^arr[i];
}
System.out.println(val^sum);

Wednesday, March 3, 2010

Distribution lists in Zimbra

Collect the Distribution lists/groups from your zimbra server to a file.

---------------------------------------------------------------------
#!/bin/bash
#Declare name of output file in the variable below:
file=dscripttest;
echo "" > $file;
for line in `zmprov gadl`
do
name=$line;
zmprov gdl $line|grep 'zimbraMailForwardingAddress:' > /tmp/tmp2.txt;
sed 's/zimbraMailForwardingAddress://g' /tmp/tmp2.txt > /tmp/tmp.txt;
dlists='';
for line1 in `cat /tmp/tmp.txt`;
do
dlists=$line1','$dlists;
done
echo $dlists;
echo $name':'$dlists >> $file;
sed -i '/^$/d' $file;
echo "">/tmp/tmp2.txt;
echo "">/tmp/tmp.txt;
done

Tuesday, March 2, 2010

getAccounts from Zimbra

Script to get account details from your zimbra mail server along with aliases. In our scenario, we did this for creating accounts in new server based on the information from the old server. The ouput file will be in the format below.
-------------------------------------
Email address:alias1,alias1
Email address2:alias1

-------------------------------------

Remarks: In this script, it will take only accounts in the active and locked state.

#!/bin/bash
#Declare name of output file in the variable below:
file=accountlist;
echo "" > $file;

for line in `zmprov gaa`;
do
status=`zmprov ga $line|grep 'zimbraAccountStatus:'|cut -d : -f 2`;
if [ $status == "active" ]||[ $status == "locked" ];
then
name=$line;
aliases='';
zmprov ga $name|grep 'zimbraMailAlias' > tmp3.txt;
for line1 in `sed 's/zimbraMailAlias://g' tmp3.txt`;
do
aliases=$line1','$aliases;
done
echo $name:$aliases >> $file;
sed -i '/^$/d' $file;
echo "" > tmp3.txt;
fi
done

Backing up in Scalix

Backing up a user folder:

Setup a cron to execute the command below at every night.
sxmboxexp -u "username" -a /path_to_file.mbox
Using crontab:
crontab -e
0 0 * * * sxmboxexp -u "username" -a /path_to_file.mbox
#The above command wil run at every night 12 AM.

To restore the mailbox using the back up,use the command below.

sxmboximp -u "John Doe" -a "/path_to_file.mbox" -s --listlevel folder

----------------------------------------------------------------------------------------------
For automatc back up of entire scalix directory.Refer the link below.

http://www.scalix.com/wiki/index.php?title=HowTos/AutoBackupScript