[newbie] Recursive algorithm - review
Wiktor
look at signature.invalid
Sat Jan 4 06:53:46 EST 2014
On Sat, 4 Jan 2014 22:18:09 +1100, Chris Angelico wrote:
>>> This is the same as you have at the top of 'if not towers'. Can you be
>>> confident that row is None any time towers is None? If so, just move
>>> this up above the other check and save the duplication.
>>
>> row is None at start, but later it is list - sometimes an empty list. For
>> that cases this if statement was written. If row == [] -> generate new random
>> row that I can pop out from.
>
> Yes, but will you ever pass a non-None row and a None towers? If not,
> you can deduplicate that bit of code by simply checking one before the
> other.
Oh, now I understand what You mean.
I rewrote that part.
def generate(size=4, towers=None, row=None, x=0):
if not row:
row = [a for a in range(1, size+1)]
random.shuffle(row)
if not towers:
towers = [[0]*size for _ in range(size)]
towers[0] = row[:]
random.shuffle(row)
x = size - 1
if x + 1 < size**2:
# [...]
Much more cleaner.
Thanks!
--
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
More information about the Python-list
mailing list