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

Wednesday, January 27, 2010

Testing Load Performance of MTA [eg:Sendmail Vs Postfix]

Philosophy : To compare the performance under heavy load on the MTA[Mail Transferring Agent]s.
Test method : Comparing by trying to send 1000 mails at instant by using a script and monitoring the load during the time.
Testing Steps:
1 ) Prepare a script to send out mails at large number,say 1000.

Script[loadtester.sh] :

#!/bin/bash
# script to send simple email
# email subject
SUBJECT="SET-EMAIL-SUBJECT"
# Email To ?
EMAIL="test@scalix.poornam.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "This is an email message test"> $EMAILMESSAGE
echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail,in this case 1000
for((i=0;i<1000 i="" span="">
do

/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE;
done

Next Step : chmod a+x loadtester
Explanation : This script will send 1000 mails using the MTA in the system.

2) We need to monitor load in the system while sending messages.

Script [loadmonitor.sh] :

#!/bin/bash
while true
do
date >> /tmp/load.txt;
/usr/bin/w >> /tmp/load.txt&
pid=$!
sleep 20
kill -SIGKILL $pid
done

Next Step: chmod a+x loadmonitor
cp -p loadmonitor /usrr/local/bin/
Explanation: Monitor the load in the system using 'w' command and append the result to 'load.txt' for reference.

3) Configure cron to run every seconds so that delay configured at load monitoring script execute load checking at interval of 20 seconds.

Adding cron entry: crontab -e
Append the line below to the file shown in the default editor in the system

* * * * * /usr/local/bin/loadmonitor

You can change the MTAs and run script to find out the load using different agents. Don't forget to attach the Antivirus/Spamassasin/Mailman and other dependent softwares which also affect the performance of MTAs.