Skip to content

Instantly share code, notes, and snippets.

View pberkes's full-sized avatar

Pietro Berkes pberkes

View GitHub Profile
def random_deck():
# initialize cards deck
cards = numpy.array([card for card in itertools.product(range(nfeatures),
repeat=ndims)]).T
n = cards.shape[1]
# shuffle
return cards[:, numpy.random.permutation(n)]
ncards = 12
def find_sets3(cards, indices=None):
nd, n = cards.shape
c0 = cards[0, :]
if indices is None:
indices = numpy.arange(n)
groups = [(c0 == f).nonzero()[0] for f in range(nfeatures)]
# equals
solequal = []
def find_sets2(cards):
ndims, ncards = cards.shape
all_features = set([0, 1, 2])
# solutions contain the indices of the cards forming sets
solutions = []
# iterate over all pairs
for idx1, idx2 in itertools.combinations(range(ncards - 1), 2):
c1, c2 = cards[:, idx1], cards[:, idx2]
def find_sets(cards):
"""Brute-force Sets solver."""
return [indices
for indices in itertools.combinations(range(cards.shape[1]), 3)
if is_set(cards, indices)]
from contextlib import contextmanager
import inspect
from traits.api import *
from traits import trait_notifiers
class ChangeEventPrinter(object):
def __init__(self):
import os
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.inprocess_kernelmanager import QtInProcessKernelManager
from IPython.kernel.zmq.ipkernel import Kernel
from IPython.kernel.inprocess.ipkernel import InProcessKernel
from IPython.lib import guisupport
@pberkes
pberkes / enable_keys.py
Created October 24, 2011 22:49
Show the content of KeyEvents in enable, and preview the new enable behavior
from enable.api import Component, ComponentEditor
from enable.events import KeyEvent
from traits.api import HasTraits, Instance, Str, Bool, Any
from traitsui.api import Item, View, VGroup
import wx
from pyface.qt import QtCore, QtGui
import enable.wx.constants as wx_constants
class KeyEventComponent(Component):