Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32654

Re: Haskell -> Python

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 <d@davea.name>
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 <d@davea.name>
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 <b19e3922-d86f-426f-afb8-1f75b793f87b@googlegroups.com>
In-Reply-To <b19e3922-d86f-426f-afb8-1f75b793f87b@googlegroups.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3217.1351886225.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Haskell -> Python foster63@gmail.com - 2012-11-02 12:19 -0700
  Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 15:56 -0400
    Re: Haskell -> Python Simon Foster <simon.foster@inbox.com> - 2012-11-02 20:09 +0000
  Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 15:40 -0600
    Re: Haskell -> Python Duncan Booth <duncan.booth@invalid.invalid> - 2012-11-03 16:29 +0000
  Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 15:46 -0600
  Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 18:24 -0400
  Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 16:27 -0600
  Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 22:03 -0400
  Re: Haskell -> Python aahz@pythoncraft.com (Aahz) - 2012-11-03 22:16 -0700

csiph-web