code: def carefullyCollideContexts(numContexts, numCells, numMinicolumns):
    
    minicolumns = []
  
    for _ in xrange(numMinicolumns):
      contextsForCell = [set() for _ in xrange(numCells)]
  
      contexts = range(numContexts)
      random.shuffle(contexts)
  
      while len(contexts) > 0:
        eligibleCells = range(len(contextsForCell))
        while len(contexts) > 0 and len(eligibleCells) > 0:
          candidateAdditions = [(context, cell)
                                for context in contexts
                                for cell in eligibleCells]
  
          # How many new duplicate collisions will come from this addition?
          #
          # For every other context in on this cell, check how many times this
          # pair occurs elsewhere.
          badness = [sum(sum(1 if (context in otherCellContexts and
                                   otherContext in otherCellContexts) else 0
                             for minicolumn in minicolumns
                             for otherCellContexts in minicolumn)
                         for otherContext in contextsForCell[cell])
                     for context, cell in candidateAdditions]
  
          selectedContext, selectedCell = candidateAdditions[
            badness.index(min(badness))]
  
          contextsForCell[selectedCell].add(selectedContext)
          eligibleCells.remove(selectedCell)
          contexts.remove(selectedContext)
  
      minicolumns.append(contextsForCell)
  
    return minicolumns
docs: Use a greedy algorithm to choose how each minicolumn should distribute
    contexts between its cells.
  
    @return (list of lists of lists of ints)
    iContext integers for each cell, grouped by minicolumn. For example,
      [[[1, 3], [2,4]],
       [[1, 2]]]
    would specify that cell 0 connects to location 1 and location 3, while cell
    1 connects to locations 2 and 4, and cell 2 (in the second minicolumn)
    connects to locations 1 and 2.