Created
November 28, 2014 08:44
-
-
Save vedhavyas/7491abc078f3b402f9d5 to your computer and use it in GitHub Desktop.
Attribute combinations - A snippet to display the combinations of attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A small JAVA program to print out the combinatrics of attributes. | |
I got this problem statement during one of my job interviews. Couldnt solve it during the interview. | |
Usage : | |
1. Start the program with no arguments - | |
javac Attributes.java | |
java Attributes | |
2. "No. of attributes" - Total number of attributes (each attribute contains multiple elements (Ex: small medium large xLarge) is one Attribute) | |
3. Based on previous input number of attributes will be asked(each element in the attribute should be sepearted with a white space) | |
Example: | |
Input: | |
[ved@mypc]$ javac Attributes.java | |
[ved@mypc]$ java Attributes | |
No. of attributes | |
3 | |
Enter 1 Atrribute values | |
small medium large | |
Enter 2 Atrribute values | |
red green blue | |
Enter 3 Atrribute values | |
men women | |
Output : | |
small red men | |
small red women | |
small green men | |
small green women | |
small blue men | |
small blue women | |
medium red men | |
medium red women | |
medium green men | |
medium green women | |
medium blue men | |
medium blue women | |
large red men | |
large red women | |
large green men | |
large green women | |
large blue men | |
large blue women |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Attributes{ | |
public static void main(String [] args){ | |
Scanner scanIN = new Scanner(System.in); | |
Scanner scanSTR = new Scanner(System.in); | |
String [] temp; | |
String str; | |
System.out.println("No. of attributes"); | |
int numAtr = scanIN.nextInt(); | |
String [][] mainArray = new String[numAtr][]; | |
for(int i=0;i<numAtr;i++){ | |
System.out.println("Enter "+(i+1)+" Atrribute values"); | |
str = scanSTR.nextLine(); | |
temp = str.split(" "); | |
mainArray[i]=new String[temp.length]; | |
for(int j=0;j<temp.length;j++){ | |
mainArray[i][j] = temp[j]; | |
} | |
} | |
scanIN.close(); | |
scanSTR.close(); | |
int [] status = new int[numAtr]; | |
int index=0,attrStat; | |
boolean isDone = false; | |
if(numAtr == 1){ | |
for(int i=0;i<mainArray[0].length;i++){ | |
System.out.println(mainArray[0][i]); | |
} | |
}else{ | |
while(!isDone){ | |
if(index == (numAtr-1)){ | |
System.out.println(mainArray[index][status[index]]); | |
if(status[index] == (mainArray[index].length-1)){ | |
for(int i=0;i<index;i++){ | |
attrStat = 1; | |
for(int j=i+1;j<index;j++){ | |
if(status[j] != (mainArray[j].length-1)){ | |
attrStat = 0; | |
break; | |
} | |
} | |
if(attrStat == 1){ | |
if(status[i] == (mainArray[i].length-1)){ | |
if(i == 0){ | |
isDone = true; | |
}else{ | |
status[i]=0; | |
} | |
}else{ | |
status[i]++; | |
} | |
} | |
} | |
status[index] = 0; | |
}else{ | |
status[index]++; | |
} | |
index = 0; | |
}else{ | |
System.out.print(mainArray[index][status[index]]+" "); | |
index++; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment