-1

I'm setting up a cognitive experiment for a professor using jsPsych, where the stimuli are about 200 logical statements of the form T ∧ F ∨ T with 4 different spacing types. I need to find a way to generate every combination of three truth values, two logical operators, and four spacing types, written in the correct order, without typing them all in by hand (hopefully).

The specifics are as follows:

Spacing types (where -- represents a space):

1) T∧T∨T i.e. no space, 2) T∧--T∨T, 3) T∧T--∨T, 4) T∧--T--∨T

Operators:

1) ∧ ∧ , 2) ∨ ∨ , 3) ∧ ∨ , 4) ∨ ∧

Truth Values:

All 3-combinations of T and F (There are 8). Ex: T T T, T T F, F T F, F F F, etc.

So I need to go through all combinations of these three things, putting them together into strings (written in html), and generating a list or array with all of the strings in it (there are about 200). I'm fairly new to cs so any help is appreciated with this problem, it's just too many moving pieces for me!

note Thanks to a commenter for pointing out that I didn't include things I've tried.

I've been trying this for a while using 3-5 nested for-loops. I initially set up an array of size 7 (as that is the longest possible length of a string) and using the for loops to fill in the spaces of the array. I have been almost completely unsuccessful using this route. It seems promising enough and maybe I can get it to work but it is also very inelegant and long and I'm wondering now if there is a more straightforward way to do this. Again, as I'm new to this there may be resources I'm unaware of.

Thanks!

Van
  • 13
  • 2
  • Hi Nic3500. This is actually my first question on stackoverflow. In fact I've been trying to do this for a while now, and I didn't know I should include all of the things I've tried like complicated looking quintuple nested for loops using arrays and what not. So your comment is very helpful for me in making future questions. Thanks! – Van Apr 01 '18 at 01:53

3 Answers3

1

To make it quick I'd use js-combinatorics.

Combinatorics.cartesianProduct(
    ['     ', '--   ', '   --', '-- --'],
    ['^^', 'vv', '^v', 'v^'],
    [ true, false ],
    [ true, false ],
    [ true, false ]
).toArray()
Ben West
  • 4,398
  • 1
  • 16
  • 16
0

Thanks to the previous answer for showing me js-combinatorics. Turns out that the code you posted does't work exactly (since each array corresponds to the position). But it helped me figure it out.

Combinatorics.cartesianProduct(
            ['T', 'F'],
            ['∧', '∨'],
            ['T', 'F'],
            ['∧', '∨'],
            ['T', 'F']
    ).toArray();

And then the spaces for each spacing type need to be added after that list is generated with a simple loop.

Van
  • 13
  • 2
0

jsPsych has a built in factorial method for situations like this.

The example from the documentation:

var factors = {
    stimulus: ['a.jpg', 'b.jpg'],
    ms_delay: [100, 200]
}

var full_design = jsPsych.randomization.factorial(factors, 1);

/*
output:
full_design = [
    {stimulus: 'a.jpg', ms_delay: 200},
    {stimulus: 'b.jpg', ms_delay: 200},
    {stimulus: 'b.jpg', ms_delay: 100},
    {stimulus: 'a.jpg', ms_delay: 100},
]
*/

You can adapt this to your problem, though you would then need to do some wrangling of the output to get it into the exact format that you are looking for.

Josh
  • 2,105
  • 1
  • 14
  • 14