1

I have a simple experiment with 8 different trials that I wish to present only once without repeat; participants are asked to 'solve' a numeric sequence where 4 sequences have a solution (S) and 4 are unsolvable (U).

Thus, I have 8 trials: S1, S2, S3, S4, U1, U2, U3, U4.

I want to present the trials so that they alternate - but randomizing the order of S and U, while maintaining the alternating pattern.

For example, a valid result might be S3, U2, S2, U4, S4, U1, S1, U3.

The only solutions I can come up with is to try and shuffle the order of the trials in the excel file and then combining them so they alternate - this doesn't seems very elegant though.

Is there a simple way to implement this within the builder or by adding a code component?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Does this answer your question? [Pythonic way to combine (interleave) two lists in an alternating fashion?](https://stackoverflow.com/questions/3678869/pythonic-way-to-combine-interleave-two-lists-in-an-alternating-fashion) – Karl Knechtel Sep 06 '22 at 01:13

2 Answers2

0

Uses prior answer on list conversions and concept of list comprehensions

import random as r 
a = ["s1","s2","s3","s4"]
b = ["u1","u2","u3","u4"]
r.shuffle(a)
r.shuffle(b)
trialList = [tr for pr in zip(a,b) for tr in pr]
Community
  • 1
  • 1
brittAnderson
  • 1,428
  • 11
  • 25
0

You should zip (or itertools.izip) and then flatten the result using itertools.chain.from_iterable. Using zip takes multiple sequences and creates a new sequence where each item contains the corresponding elements from the original sequences. In your case, it will create something like [(S3, U2), (S2, U4), (S4, U1), (S1, U3)]. You can then flatten using the chain function to get the [S3, U2, S2, U4, S4, U1, S1, U3] sequence you want. shuffle randomizes the order of the two sequences in-place, while list converts the iterator that results from chain into a list:

from itertools import chain, izip
from random import shuffle

solvable = [s1, s2, s3, s4]
unsolvable = [u1, u2, u3, u4]

shuffle(solvable)
shuffle(unsolvable)

trials = list(chain.from_iterable(izip(solvable, unsolvable)))
TheBlackCat
  • 9,791
  • 3
  • 24
  • 31