Marcelo Filho
BAN USERI'm a brazilian Android developer studing to get a Facebook job!
Career developed in the IT field, with extensive experience on creation, development and project
scheduling, implementation and maintenance of systems, implementing improvements in
developing trajectory in small and large national and multinational companies.
Realization of activity planning, specification, definition and implementation of frameworks and
libraries, optimizing routines mainly in projects for mobile devices, reducing costs and time, and
ensuring the quality and speed of delivery.
/**
*
* @author Marcelo Filho
* @email marcelolfilho@gmail.com
* @facebook facebook.com/idemax.green
*
*/
public class PalindromePratice {
/**
* Buffer of results to be displayed.
*/
private static StringBuilder strB;
/**
* Main called by Java.
*
* @param args
*/
public static void main( final String[] args ) {
PalindromePratice.palindrome( "abba" );
}
/**
* Add some {@link String} to result buffer.
*
* @param value
*/
private static void addToBuffer( final String value ) {
if ( PalindromePratice.strB == null ) {
PalindromePratice.strB = new StringBuilder();
}
PalindromePratice.strB.append( value );
PalindromePratice.strB.append( ',' );
}
/**
* Display results in buffer.
*/
private static void displayResults() {
if ( PalindromePratice.strB != null ) {
PalindromePratice.strB.deleteCharAt( PalindromePratice.strB.length() - 1 );
System.out.println( PalindromePratice.strB.toString() );
}
}
/**
* Scan for palindromes.
*
* @param value
* {@link String} with two or more chars.
*/
private static void palindrome( final String value ) {
PalindromePratice.palindrome( value, true );
PalindromePratice.displayResults();
}
/**
* Scan for palindromes.
*
* @param value
* {@link String} with two or more chars.
* @param recursive
* If algorithm has to scan recursively.
*/
private static void palindrome( final String value, final boolean recursive ) {
final int valueLen = value.length();
if ( valueLen > 0 ) {
final int halfValueLen = valueLen / 2;
boolean isPalindrome = true;
if ( recursive ) {
int wordSize = 1;
for ( int i = 0; i < valueLen; i++ ) {
while ( ( wordSize < valueLen ) && ( i <= wordSize ) ) {
PalindromePratice.palindrome( value.substring( i, wordSize++ ), false );
}
wordSize = 1;
}
}
for ( int i = 0, j = ( valueLen - 1 ); i <= halfValueLen; i++, j-- ) {
isPalindrome = value.charAt( i ) == value.charAt( j );
if ( !isPalindrome ) {
break;
}
}
if ( isPalindrome ) {
PalindromePratice.addToBuffer( value );
}
}
}
}
Java based on Umer Javaid answer.
public class Main {
private static int sequences[];
public static void main( final String[] args ) {
Main.combination( 4, 0, 3 );
}
private static void combination( final int n, final int i, final int l ) {
if ( Main.sequences == null ) {
Main.sequences = new int[n];
}
if ( n == 0 ) {
Main.display( Main.sequences, i );
} else {
for ( int j = 1; j <= l; j++ ) {
if ( i < Main.sequences.length ) {
Main.sequences[i] = j;
} else {
break;
}
Main.combination( n - j, i + 1, j );
}
}
}
private static void display( final int[] sequence, final int l ) {
final StringBuilder strBuilder = new StringBuilder();
strBuilder.append( '{' );
for ( int i = 0; i < l; i++ ) {
strBuilder.append( sequence[i] );
strBuilder.append( ',' );
}
strBuilder.deleteCharAt( strBuilder.length() - 1 );
strBuilder.append( '}' );
System.out.println( strBuilder.toString() );
}
}
- Marcelo Filho November 20, 2013