user3823821
user3823821

Reputation: 91

Java calculate the count with condition

I have the following data, I want to know what the best data structure to calculate the count for the following:

enter image description here

Note:

One way to do that is by using prefix tree (tries), here the paper to describe that: http://www.sciencedirect.com/science/article/pii/S095070511400152X#

but I don't want to use any tree as a data structure.

Update, for people who asked if I did effort:

Yes I did my effort, but I was looking for better ideas, and thank you all. Special tahnks to @isaac, I really appropriate your help.

I rearranged my the data to this format (variable, arraylist pairs (record number that variable appears in, length of the record)) Where the length of the record = how many time the variable's value=1

Example:

A, comes in the records ( (7,1) ,(8,2) , (9,2) , (10,3), (11,3), (12,3) ,(13,4) )
B, comes in the records ( (4,1) , (5,2) ,(6,3), (11,3), (12,3) ,(13,4))
C, comes in the records ( (2,1), (3,2), (5,2) ,(6,3), (9,2) , (10,3), (12,3) ,(13,4) )
Z, comes in the records ( (1,1) , (3,2), (6,3), (8,2), (10,3), (11,3), (13,4) )

Then:

For example: the intersection between the variables (A, B, Z) lies in records (11, and 13) But

The length of record 13 > the number of variables. thus, I will not count it

As result:

The count number of (A=1, B=1, Z=1) = 1. Let call this result R1
From this, I can figure out the others values:
The count number of (A=1, B=1, Z=0) =  R1 -  Afrequency  =1-1=0
The count number of (A=0, B=1, Z=1) = R1 -  Zfrequency = 1-1 =0
The count number of (A=0, B=1, Z=0) = Bfrequency =1

Upvotes: 0

Views: 1183

Answers (1)

Isaac
Isaac

Reputation: 158

I don't research on this prefix tree you're talking about nor bother to read the paper you've given. But from my understanding of your problem, you need to count how many condition of a given state you specify, and I can give you some idea.

I propose a method count(Boolean a, Boolean b, Boolean c, Boolean z).

just have a bunch of if and else if to compare every possible states with a,b,c and z. Increment the counter if the tested states equals your input and then you can return the value. For your example you will call count(true,true,null,true);

Notice that you can't use primitive type boolean because it cannot holds null.

EDIT: Here's my implementation. For simplicity there is only 2 bit states. And possible states are (A,B) 0,0 ; 0,1 ; 1,0 ; 1,1. And I try to count how many A = 1;

public static void main(String[] args) {
    boolean[][] possibleStates = {{false,false},{false,true},{true,false},{true,true}};
    int result = count(true,null,possibleStates);
    System.out.println(result);
}
public static int count(Boolean a,Boolean b,boolean[][] possibleStates) {
    int counter = 0;
    Boolean oldA = a;
    Boolean oldB = b;
    for(boolean[] state : possibleStates) {
        a = oldA;
        b = oldB;
        if(a == null) 
            a = state[0];
        if(b == null) 
            b = state[1];
        if(state[0] == a && state[1] == b)
            counter++;
    }
    return counter;
}

it outputs 2

Upvotes: 1

Related Questions