Path: csiph.com!news.mixmin.net!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'subject:Python': 0.05; 'subject:How': 0.09; 'collections': 0.09; 'subject:using': 0.09; 'assume': 0.11; 'def': 0.13; 'skip:p 40': 0.15; 'variables': 0.15; '12:57': 0.16; 'hint?': 0.16; 'solver': 0.16; 'subject:array': 0.16; 'wrote:': 0.16; '2015': 0.20; 'martin': 0.22; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'room': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'finds': 0.29; 'anyone': 0.32; 'tue,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'solving': 0.35; 'something': 0.35; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'to:addr:python.org': 0.40; 'still': 0.40; 'easy': 0.60; 'your': 0.60; 'more': 0.63; '20,': 0.66; 'offer': 0.66; 'individuals': 0.73; 'csp': 0.84; 'fulfilled.': 0.84; 'room.': 0.84; 'rooms': 0.84; 'to:name:python': 0.84; 'room,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=0+LaRERWd51Co/eAq3ooJFTURgFGHmZG0hgSFN/bzMM=; b=lQZAn3aANDbYYlIkAaUnAP5nqhoyjksoJ4ppDwgY/Koo2pn4xkd1msFh9IOJHB+Nkh bl9hgeIK2xiFU1bJOXJ0QSazjU4+r+3bJYFP5Xktl0Jg0FBG2aG8uwn42gl23j1v+7PW MpeXlJ2T1R3C26/9pM3hilVhP1SA3JXFt9DU/uyyPp18epD9xgQfRDO8hp14DLf9AjeZ O8vxG5o8GbuA8IAmS5/q9xn/vOG6wrhgtEuZUTQbwsJYDVQcgs6jmyUIOfqHHjxaT7mY DB9WeU133zY/1lxBWKqcDXNubaYHdLnRiYRIYG6M5TyTbh4hp5gPWhbc/hpSvk58ywAK jFwQ== X-Received: by 10.107.14.196 with SMTP id 187mr6757502ioo.11.1445369210889; Tue, 20 Oct 2015 12:26:50 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <1468455.P0rGZF1LBf@PointedEars.de> From: Ian Kelly Date: Tue, 20 Oct 2015 13:26:11 -0600 Subject: Re: How to rearrange array using Python? To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1445369216 news.xs4all.nl 23725 [2001:888:2000:d::a6]:49412 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97842 On Tue, Oct 20, 2015 at 12:57 PM, Martin Sch=C3=B6=C3=B6n wrote: > It has been a while. > I have mastered solving Kenken and Sudoku using Python-constraint. > > I still have no clue on how to tell the solver how to constrain > the number of occupants in rooms: I have made up an simple example > with nine persons and three rooms. Wishes for room mates are > mild in the extreme so it is very easy for a human to place these > nine persons in the three three-bed rooms such that all wishes are > fulfilled. Python-constraint set up by me finds 27 solutions of > which most place more than three persons in at least one room. > > Anyone into CSP willing to offer me a hint? I assume that your variables are the individuals and the domains of those variables are the rooms. Based on the python-constraint docs, your constraint could look something like this: from collections import Counter ROOM_SIZE =3D { 'A': 3, 'B': 3, 'C': 4, 'D': 4, 'E': 5, } def room_size_constraint(*v): counter =3D Counter(v.values()) return all(count <=3D ROOM_SIZE[room] for room, count in counter.items()) problem.addConstraint(room_size_constraint)