Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107962 > unrolled thread
| Started by | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| First post | 2016-05-01 20:17 -0700 |
| Last post | 2016-05-02 00:52 -0700 |
| Articles | 5 — 2 participants |
Back to article view | Back to comp.lang.python
Code Opinion - Enumerate Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-01 20:17 -0700
Re: Code Opinion - Enumerate Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-01 20:37 -0700
Re: Code Opinion - Enumerate Stephen Hansen <me+python@ixokai.io> - 2016-05-01 22:24 -0700
Re: Code Opinion - Enumerate Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-01 23:28 -0700
Re: Code Opinion - Enumerate Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-02 00:52 -0700
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-05-01 20:17 -0700 |
| Subject | Code Opinion - Enumerate |
| Message-ID | <0a2a0652-85c2-4283-a3c4-3ca8bc4e5481@googlegroups.com> |
Looking at various Python implementations of Conway's game of life.
I came across one on rosetta using defaultdict.
http://rosettacode.org/wiki/Conway%27s_Game_of_Life#Python
Just looking for your opinion on style would you write it like this continually calling range or would you use enumerate instead, or neither (something far better) ?
import random
from collections import defaultdict
printdead, printlive = '-#'
maxgenerations = 3
cellcount = 3,3
celltable = defaultdict(int, {
(1, 2): 1,
(1, 3): 1,
(0, 3): 1,
} ) # Only need to populate with the keys leading to life
##
## Start States
##
# blinker
u = universe = defaultdict(int)
u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1
for i in range(maxgenerations):
print "\nGeneration %3i:" % ( i, )
for row in range(cellcount[1]):
print " ", ''.join(str(universe[(row,col)])
for col in range(cellcount[0])).replace(
'0', printdead).replace('1', printlive)
nextgeneration = defaultdict(int)
for row in range(cellcount[1]):
for col in range(cellcount[0]):
nextgeneration[(row,col)] = celltable[
( universe[(row,col)],
-universe[(row,col)] + sum(universe[(r,c)]
for r in range(row-1,row+2)
for c in range(col-1, col+2) )
) ]
universe = nextgeneration
Just finished watching ned batchelders talk and wondering how far I should take his advice.
http://nedbatchelder.com/text/iter.html
Thanks
Sayth
[toc] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-05-01 20:37 -0700 |
| Message-ID | <257326e1-57b7-4c96-9b42-2b72d039704a@googlegroups.com> |
| In reply to | #107962 |
Also not using enumerate but no ugly for i range implementation
this one from code review uses a generator on live cells only.
http://codereview.stackexchange.com/a/108121/104381
def neighbors(cell):
x, y = cell
yield x - 1, y - 1
yield x , y - 1
yield x + 1, y - 1
yield x - 1, y
yield x + 1, y
yield x - 1, y + 1
yield x , y + 1
yield x + 1, y + 1
def apply_iteration(board):
new_board = set([])
candidates = board.union(set(n for cell in board for n in neighbors(cell)))
for cell in candidates:
count = sum((n in board) for n in neighbors(cell))
if count == 3 or (count == 2 and cell in board):
new_board.add(cell)
return new_board
if __name__ == "__main__":
board = {(0,1), (1,2), (2,0), (2,1), (2,2)}
number_of_iterations = 10
for _ in xrange(number_of_iterations):
board = apply_iteration(board)
print board
Sayth
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Date | 2016-05-01 22:24 -0700 |
| Message-ID | <mailman.306.1462166700.32212.python-list@python.org> |
| In reply to | #107962 |
On Sun, May 1, 2016, at 08:17 PM, Sayth Renshaw wrote:
> Just looking for your opinion on style would you write it like this
> continually calling range or would you use enumerate instead, or neither
> (something far better) ?
I can't comment on your specific code because there's too much noise to
it, but in general:
Using enumerate increases readability, and I use it whenever the idiom:
for index, item in enumerate(thing):
...
is used.
Enumerate is your friend. Hug it.
--
Stephen Hansen
m e @ i x o k a i . i o
[toc] | [prev] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-05-01 23:28 -0700 |
| Message-ID | <1767aac2-ae13-485d-9184-84827c44bd5c@googlegroups.com> |
| In reply to | #107985 |
Thanks for the opinion. I should add that is not my code in first post it's the code from Rosetta on how to do Conway's GOL. I thought it looked ugly. Sayth
[toc] | [prev] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-05-02 00:52 -0700 |
| Message-ID | <f76b87ce-588c-4e3c-9a46-235dba73eb1a@googlegroups.com> |
| In reply to | #107962 |
As a reference here is a functional implementation of conways GOL. http://programmablelife.blogspot.com.au/2012/08/conways-game-of-life-in-clojure.html The author first does it in clojure and then transliterates it to python. Just good for a different view. Sayth
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web