Printing all possible combinations of a given word.

Well I'm supposed to print all possible combinations of a given word(I'm writing this for the benefit of those who might not have guessed this from the subject). I've got some code but its horribly wrong. Could anyone point out any mistakes.
public class Try
    public void display(String s)
        char ch[]=s.toCharArray();
        for(int i=0;i<ch.length;i++)
            String word="";
            word+=ch;
char c[]=new char[s.length()];
for(int j=0;j<ch.length;j++)
if(ch[j]!=ch[i])
c[j]=ch[j];
for(int j=0;j<ch.length;j++)
for(int k=0;k<ch.length;k++)
if(c[j]!=c[k])
word+=c[k];
word+=c[j];
System.out.println(word);

Me and Lucifer have been workin on this program for ages.... we cant quite understand your pseudo code.. could you elaborate a bit more on how it works ??
Meanwhile this is the dysfunctional program weve managed to come up with so far:
public class SHAM
    public void display(String s)
        char c[]=s.toCharArray();
        for(int i=0;i<c.length;i++)
            String word="";
            char c1[]=new char[c.length-1];
            int a=0;
            for(int l=0;l<c.length;l++)
                if(i!=l)
                    c1[a++]=c[l];
            for(int j=0;j<c.length-1;j++)
                char c2[]=c1;
                word=c[i]+word;
                if(j+1!=c.length)
                    char t=c2[j];
                    c2[j]=c2[c2.length-1];
                    c2[c2.length-1]=t;
                for(int k=0;k<c2.length;k++)
                    word+=c2[k];
                System.out.println(word);               
}And this is my coding of your pseudo code:
public class WordCombo2
    public void combo(String s)
        getCombos(s.toCharArray(), s.length());
    private void getCombos(char[] c,int n)
        if (n==1)
            print(c);
            System.out.println();
        else
            for (int i=0;i<c.length;i++)
                if (i==0 && i<n)
                    c=c[n-1];
