patternpythonMinor
Identicon Generator
Viewed 0 times
identicongeneratorstackoverflow
Problem
I'm just playing with Python, trying to refamiliarize myself with it since I haven't used it in a few years. I'm using Python3, but as far as I know it could easily be Python2 with a change to the print statements. What I'm generating is something similar to GitHub's Identicons.
I know of two possible issues, one is that I don't account for an odd width, and the other is that my fill string will be reversed on the symmetric half, so I can't use something like
```
import random
def generateConnectedAvatar(fill="XX", empty=" ", height=10, width=10, fillpercent=0.4):
halfwidth = int(width/2)
painted = 0
maxPainted = heighthalfwidthfillpercent
adjacent = [(-1,-1), (-1,0), (-1,1), (0,1), (0,-1), (1,-1), (1,0), (1,1)]
# initialize a blank avatar
avatar = [[empty for w in range(halfwidth)] for h in range(height)]
# 'paint' a single cell and add it to the stack
y, x = random.randint(0,height-1), random.randint(0,halfwidth-1)
lst = [(y,x)]
while (len(lst) != 0):
# Take the first element off the list
y, x = lst.pop()
# Determine if we should paint it
if painted = 0 and tmpx = 0 and tmpy < height:
# Make sure we haven't already painted it
if avatar[tmpy][tmpx] is not fill:
lst.append((tmpy,tmpx))
# output the result (symmetrically)
# (in our case, just printing to console)
for h in range(heigh
I know of two possible issues, one is that I don't account for an odd width, and the other is that my fill string will be reversed on the symmetric half, so I can't use something like
[ ]. ```
import random
def generateConnectedAvatar(fill="XX", empty=" ", height=10, width=10, fillpercent=0.4):
halfwidth = int(width/2)
painted = 0
maxPainted = heighthalfwidthfillpercent
adjacent = [(-1,-1), (-1,0), (-1,1), (0,1), (0,-1), (1,-1), (1,0), (1,1)]
# initialize a blank avatar
avatar = [[empty for w in range(halfwidth)] for h in range(height)]
# 'paint' a single cell and add it to the stack
y, x = random.randint(0,height-1), random.randint(0,halfwidth-1)
lst = [(y,x)]
while (len(lst) != 0):
# Take the first element off the list
y, x = lst.pop()
# Determine if we should paint it
if painted = 0 and tmpx = 0 and tmpy < height:
# Make sure we haven't already painted it
if avatar[tmpy][tmpx] is not fill:
lst.append((tmpy,tmpx))
# output the result (symmetrically)
# (in our case, just printing to console)
for h in range(heigh
Solution
This is quite neat. I suggest some improvements though.
The method does two things: it generates an identicon and then prints it. It would be better to split these two operations so that you can have functions that follow the single responsibility principle.
The while loop condition can be simplified to be more pythonic:
The range conditions can be simplified too:
Your naming and spacing should follow PEP8, the python style guide.
The method does two things: it generates an identicon and then prints it. It would be better to split these two operations so that you can have functions that follow the single responsibility principle.
The while loop condition can be simplified to be more pythonic:
while lst:The range conditions can be simplified too:
if 0 <= tmpx < halfwidth:Your naming and spacing should follow PEP8, the python style guide.
Code Snippets
if 0 <= tmpx < halfwidth:Context
StackExchange Code Review Q#93245, answer score: 3
Revisions (0)
No revisions yet.