Balakrishna Konduri
BAN USERI am a Java Developer with 5+ yrs experience
Below is the java program
package com.bala.logical;
public class Polindrome {
public static void main(String[] args) {
String bigString = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";
String bigPoli = "";
for (int i = 0; i < bigString.length(); i++) {
for (int j = i + 1; j < bigString.length(); j++) {
String s = bigString.substring(i, j);
if (isPolindrome(s)) {
if (s.length() > bigPoli.length()) {
bigPoli = s;
}
}
}
}
System.out.println(bigPoli);
}
public static boolean isPolindrome(String s) {
String a = "";
for (int i = s.length() - 1; i >= 0; i--) {
a = a + s.charAt(i);
}
if (s.equals(a)) {
return true;
}
return false;
}
}
output: ranynar
Here is java program which will print all touples with the combinations
package com.bala.logical;
import java.util.ArrayList;
import java.util.List;
public class FindTouple {
static int[] array = new int[] { 3, 2, 1, 6, 5, 4, 9, 8, 7 };
static String finalString = "";
public static void main(String[] args) {
List<String> touples = new ArrayList<String>();
for (int i = 0; i < array.length; i++) {
getAllTouples(touples, i, 0);
}
System.out.println(touples);
}
public static List<String> getAllTouples(List<String> touples, int index,
Integer pos) {
int pivotElement = array[index];
for (int i = index + 1; i < array.length; i++) {
if (pivotElement < array[i] && pos < i) {
finalString = finalString + pivotElement;
if (finalString.length() == 2) {
finalString = finalString + array[i];
touples.add(finalString);
finalString = finalString.substring(0,
finalString.length() - 2);
pos = i;
i = index;
} else {
getAllTouples(touples, i, pos);
finalString = finalString.substring(0,
finalString.length() - 1);
}
}
}
return touples;
}
}
Output:
[369, 368, 367, 359, 358, 357, 349, 348, 347, 269, 268, 267, 259, 258, 257, 249, 248, 247, 169, 168, 167, 159, 158, 157, 149, 148, 147]
Here is the java program which will print the combinations and sum of all combinations. Little more info apart from sum.
package com.bala.logical;
import java.util.ArrayList;
import java.util.List;
public class SumCombination {
static String finalString = "";
public static void main(String[] args) {
String s = "1234";
List<String> numList = new ArrayList<String>();
System.out.println(arrangeNumbers(s, numList));
System.out.println("Size of the elements "+numList.size());
int sum = 0;
for(String combination: numList){
sum = sum + Integer.parseInt(combination);
}
System.out.println(sum);
}
public static String[] swap(String s) {
String[] combinations = new String[s.length()];
if (s.length() == 1) {
combinations[0] = s;
return combinations;
}
for (int i = 0; i < s.length(); i++) {
combinations[i] = s.substring(1, s.length()) + s.substring(0, 1);
s = s.substring(1, s.length()) + s.substring(0, 1);
}
return combinations;
}
public static List<String> arrangeNumbers(String s, List<String> numberList) {
String[] combinations = swap(s);
for (int i = 0; i < combinations.length; i++) {
finalString = finalString + combinations[i].substring(0, 1);
if (combinations.length == 1) {
numberList.add(finalString);
} else {
String subS = combinations[i].substring(1,
combinations[i].length());
arrangeNumbers(subS, numberList);
}
finalString = finalString.substring(0, finalString.length() - 1);
}
return numberList;
}
}
package com.bala.logical;
public class Polindrome {
public static void main(String[] args) {
String bigString = "aaabbaaaccdeqjncsdddmmmkkkmmmddd";
String bigPoli = "";
for (int i = 0; i < bigString.length(); i++) {
for (int j = i + 1; j < bigString.length(); j++) {
String s = bigString.substring(i, j);
if (isPolindrome1(s)) {
if (s.length() > bigPoli.length()) {
bigPoli = s;
}
}
}
}
System.out.println(bigPoli);
}
public static boolean isPolindrome1(String s) {
int i = 0;
while (i < s.length()) {
if (s.charAt(i) != s.charAt(s.length() - i)) {
return false;
}
i++;
}
return true;
}
}
Very simple program to understand, but it takes more memory and processing.
package com.bala.logical;
import java.util.ArrayList;
import java.util.List;
public class RepitivieArray {
public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 9, 7, 8, 9, 1,
2, 7, 8, 9, 1, 2 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
int index = 2;
while (index <= array.length / 2 && (i + index) < array.length) {
int[] array1 = new int[index];
int[] array2 = new int[index];
int curIndex = 0;
while (curIndex < index) {
array1[curIndex] = array[i + curIndex];
curIndex++;
}
for (int k = index; k < array.length; k++) {
int len = array.length - k - i;
if (len < index)
break;
curIndex = 0;
while (curIndex < index) {
array2[curIndex] = array[i + k + curIndex];
curIndex++;
}
if (compareArrays(array1, array2)) {
if (list.size() > 0) {
int[] duplicateArray = (int[]) list.get(0);
if (array1.length > duplicateArray.length) {
list.clear();
list.add(array1);
}
} else {
list.add(array1);
}
}
}
index++;
}
}
for (int i = 0; i < list.size(); i++) {
for (int m = 0; m < ((int[]) list.get(i)).length; m++) {
System.out.print(((int[]) list.get(i))[m]);
}
System.out.println("");
}
}
public static boolean compareArrays(int[] a, int[] b) {
for (int j = 0; j < a.length; j++) {
if (a[j] != b[j])
return false;
}
return true;
}
}
- Balakrishna Konduri July 05, 2013
In QTP its not a good practice to go head with recording the scripts for real time project. Its better build a framework such that with small changes, it should take care even the big changes in the functionality. QTP provided recording is not works always with different kind of browsers.
- Balakrishna Konduri August 01, 2013