n--;
getCombos(c,n);
c[i]=c[n-1];
private void print(char c[])
for (int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
I really dont understand how this is supposed to work so i dont know whats wrong :( Im sorry this program is just totally baffling me, and on top of that recursion kind of confuses me.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • Creating a tree of all possible combinations of {a,b,c,d}

    I am an MSc conversion student who is struggling with the initial stages of coding his Association Rule Mining dissertation project.
    I have a TreeNode class, which is in the process of being written.
    The class is designed to create a tree that stores all possible combinations of a Vector alphabet of strings {a,b,c,d}
    Please could somebody explain what my TreeNode class is doing so far?
    I have had a lot of help with this and I can't understand why there are so many vectors and what they all do.
    A few pointers (no sarcastic comments please) would be much appreciated.
    The code so far:
    package treenode;
    import java.io.*;
    import java.util.*;
    public class TreeNode
    public static final String INFO_TEXT ="A program to store a vector of string items a,b,c,d in all their possible combinations into a TreeNode class";
    private Vector alphabet;
    private Vector data; // this IS TreeRootData
    private Vector children;
    public TreeNode(Vector inputAlphabet, Vector inputData)
    // the alphabet and data have already been parsed in.
    this.alphabet = inputAlphabet;
    this.data = inputData;
    System.out.println("Tree node constructed with data "+displayVectorOfStrings(this.data));
    children = new Vector(); // we now construct the Vector of children
    //createChildren();
    public void createChildren()
    if data.length =
    // first check to see whether the current data length is the same
    // as the length of the alphabet
    // if it is then stop here (return;)
    // we loop through the alphabet we have...
    // for each candidate in the alphabet, attempt to create a new child TreeNode
    // with a data Vector that is the same as the data in this TreeNode plus
    // the candidate we are looking at in the alphabet. i.e. add available LHS
    // NB: Do NOT add if the candidate is already in the data. {a,b,a} is wrong!
    // NB: We will check to see whether data is already in the tree at a later stage
    // NB: The new Child TreeNode MUST BE ADDED to the children VECTOR -
    // or it will be lost
    public static String displayVectorOfStrings(Vector vector) //Printing the Vector of Strings
    String vectorText = "{";
    for (int i=0; i<vector.size(); i++)
    vectorText += (String)vector.elementAt(i)+",";
    if (vector.size() > 0) vectorText = vectorText.substring(0, vectorText.length()-1);
    // now we chop off the last element, replacing it with a closing bracket,
    // provided that the Vector is not empty.
    vectorText += "}";
    return vectorText;
    public static void main(String[] args) //main method WITH THE ARGUMENTS PASSED IN.
    // This main class can be thought of as OUTSIDE the TreeNode class. Although
    // it is in the file called TreeNode.java it is only here for convenience.
    // This is the top level of the program where everything starts. It is here -
    // that the alphabet is hardcoded in and that the Tree root is constructed.
    displayHeading();
    Vector alphabet = new Vector(); // constructing an initial alphabet,
    alphabet.addElement("a"); // which will eventually come from the database
    alphabet.addElement("b");
    alphabet.addElement("c");
    alphabet.addElement("d");
    System.out.println("Alphabet set to be "+TreeNode.displayVectorOfStrings(alphabet));
    // the root's initial data is now made into an empty Vector...
    Vector initialData = new Vector();
    // the root can now be constructed
    TreeNode treeRoot = new TreeNode(alphabet, initialData); // the treeRoot constructor
    public static void displayHeading()
    System.out.println();
    System.out.println(INFO_TEXT);

    I don't remember from which site I took this material.
    You read this to find all possible combination of a set {a,b,c,d}
    Consider three elements A, B, and C. It turns out that for n distinct elements there are n! permutations possible. For these three elements all of the possible permutations are listed below:
    ABC, ACB, BAC, BCA, CAB, CBA
    Since there are 3 elements there are 3! = 6 permutations possible.
    Suppose instead of A, B, and C you have a deck of 52 playing cards. There are 52! permutations possible (i.e., the number of different ways the cards could be shuffled). Since 52! = 8x10^67, you will need to play a lot of cards before you have seen every permutation of the deck! Since shuffling the deck is a random process (or it should be), each of these 52! permutations is equally likely to occur.
    Developing a non-recursive algorithm to generate all permutations of n elements is a non-trivial task. However, developing a recursive algorithm to do this requires only modest effort and this is what we will do next.
    1. Let E = {e1, e2, ..., en } denote the set of n elements whose permutations we are to generate.
    2. Let Ei be the set obtained by removing element i from E.
    3. Let perm(X) denoted the permutations of the elements in the set X.
    4. Let ei.perm(X) denote the permutation list obtained by prefixing each permutation in perm(X) with element ei.
    Consider the following example, which is provided to clarify the definitions given above:
    If E = {a, b, c}, then E1 = {b, c} since the element in position 1 has been removed from E to create E1. Perm(E1) = (bc, cb) since these are the only two possible permutations of the two elements which appear in E1. Finally, e1.perm(E1) = (abc, acb) which should be apparent from the value of e1 and perm(E1) since the element a is simply used as a prefix to each permutation in perm(E1).
    Given these definitions, we set the recursion base case when n = 1. Since only one permutation is possible for one element, perm(E) = (e) where e is the lone element in E. When n > 1, perm(E) is the list e1.perm(E1) followed by e2.perm(E2) followed by e3.perm(E3) � followed by en.perm(En). This recursive definition of perm(E) defines perm(E) in terms of n perm(X)s, each of which involves an X with n � 1 elements. Thus both the base component and the recursive component (of a recursive algorithm) have been established and we thus have a complete recursive technique to generate the permutations.
    The following Java method is an implementation of this recursive definition of perm(E). This method will output all permutations whose prefix is list[0:k-1] and whose suffixes are the permutations of list[k:m]. By invoking the method with Perm(list, 0, n-1) all n! permutations of list[0: n-1] will be produced. [This is because this invocation will set k = 0 and m = n � 1, so the prefix of the generated permutations is null and their suffixes are the permutations of list[0: n-1]. When k = m, there is only one suffix which is list[m] and list[0: m] defines a permutation that is to be produced. When k < m, the else clause is executed.
    In the algorithm, let E denote the elements in list[k: m] and let Ei be the set obtained by removing ei = list[i] from E. The first swapping sequence in the for-loop has the effect of setting list[k] = ei and list[k+1: m] = Ei. Therefore, next statement which is the call to perm computes ei.perm(Ei). The final swapping sequence restores list[k: m] to its state prior to the first swapping sequence (the state is was in before the recursive call occurred).
    public static void perm(Object [] list, int k, int m)
    {    //generate all permutations of list[k:m]
    int i; Object temp;
    if (k == m) //list has one permutation � so output it
    {      for (i = 0; i <= m; i++)
    System.out.print(list);
    System.out.println;
    else //list has more than one permutation � so generate them recursively
    for (i = k; i <= m; i++)
    {     temp = list[k]; //these next three lines are a simple swap function
    list[k] = list[i];
    list[i] = temp;
    perm(list, k+1, m); //the recursive call
    temp = list[k]; //reset the order in the list
    list[k] = list[i];
    list[i] = temp;
    }//end for-loop
    } //end method perm
    See the program pasted below that prints all possible combination of a set {a,b,c,d}
    public class Perm
    public Perm()
    public static void main(String[] args)
    String s = "abcd";
    char [] chars = s.toCharArray();
    perm(chars, 0);
    public static void perm(char [] list, final int k)
    int i;
    char temp;
    if (k == list.length-1) //list has one permutation, so output it
    for (i = 0; i <= list.length-1; i++)
    System.out.print(list[i]);
    System.out.println();
    else { //list has more than one permutation, so generate them recursively
    for (i = k; i <= list.length-1; i++)
    temp = list[k]; //these next three lines are a simple swap function
    list[k] = list[i];
    list[i] = temp;
    perm(list, k+1); //the recursive call
    temp = list[k]; //reset the order in the list
    list[k] = list[i];
    list[i] = temp;
    }//end for-loop

  • All possible combinations of 23 columns in a table.

    Hi,
    We have a table as follows;
    CREATE TABLE ALL_PROD_COMB_TMP
    HDLM_ID NUMBER(10),
    MULTIPATH_ID NUMBER(10),
    TRUE_COPY_ID NUMBER(10),
    UVM_ID NUMBER(10),
    TUNING_ID NUMBER(10),
    CLUSTER_ID NUMBER(10),
    MIDDLEWARE_ID NUMBER(10),
    TAPE_ID NUMBER(10),
    THIRD_PARTY_ID NUMBER(10),
    NAS_ID NUMBER(10),
    HBA_ID NUMBER(10),
    BCM_ID NUMBER(10),
    VOLUME_MIGRATION_VERSION_ID NUMBER(10),
    TIERED_STO_MGR_VERSION_ID NUMBER(10),
    HDPS_ID NUMBER(10),
    SERVER_ID NUMBER(10),
    OS_ID NUMBER(10),
    DWDM_ID NUMBER(10),
    EXTENDER_ID NUMBER(10),
    SWITCH_ID NUMBER(10),
    INTERFACE_ID NUMBER(10),
    HUR_ID NUMBER(10),
    STORAGE_FAMILY_ID NUMBER(10)
    The data in the table is as follows;
    For formatting purpose the table data is shown in transposed form.
    Serial No. Column Name Data Value
    1     HDLM_ID                    377     586               
    2     MULTIPATH_ID               142     357               
    3     TRUE_COPY_ID               1     87     12          
    4     UVM_ID                    89     79               
    5     TUNING_ID               10     9     8          
    6     CLUSTER_ID               3     327     638          
    7     MIDDLEWARE_ID                         
    8     TAPE_ID                    3     47     54          
    9     THIRD_PARTY_ID               3                    
    10     NAS_ID                    12     91     1          
    11     HBA_ID                    300     400               
    12     BCM_ID                    14                    
    13     VOLUME_MIGRATION_VERSION_ID     13     12               
    14     TIERED_STO_MGR_VERSION_ID     1     2               
    15     HDPS_ID                    10     100               
    16     SERVER_ID               3     4               
    17     OS_ID                    2     7               
    18     DWDM_ID                    1                    
    19     EXTENDER_ID               520                    
    20     SWITCH_ID               4                    
    21     INTERFACE_ID               2     1     3     4     
    22     HUR_ID                    2     1     3     4     5
    23     STORAGE_FAMILY_ID          2
    Now we have another table named ALL_PROD_COMB having the same structure as that of ALL_PROD_COMB_TMP. In this ALL_PROD_COMB we need all the possible combinations of all the column values from ALL_PROD_COMB_TMP.
    We tried the following thing;
    insert into
         all_prod_comb (
         hdlm_id           
                                       ,multipath_id           
                                       ,true_copy_id           
                                       ,uvm_id                
                                       ,tuning_id           
                                       ,cluster_id           
                                       ,middleware_id
                                       ,tape_id
                                       ,third_party_id
                                       ,nas_id
                                       ,hba_id
                                       ,bcm_id
                                       ,volume_migration_version_id
                                       ,tiered_sto_mgr_version_id
                                       ,hdps_id
                                       ,server_id
                                       ,os_id                
                                       ,dwdm_id           
                                       ,extender_id           
                                       ,switch_id           
                                       ,interface_id           
                                       ,hur_id                
                                       ,storage_family_id
    select distinct
         a.hdlm_id           
                   ,b.multipath_id           
                   ,c.true_copy_id           
                   ,d.uvm_id                
                   ,e.tuning_id           
                   ,f.cluster_id           
                   ,g.middleware_id
                   ,h.tape_id
                   ,i.third_party_id
                   ,j.nas_id
                   ,k.hba_id
                   ,l.bcm_id
                   ,m.volume_migration_version_id
                   ,n.tiered_sto_mgr_version_id
                   ,o.hdps_id
                   ,p.server_id
                   ,q.os_id                
                   ,r.dwdm_id           
                   ,s.extender_id           
                   ,t.switch_id           
                   ,u.interface_id           
                   ,v.hur_id                
                   ,w.storage_family_id
         from (select hdlm_id from all_prod_comb_tmp) a
    ,(select multipath_id from all_prod_comb_tmp) b
    ,(select true_copy_id from all_prod_comb_tmp) c
                   ,(select uvm_id from all_prod_comb_tmp) d
                   ,(select tuning_id from all_prod_comb_tmp) e
                   ,(select cluster_id from all_prod_comb_tmp) f
                   ,(select middleware_id from all_prod_comb_tmp) g
                   ,(select tape_id from all_prod_comb_tmp) h
                   ,(select third_party_id from all_prod_comb_tmp) i
                   ,(select nas_id from all_prod_comb_tmp) j
                   ,(select hba_id from all_prod_comb_tmp) k
                   ,(select bcm_id from all_prod_comb_tmp) l
                   ,(select volume_migration_version_id from all_prod_comb_tmp) m
                   ,(select tiered_sto_mgr_version_id from all_prod_comb_tmp) n
                   ,(select hdps_id from all_prod_comb_tmp) o
                   ,(select server_id from all_prod_comb_tmp) p
                   ,(select os_id from all_prod_comb_tmp) q
                   ,(select dwdm_id from all_prod_comb_tmp) r
                   ,(select extender_id from all_prod_comb_tmp) s
                   ,(select switch_id from all_prod_comb_tmp) t
                   ,(select interface_id from all_prod_comb_tmp) u
                   ,(select hur_id from all_prod_comb_tmp) v
                   ,(select storage_family_id from all_prod_comb_tmp) w;
    But after looking at the explain plan we had to discard this. Simply not possible. Please help us with some other feasible solution.
    regards,
    Dipankar Kushari.

    Hi,
    That's what I have written while posting. Cartesian is no way possible. But is there any way so that I can find out all possible combination of the following values which is there in a table.
    1 HDLM_ID 377 586
    2 MULTIPATH_ID 142 357
    3 TRUE_COPY_ID 1 87 12
    4 UVM_ID 89 79
    5 TUNING_ID 10 9 8
    6 CLUSTER_ID 3 327 638
    7 MIDDLEWARE_ID
    8 TAPE_ID 3 47 54
    9 THIRD_PARTY_ID 3
    10 NAS_ID 12 91 1
    11 HBA_ID 300 400
    12 BCM_ID 14
    13 VOLUME_MIGRATION_VERSION_ID 13 12
    14 TIERED_STO_MGR_VERSION_ID 1 2
    15 HDPS_ID 10 100
    16 SERVER_ID 3 4
    17 OS_ID 2 7
    18 DWDM_ID 1
    19 EXTENDER_ID 520
    20 SWITCH_ID 4
    21 INTERFACE_ID 2 1 3 4
    22 HUR_ID 2 1 3 4 5
    23 STORAGE_FAMILY_ID 2
    regards,
    Dipankar.

  • Fill Array with all possible combinations of 0/1

    Hi,
    i'm looking for a fast way to fill an array, respectively an int[8], with all possible combinations of 0 and 1 (or -1 and 1, shouldn't make a difference).
    I kind of know how to do it using multiple loops but I assume there is a more elegant or at leaster "better" practice.
    Thanks,
    nikolaus
            static int cnt = 0;
         public static void main(String[] args) {
              int[] element = new int[]{1,1,1,1,1,1,1,1};
              Integer[] x = new Integer[2];
              x[0] = 1;
              x[1] = -1;
              for(int i7:x){
                   element[7] = i7;
                   for(int i6:x){
                        element[6] = i6;
                        for(int i5:x){
                             element[5] = i5;
                             for(int i4:x){
                                  element[4] = i4;
                                  for(int i3:x){
                                       element[3] = i3;
                                       for(int i2:x){
                                            element[2] = i2;
                                            for(int i1:x){
                                                 element[1] = i1;
                                                 for(int i0:x){
                                                      element[0] = i0;
                                                      cnt++;
              }Edited by: NikolausO on Oct 30, 2008 4:21 AM
    Edited by: NikolausO on Oct 30, 2008 4:22 AM

    pm_kirkham wrote:
    No I replied to message number 5. as the ' (In reply to #5 )' above my post indicates, which was in reply to (a reply) to Sabre150's post which wasn't using enhanced loops, nor has any obvious place where you could use that approach to fill the array.
    Though you could pass in an array of the values to fill the array with, and loop over those, instead of using 0 or 1, at which point Sabre's approach becomes the same as your OP, but without the manual unrolling:
    import java.util.Arrays;
    public class NaryCombinations {
    public interface CombinationHandler {
    void apply (int[] combination) ;
    public static void main(String[] args) {
    calculateCombinations(new int[]{-1, 0, 1}, 4, new CombinationHandler () {
    public void apply (int[] combination) {
    System.out.println(Arrays.toString(combination));
    public static void calculateCombinations (int[] values, int depth, CombinationHandler handler) {
    recursivelyCalculateCombinations(values, 0, depth, handler, new int[depth]);
    private static void recursivelyCalculateCombinations (int[] values, int index, int depth,
    CombinationHandler handler, int[] combination) {
    if (index == depth) {
    handler.apply(combination);
    } else {
    for (int value : values) {
    combination[index] = value;
    recursivelyCalculateCombinations(values, index + 1, depth, handler, combination);
    Which looks to use the same basic approach to the generalization I created shortly after posting the original.
    public class Scratch1
         * A 'callback' to be invoked with every combination
         * of the result.
        public interface Callback
             * Invoked for each combination.
             * <br>
             * Each call is passed an new instance of the array.
             * @param array the array containing a combination.
            void processArray(int[] array);
        public Scratch1(final int[] array, final Callback callback, final int... values)
            if (callback == null)
                throw new IllegalArgumentException("The 'callback' cannot be 'null'");
            if (array.length < 1)
                throw new IllegalArgumentException("The array length must be >= 1");
            if ((values == null) || (values.length < 1))
                throw new IllegalArgumentException("The 'values' must have be at least of length 2");
            callback_ = callback;
            values_ = values.clone();
            array_ = array;
        public Scratch1(final int order, final Callback callback, final int... values)
            this(new int[order], callback, values);
         * Generates every possible value and invokes the callback for each one.
        public void process()
            process(0);
         * Internal method with no logical external use.
        private void process(int n)
            if (n == array_.length)
                callback_.processArray(array_.clone());
            else
                for (int v : values_)
                    array_[n] = v;
                    process(n + 1);
        private final Callback callback_;
        private final int[] values_;
        private final int[] array_;
        public static void main(String[] args) throws Exception
            final Callback callback = new Callback()
                public void processArray(int[] array)
                    System.out.println(java.util.Arrays.toString(array));
            new Scratch1(6, callback, 2, 1, 0).process();

  • All possible combinations of a column as applied to a unique entry?

    i am having trouble getting a loop to do what i want, if you
    can help me find out what i am doing wrong i would appreciate it:
    i have a database with 3 tables in it
    table 1 has a list of documents - each one having a primary
    key
    table 2 has a list of document properties, i will call them
    property A, B, and C - each one having a primary key
    table 3 is a "relational" table that has a column for a
    document's PK and a property's PK that that document has
    Example of table 3:
    docPK / propPK
    1 (Document 1) / 1 (A)
    2 (Document 2) / 1 (B)
    3 (Document 3) / 2 (B)
    1 (Document 1) / 2 (B)
    1 (Document 1) / 3 (C)
    i need to create a loop in ColdFusion that spits out the
    number of possible combinations that exist for each document
    so the correct output should look like:
    Property A
    Document 1
    Property B
    Document 1
    Document 2
    Document 3
    Property C
    Document 3
    Property A, Property B
    Document 1
    Proberty A, Property B, Property C
    Document 1
    this output displays all possible combinations of properties
    under the conditions of existing documents
    here is the loop i have so far that does not seem to be
    working for me,
    <cfoutput query="rsProperties">
    <ul>
    <cfloop from="0" to="# of properties per document"
    index="i">
    <li>
    <cfoutput group="propPK">
    <cfif i EQ "# of properties per document">
    [#Trim(propertyName)#]
    </cfif>
    </cfoutput>
    </li>
    <ul>
    <cfoutput group="docPK">
    <li><a href="">#Document
    Name#</a></li>
    </cfoutput>
    </ul>
    </cfloop>
    </ul>
    </cfoutput>
    my loop returns possible combinations, but it is not
    returning ALL possible combinations, for example it may only return
    Property A as a single instead of also returning singles Property B
    and C
    my query simply consists of a SELECT * FROM [the database]
    WHERE [the three tables are set equal (as a join)] AND WHERE
    [documents exist]
    i know this is all rather confusing but if you can help me
    make sense of it i will be grateful
    thanks for your time

    Read the cfoutput section of the cfml reference manual. If
    you don't have one, the internet does.
    Look for the example that shows you how to use the group
    attribute.

  • SQL - Can u print all the dates between two given dates (Without PL/SQL)

    Hi Friends,
    I want to know if u can print all the dates between two given dates without using pl/sql.
    date1,date2 are given
    write a sql statement to display all the dates lying between those two dates.
    An earlier will be appreciated.
    Thanks in Advance
    Sriram
    null

    Sriram,
    Try this....
    select to_date('01-JAN-00')+to_number(rownum)
    from all_tables
    where rownum < to_date('10-JAN-00')-to_date('01-JAN-00')
    TO_DATE('
    02-JAN-00
    03-JAN-00
    04-JAN-00
    05-JAN-00
    06-JAN-00
    07-JAN-00
    08-JAN-00
    09-JAN-00

  • Generating all possible combinations question

    i'm trying to generate all possible combinations for several single column tables each with one string column.
    so - if table 1 has values (aa, ab, ac)
    table 2 has values (zz, zx)
    table 3 has values (qw, qe)
    the result set would contain all possible combinations:
    aa, ab, ac, zz, zx, qw, qe, aazz, aazx, aaqw, aaqe, aazzqw, aazzqe, aazxqw, aazxqe...etc.
    I've tried cross joins - but that does not get the smaller combinations.
    I've looked at some code examples but they seem to be focused on single letter or number combinations.
    I need to do this using tsql.
    code examples or links to such would be of much help.
    Thanks.

    Something like this might work:
    with t1 as (
    select val from table1
    union all
    select ''
    ), t2 as (
    select val from table2
    union all
    select ''
    , t3 as (
    Select val from table3
    union all
    select ''
    select t1.val + t2.val + t3.val
    from t1 cross join t2 cross join t3
    Russel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.com

  • Help with oracle sql to get all possible combinations in a table.

    Hello guys I have a small predicatement that has me a bit stumped. I have a table like the following.(This is a sample of my real table. I use this to explain since the original table has sensitive data.)
    CREATE TABLE TEST01(
    TUID VARCHAR2(50),
    FUND VARCHAR2(50),
    ORG  VARCHAR2(50));
    Insert into TEST01 (TUID,FUND,ORG) values ('9102416AB','1XXXXX','6XXXXX');
    Insert into TEST01 (TUID,FUND,ORG) values ('9102416CC','100000','67130');
    Insert into TEST01 (TUID,FUND,ORG) values ('955542224','1500XX','67150');
    Insert into TEST01 (TUID,FUND,ORG) values ('915522211','1000XX','67XXX');
    Insert into TEST01 (TUID,FUND,ORG) values ('566653456','xxxxxx','xxxxx');
    "TUID"                        "FUND"                        "ORG"                        
    "9102416AB"                   "1XXXXX"                      "6XXXXX"                     
    "9102416CC"                   "100000"                      "67130"                      
    "955542224"                   "1500XX"                      "67150"                      
    "915522211"                   "1000XX"                      "67XXX"                      
    "566653456"                   "xxxxxx"                      "xxxxx"                       The "X"'s are wild card elements*( I inherit this and i cannot change the table format)* i would like to make a query like the following
    select tuid from test01 where fund= '100000' and org= '67130'however what i really like to do is retrieve any records that have have those segements in them including 'X's
    in other words the expected output here would be
    "TUID"                        "FUND"                        "ORG"                        
    "9102416AB"                   "1XXXXX"                      "6XXXXX"                     
    "9102416CC"                   "100000"                      "67130"                      
    "915522211"                   "1000XX"                      "67XXX"                      
    "566653456"                   "xxxxxx"                      "xxxxx"  i have started to write a massive sql statement that would have like 12 like statement in it since i would have to compare the org and fund every possible way.
    This is where im headed. but im wondering if there is a better way.
    select * from test02
    where fund = '100000' and org = '67130'
    or fund like '1%' and org like '6%'
    or fund like '1%' and org like '67%'
    or fund like '1%' and org like '671%'
    or fund like '1%' and org like '6713%'
    or fund like '1%' and org like '67130'
    or fund like '10%' and org like '6%'...etc
    /*seems like there should be a better way..*/can anyone give me a hand coming up with this sql statement...

    mlov83 wrote:
    if i run this
    select tuid,fund, org
    from   test01
    where '100000' like translate(fund, 'xX','%%') and '67130' like translate(org, 'xX','%%');this is what i get
    "TUID"                        "FUND"                        "ORG"                        
    "9102416AB"                   "1XXXXX"                      "6XXXXX"                     
    "9102416CC"                   "100000"                      "67130"                      
    "915522211"                   "1000XX"                      "67XXX"                      
    "566653456"                   "xxxxxx"                      "xxxxx"                      
    "9148859fff"                  "1XXXXXX"                     "X6XXX"                       the last item should be excluded. The second digit in "org" is a "7" Fund is wrong, too. You're looking for 6 characters ('100000'), but fund on that row is 7 characters ('1XXXXXX').
    and this is sitll getting picked up.That's why you should use the _ wild-card, instead of %
    select  tuid, fund, org
    from    test01
    where  '100000' like translate (fund, 'xX', '__')
    and    '67130'  like translate (org,  'xX', '__')
    ;It's hard to see, but, in both calls to TRANSLATE, the 3rd argument is a string of 2 '_'s.

  • Possible combination of coins

    Hello, I'm just new here. I have some algorithm problem with algorithm that couldn't put it to a code. The problem is "For a given denomination of money (5, 10, 20, 50, 100), get all possible combinations that will not exceed 2000 and at least one of them is present".
    I know this is easy for others but for me, it's quite difficult. I already searched but I can't find something that is similar with this problem.
    Here's what I tried,
    Given:
    denominations = { 5, 10, 20, 50, 100};
    total = 2000;
    A=5, B=10, C=20, D=50, E=100
    A' = counts of A // A' >= 1
    B' = counts of B // B' >= 1
    C' = counts of C // C' >= 1
    D' = counts of D // D' >= 1
    E' = counts of E // E' >= 1
    A A' + BB' + C*C' + D*D' + E*E' <= total
    The problem is I can't find a way for a possible counts of the denomination.
    Any help is much appreciated.
    Thanks.

    For a brute force approach, consider a simpler problem:
    Can you solve the problem if denominations = {5} and maxValue = 1820?
    For each solution of the problem the above case, does it generate a smaller problem to be solved for the next higher denomination in {5, 10, 20, 50, 100}?
    Can you see how to get a solution to the bigger problem from the smaller ones?
    If you've done data structures in your course, you may also want to make it more efficient by memotisation so you don't recompute the combinations of 20,50,100 given 10 = 2 * 5 etc.

  • All possible race outcomes

    Hey,
    This is my problem - I'm making a program that will print all possible outcomes of a race, given that ALL participants can be disqualified, and logically, that there can not be several same places.
    Say that we have 2 competitors. My idea was/is to put all the options into an array razvrstitve = (1st place, 2nd place, disqualification, disqualification), and then print all the possible permutations of it. Problem is that it also considers all disqualifications to be separate, so it, will for example print out that "1st driver : disqualified (taking in razvrstitve[2]), 2nd driver: disqualified (razvrstitve[3])" and "1st driver : disqualified (taking in razvrstitve[3]), 2nd driver: disqualified (razvrstitve[2])", which is practically the same and shouldn't be printed out.
    Output example(odstop means disqualification):
    1. Voznik: 2. mesto 2. Voznik: odstop
    1. Voznik: odstop 2. Voznik: odstop
    1. Voznik: 1. mesto 2. Voznik: odstop
    1. Voznik: odstop 2. Voznik: odstop
    1. Voznik: 2. mesto 2. Voznik: odstop
    1. Voznik: 1. mesto 2. Voznik: odstop
    1. Voznik: odstop 2. Voznik: 1. mesto
    1. Voznik: odstop 2. Voznik: 1. mesto
    1. Voznik: 2. mesto 2. Voznik: 1. mesto
    1. Voznik: odstop 2. Voznik: 2. mesto
    1. Voznik: odstop 2. Voznik: 2. mesto
    1. Voznik: 1. mesto 2. Voznik: 2. mesto
    I've tried with creating new arrays and not "letting in" those elements, which's exact order has already been printed, but It's not working like it should :/
    Anyway, here is my code (the one that prints out all permutations, including duplicates), so if anyone has any ideas or pointers of how I can do what I want it'd be greatly appreciated.
    Regards,
    Sumo
    public class Naloga4 {
         public static void main(String[] args) {
            int mest = 0;     
              int stTekmovalcev = 2;     // number of competitors a.k.a
              //how long permutations should be
            int N = 2*stTekmovalcev; // number of options, inluding disqualification of
            //possibly every participant          
            int[] razvrstitve = new int[N];     
            for (int i = 0; i < N; i++) { // creates an array with all options inside            
                 if (i < stTekmovalcev) {
                      razvrstitve[i] = 0;
                 } else {
                      razvrstitve[i] = mest+1;
                      mest++;
            razvrsti(razvrstitve, razvrstitve.length, stTekmovalcev);     
        private static void razvrsti(int[] razvrstitve, int n, int r) {     // prints out all possible permutations
            if (r == 0) {
                   for (int i = n; i < razvrstitve.length; i++) {
                        String mesto = "";
                        if (razvrstitve[i] != 0) {
                             mesto = String.valueOf((int) razvrstitve) + ". mesto";
                        } else {
                             mesto = "odstop";
    System.out.printf((i-n+1) + ". Voznik: " + mesto + " ");
    System.out.println();
    return;
    for (int i = 0; i < n; i++) {
    zamenjaj(razvrstitve, i, n-1);
    razvrsti(razvrstitve, n-1, r-1);
    zamenjaj(razvrstitve, i, n-1);
    public static void zamenjaj(int[] razvrstitve, int i, int j) {     // swaps positions in the array
         // so all permutations can be made
    int temp = razvrstitve[i];
    razvrstitve[i] = razvrstitve[j];
    razvrstitve[j] = temp;

    Here is the above code translated, if it helps:
    public class ResultsPermutations {
         public static void main(String[] args) {
            int setResult = 0;     
              int nrOfCompetitors = 2;     // number of competitors a.k.a
              //how long permutations should be     
            int N = 2*nrOfCompetitors;      // number of options, including disqualification of
            // possibly every participant     
            int[] possibleresults = new int[N];     
            for (int i = 0; i < N; i++) {           // creates an array with all possible results 
                 if (i < nrOfCompetitors) {
                      possibleresults[i] = 0;
                 } else {
                      possibleresults[i] = setResult+1;
                      setResult++;
            enumerate(possibleresults, possibleresults.length, nrOfCompetitors);     
        private static void enumerate(int[] possibleresults, int n, int r) {     // prints out all possible permutations
            if (r == 0) {
                   for (int i = n; i < possibleresults.length; i++) {
                        String place = "";
                        if (possibleresults[i] != 0) {
                             place = String.valueOf((int) possibleresults) + ". place";
                        } else {
                             place = "Disqualification";
    System.out.printf("Driver " + "#" + (i-n+1) + ": " + place + " ");
    System.out.println();
    return;
    for (int i = 0; i < n; i++) {
    swapArray(possibleresults, i, n-1);
    enumerate(possibleresults, n-1, r-1);
    swapArray(possibleresults, i, n-1);
    public static void swapArray(int[] possibleresults, int i, int j) {          // swaps positions in the array
         // so all permutations can be made
    int temp = possibleresults[i];
    possibleresults[i] = possibleresults[j];
    possibleresults[j] = temp;

  • Printing every possible pair combination of elements in two arrays??

    Hi there,
    I'm trying to implement the problem of printing every possible pair combination of elements in two arrays. The pattern I have in mind is very simple, I just don't know how to implement it. Here's an example:
    g[3] = {'A', 'B', 'C'}
    h[3] = {1, 2, 3}
    ...code...??
    Expected Output:
    A = 1
    B = 2
    C = 3
    A = 1
    B = 3
    C = 2
    A = 2
    B = 1
    C = 3
    A = 2
    B = 3
    C = 1
    A = 3
    B = 1
    C = 2
    A = 3
    B = 2
    C = 1
    The code should work for any array size, as long as both arrays are the same size. Anybody know how to code this??
    Cheers,
    Sean

    not a big fan of Java recursion, unless tail recursion, otherwise you are bound to have out of stack error. Here is some generic permutation method I wrote a while back:
    It is generic enough, should serve your purpose.
    * The method returns all permutations of given objects.  The input array is a two-dimensionary, with the 1st dimension being
    * the number of buckets (or distributions), and the 2nd dimension contains all possible items for each of the buckets.
    * When constructing the actual all permutations, the following logic is used:
    * take the following example:
    * 1    2    3
    * a    d    f
    * b    e    g
    * c
    * has 3 buckets (distributions): 1st one has 3 items, 2nd has 2 items and 3rd has 2 items as well.
    * All possible permutaions are:
    * a    d    f
    * a    d    g
    * a    e    f
    * a    e    g
    * b    d    f
    * b    d    g
    * b    e    f
    * b    e    g
    * c    d    f
    * c    d    g
    * c    e    f
    * c    e    g
    * You can see the pattern, every possiblity of 3rd bucket is repeated once, every possiblity of 2nd bucket is repeated twice,
    * and that of 1st is 4.  The number of repetition has a pattern to it, ie: the multiplication of permutation of all the
    * args after the current one.
    * Therefore: 1st bucket has 2*2 = 4 repetition, 2nd has 2*1 = 2 repetition while 3rd being the last one only has 1.
    * The method returns another two-dimensional array, with the 1st dimension represent the number of permutations, and the 2nd
    * dimension being the actual permutation.
    * Note that this method does not purposely filter out duplicated items in each of given buckets in the items, therefore, if
    * is any duplicates, then the output permutations will contain duplicates as well.  If filtering is needed, use
    * filterDuplicates(Obejct[][] items) first before calling this method.
    public static Object[][] returnPermutation(Object[][] items)
         int numberOfPermutations = 1;
         int i;
         int repeatNum = 1;
         int m = 0;
         for(i=0;i<items.length;i++)
              numberOfPermutations = numberOfPermutations * items.length;
         int[] dimension = {numberOfPermutations, items.length};
         Object[][] out = (Object[][])Array.newInstance(items.getClass().getComponentType().getComponentType(), dimension);
         for(i=(items.length-1);i>=0;i--)
              m = 0;
              while(m<numberOfPermutations)
                   for(int k=0;k<items[i].length;k++)
                        for(int l=0;l<repeatNum;l++)
                             out[m][i] = items[i][k];
                             m++;
              repeatNum = repeatNum*items[i].length;
         return out;
    /* This method will filter out any duplicate object in each bucket of the items
    public static Object[][] filterDuplicates(Object[][] items)
         int i;
         Class objectClassType = items.getClass().getComponentType().getComponentType();
         HashSet filter = new HashSet();
         int[] dimension = {items.length, 0};
         Object[][] out = (Object[][])Array.newInstance(objectClassType, dimension);
         for(i=0;i<items.length;i++)
              filter.addAll(Arrays.asList(items[i]));
              out[i] = filter.toArray((Object[])Array.newInstance(objectClassType, filter.size()));
              filter.clear();
         return out;

  • After using my macbook for 2 years with same printer,all of a sudden I can't print a page doc even though printer is working and I can print from I/Mail, etc.Anybody had this happen and have a possible answer?

    After using my MacBook Pro and pages for 2 years with the same printer, all of a sudden I can't print a pages doc even though the printer works and I can print from I/Mail, etc. Anyone have a possible answer?

    Often printing problems are a sign of corrupt preferences. Go to HD > Users > (your account) > Library > Preferences, delete the com.apple.iwork.pages.plist file & then restart Pages.

  • I have a hp photosmart c4780 and i want to print from my new ipad. is this at all possible?

    I have an HP Photosmart c4780 all in one printer. I know it doesnt have eprint so I installed the HP home and biz app.  Im stuck and cannot figure out how to print from my IPAD.  Is this at all possible?  I just want to print "pages" documents from my IPAD to my HP PHotosmart c4780.  How do I do this?

    Hi rorybird,
    The HP ePrint Home & Biz app, which is a free download from the App Store, supports many "legacy" printers, so it should work for you.
    If I have solved your issue, please feel free to provide kudos and make sure you mark this thread as solution provided.
    Although I work for HP, my posts and replies are my own opinion and not those of HP.

  • Show all the possible combinations of n elements in an array???

    If I have an array of n elements, how to show all the possible combinations of n elements.
    What is the java library for doing the combinations??
    For example,
    public class CombinationTest
    {     public static void main(String[] args)     
         {     ArrayList letters = new ArrayList();
              leters.add("s1");
              leters.add("s2");
              leters.add("s3");
    Find all the combinations of 3 elements: C(3,2). Here's the result:
    1) s1, s2
    2) s1, s3
    3) s2, s3
    }

    There isn't a built-in API method for this. Still, combinations are pretty easy. Pseudocode (borrowing notation from ML and GJ, and assuming immutable sets): Set<Set<Object>> getCombinations(int itemsInCombo, Set<Object> itemsToCombine)
        // Handle base case and exceptional cases
        if (itemsInCombo = 0) return Set.emptySet();
        if (itemsInCombo < 0) throw new Exception("?!");
        // Recursive case
        Object o = itemsToCombine.first(); // use iterator - may throw exception
        // Get combos with first element
        Set<Set<Object>> rv = map (x => x.add(o)) getCombinations(itemsInCombo - 1, itemsToCombine.remove(o));
        // Add combos without first element
        rv.add(getCombinations(itemsInCombo, itemsToCombine.remove(o)));
        return rv;
    }

  • Is it possible to edit the MS-Word "Print It!" on a standalone topic?

    Is it possible to edit the MS-Word "Print It!" on a standalone topic?
    The topic would need to be edited in the Topic Editor and republished for the edits to be included in the standalone topic.
    Best regards,
    Marc

    This has been posted to the UPK forum:
    Is it possible to edit the MS-Word "Print It!" on a standalone topic?
    Best regards,
    Marc

Maybe you are looking for

  • Inserting Flash in a DW template

    I'm relatively new to Dreamweaver (version 8) and I'm having a problem inserting a flash (.swf) slideshow of pictures into a template that then updates several webpages. The problem is, when I insert the flash into the template page and the webpages

  • API for getting iView name, type, tCode

    Hi I am trying to figure out if there is a way in my program to get iView properties: the iView type (e.g. transaction, Web Dynpro, etc), then if it's a transaction iView to get the tCode for that iView. I have seen document that tells me I can get t

  • How to obtain username and date of users who executed a stored procedure

    Hi, I am modifying a stored procedure called storedprocedure_A. Within the storedprocedure_A, I want to obtain the date and username that is executing the storedprocedure_A. The date, I can obtain from sysdate. But I do not know which data dictionary

  • Powerbook MacOS 10.4 bookmarks lost on upgrade to Firefox 3.6

    I just today received an automatic upgrade to FireFox 3.6 on my Mac Powerbook G3 running Mac Os 4.12 Tiger. Now my bookmarks have vanished and some of my Doc icons for programs (iPhoto, iMovie, and iMovie HD have a questions mark instead of their ico

  • Why are "Events from my Mac" in iPhoto out of order

    "Events from my Mac"  do not sync to ipad and iPhone in the sort order specified in iPhoto???  They sync in random order and in a different order on each device.  This makes using iPhoto irritating and finding a particular event impossible.  I unders