Palindromer

•September 10, 2008 • Leave a Comment

This is a personal fav of mine…. create any string to the shortest possible palindromes ….

package palindromeCheck;

/**
* Converts any string into the shortest possible Palindromes ..
* @author sphukan
*
*/
public class Palindromer {

private String palindromeIt(String text,int startIndex,int endIndex,boolean fromRightToLeft){

if (startIndex==endIndex ){
return text;
} else {

char startChar=text.charAt(startIndex);
char endChar=text.charAt(endIndex);

if (startChar == endChar){ //Is a Palindrome…
if (startIndex >= endIndex ) { //Index crossed over …. end check
return text;
}else{
return palindromeIt(text,++startIndex,–endIndex,fromRightToLeft);
}

}else { //Not a Palindrome….Reconstruct

//Not good…iterates over checked indices
if (fromRightToLeft) {
return palindromeIt(text.substring(0,startIndex)+endChar+text.substring(startIndex));
} else {
return palindromeIt(text.substring(0,endIndex)+endChar+startChar+text.substring(endIndex+1));
}

}
}

}

private String palindromeIt(String text){

String result1=palindromeIt(text,0,text.length()-1,false); //From Left2Right
String result2=palindromeIt(text,0,text.length()-1,true); //From Right2Left

return (result1.length()==result2.length() && !result1.equals(result2))?result1+”\n”+result2:(result1.length()==result2.length())?result1:(result1.length()<result2.length())?result1:result2;

}

public static void main(String[] a){

System.out.println(“Shortest Palindrome(s) Possible:\n”+new Palindromer().palindromeIt(“TEST”));
}

}

Output:

Shortest Palindrome(s) Possible:
TESET
TSEST

Java Palindrome Checker

•September 5, 2008 • 1 Comment

Another Palindrome Checker …. in Java …

package palindromeCheck;
/**
* Checks if a string is a Palindrome or not …
* @author sphukan
*/

public class palindromeChecker {

private boolean checkPalindrome(String text){

if (text.charAt(0)==text.charAt(text.length()-1)){
if (text.length()==1) {
return true;
}else {
return (checkPalindrome(text.substring(1,text.length()-1)));
}
}else {
return false;
}

}

public static void main(String[] a){
System.out.println(new palindromeChecker().checkPalindrome(“ablewasiereisawelba”));

}

}

XSLT Palindrome Checker

•September 5, 2008 • Leave a Comment

A simple XSLT based palindrome checker ….

Input XML:

recInput.xml

recInput.xml

XSLT File:

recTransform.xsl

recTransform.xsl

Output XML

Out.xml

Out.xml

For a non Palindrome …we get the following result …

recInput.xml

recInput.xml

Out.xml

Out.xml

String Reversion via XSLT Recursion

•August 29, 2008 • Leave a Comment

Heres a simple way to reverse a string using XSLT. It sure would look better with a single parameter … but wasnt able to do that …. maybe will find a way …. here goes ….

Input.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<tag><value kind=”MSISDN”>sandeep phukan</value></tag>

XSLT transform:

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″>

<xsl:template name=”test” match=”tag”>
<xsl:param name=”reversedString”/>
<xsl:param name=”theRestOfTheString”/>
<xsl:choose>
<xsl:when test=”$theRestOfTheString != ””>
<xsl:call-template name=”test”>
<xsl:with-param name=”reversedString” select=”concat($reversedString,substring($theRestOfTheString,string-length($theRestOfTheString),1))” />
<xsl:with-param name=”theRestOfTheString” select=”substring($theRestOfTheString,1,string-length($theRestOfTheString)-1)” />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
<reversedText><xsl:value-of select=”$reversedString”/></reversedText>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!–This template acts as the initiator –>
<xsl:template name=”Init” match=”tag”>
<xsl:call-template name=”test”>
<xsl:with-param name=”theRestOfTheString” select=”value”/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>

XALAN output:

<?xml version=”1.0″ encoding=”UTF-8″?>
<reversedText>nakuhp peednas</reversedText>

Letters Differentiator

•January 4, 2008 • Leave a Comment

Another differentiator…pretty naive and letter by letter comparison…complexity is O(n)….pretty bad!!! Somehow I am not able to find a better way to do it…

package textManipulate;

/**
* Finds the difference between the letters of two texts using Recursion
* @author sphukan
*
*/
public class lettersDifference {

int count=0;
int differentiate(String text1,String text2){

if (text1.length()==0) return count;
else {

if (!(text1.substring(0,1)).equals(text2.substring(0,1))){
System.out.println(“Match=”+(text1.substring(0,1))+”\tWith=”+(text2.substring(0,1))+”\tis FALSE\tTotal mismatches=”+(++count));
}else {
System.out.println(“Match=”+(text1.substring(0,1))+”\tWith=”+(text2.substring(0,1))+”\tis TRUE\t\tTotal mismatches=”+(count));
}
return differentiate(text1.substring(1),text2.substring(1));
}

}

public static void main(String[] a){
new lettersDifference().differentiate(“Hello”, “Halos”);

}

}

Output:
Match=H With=H is TRUE Total mismatches=0
Match=e With=a is FALSE Total mismatches=1
Match=l With=l is TRUE Total mismatches=1
Match=l With=o is FALSE Total mismatches=2
Match=o With=s is FALSE Total mismatches=3

Frequency Finder

•January 3, 2008 • Leave a Comment

Heres another interesting one….finds out the frequency of letters in a line of text using recursion and Java collections….I am sure the same can be done without employing collections…but then I kinda like em….!!!

package textManipulate;

import java.util.HashMap;
import java.util.Map;

public class Frequency {

static Map<String,Integer> map=new HashMap<String,Integer>();

public static void main(String[] a){

Frequency freq=new Frequency();
System.out.println(freq.getFrequency(map,”abbcccddddeeeee”));

}

public Map<String, Integer> getFrequency(Map<String,Integer> map,String text) {

if (text.length()==0) return map;
else {

int freq=1;
if (map.containsKey(“”+text.charAt(0))) {
freq=map.get(“”+text.charAt(0));
map.put(“”+text.charAt(0), ++freq);
}else {
map.put(“”+text.charAt(0), freq);
}

return getFrequency(map,text.substring(1));

}

}

public String getFrequency(String text){
return “”+getFrequency(map,text);
}

}

Output:
{d=4, e=5, b=2, c=3, a=1}

Repetitive Letters Remover

•January 3, 2008 • Leave a Comment

Heres a small chunk of code to start with. This code removes repetitive letters from a line of text using recursion.

package textManipulate;

/**
* Removes repetitive letters from a string using recursion
* @author sphukan
*
*/
public class RepetitionRemover {

String removeit(String text){

if (text.length()==1) return text;
else {
//Get the next letter and check against the previous
if ((text.substring(0,1)).equals(text.substring(1,2))){
//Clip first character
return removeit(text.substring(1));

}else {
//Add the unique letter and continue searching…
return text.substring(0,1)+removeit(text.substring(1));
}

}

}

public static void main(String[] a){
RepetitionRemover r=new RepetitionRemover();
System.out.println(r.removeit(“aaabbbbcccddddddddefffgggghhhhhiiiiiiiiiijkkklm”));
}

}

Output:
abcdefghijklm