Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.05; 'parameter': 0.07; 'variant': 0.07; 'xor': 0.09; 'zero.': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'assume': 0.11; '[x]': 0.16; 'head,': 0.16; 'right:': 0.16; 'solver': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'specify': 0.17; 'versions': 0.20; 'all,': 0.21; 'trying': 0.21; 'fairly': 0.21; 'cc:2**0': 0.23; 'player': 0.23; 'thus': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'options': 0.27; "doesn't": 0.28; 'appending': 0.29; 'tail': 0.29; "i'm": 0.29; 'ends': 0.30; 'initially': 0.30; 'code': 0.31; 'computer.': 0.32; 'could': 0.32; 'zero': 0.33; 'problem': 0.33; 'anyone': 0.33; 'list': 0.35; 'winning': 0.35; 'pm,': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'totally': 0.36; 'anything': 0.36; "i'll": 0.36; 'enough': 0.36; 'possible': 0.37; 'ok,': 0.37; 'does': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'end': 0.40; 'your': 0.60; 'save': 0.61; 'map': 0.61; 'perfect': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.63; 'strategy': 0.64; 'results': 0.65; 'stuck': 0.65; 'sum': 0.66; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'goal': 0.74; 'winner': 0.78; 'moves': 0.84; 'received:74.208.4.194': 0.84; 'good,': 0.91 Date: Fri, 02 Nov 2012 15:56:40 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: foster63@gmail.com Subject: Re: Haskell -> Python References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:MbbZOkJbaDuZDo4nzBdXcqkdBkmIxqZFh7dq9tgAa4M y0To3t7+pEDFjKVFizSYJT0gauWojvkttc9lCnpCmdiXBnZUIs YfkZdDrRuNcOhDnNphA1LlrEYcDHaH2v1BnRD1ihwmRuyVfpI9 s2VbDoF5QBvZXoYKYbk+rVQJcvPJiti8dCXCDVYoSkxQlFJIgd 8jnKhzl13R8WYMuXb2SUvDVRq1L6R77JvWyhz/WeQOk1vIU4YL gEfCMyh95267lCMS5w6K54J1oIAM4nSCdf4xLCk4cU1MbzLgdq +jUAYpS0EP0EQRX6r2fO8n65pKfZvVDUhee80dHqR2poFbUOA= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351886225 news.xs4all.nl 6940 [2001:888:2000:d::a6]:39856 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32654 On 11/02/2012 03:19 PM, foster63@gmail.com wrote: > Hi All, > > As part of a Nim solver I'm playing around with I'm trying to code this Haskell snippet: > > options [x] = zero : [ [y] | y <- [1..x - 1] ] > options (x:xs) = map (++ xs) (options [x]) ++ map (x:) (options xs) > > in Python. So far I have this, which works OK, but somehow doesn't feel right: > > def options( heaps ): > > if heaps == []: return [] > > head, tail = heaps[:1], heaps[1:] > > # Calculate all possible moves which is the sum of > # prepending all possible head "moves" to the tail > # and appending all possible tail "moves" to the head > > return [ [h] + tail for h in range( head[0] ) ] \ > + [ head + t for t in options( tail ) ] > > Is there anything anyone could recommend to make it more "Pythonic" or more functional. It looks clumsy next to the Haskell. > > Regards > > etc. You'd save people a lot of time if you'd specify that the parameter heaps is a list of ints, perhaps initially [1,3,5,7] or [3, 4, 5] depending on which variation of Nim you're trying to. There are many. One variant is that some versions of Nim say the winner is the player who does NOT take the last piece. I'll assume that the goal is to end up with [0,0,0,0] My main problem with studying your code is that brute force is totally unnecessary; there's a fairly simple strategy for winning at Nim. Certainly it's simple enough to have perfect strategy without any computer. A "good" move is any one where the xor of all the items in the list ends up as zero. There is always at least one move for an "ungood" position that results in a "good" one. Thus the strategy is to go from good to good, with the opponent always stuck on an ungood one. -- DaveA