8
Guido van Rossum
Last seen 8 years ago
Member for 11 years, 1 month, 16 days
Difficulty Normal
def getAdjNode(node):
I'd use 'adjacent' in full in the function name.
More
return (row,state[row].index(elem))
I'd rewrite this whole thing as follows:
for row, line in enumerate(state):
col = line.find(elem)
if col >= 0:
return row, col
That's one more line of code, but it's more readable and less wasteful
(your solutio
More
#get position of an element in an array
That's a pretty poor description. This function finds the (row, col)
position of an element in a puzzle state (represented as a 2-dimensional
array).
More
bestF = bestNode.f()
Using a new feature of min(), you can write this entire function as follows:
return min(openSet.values(), key=lambda node: node.f())
This will call f() the minimal number of times. It may also make sense to
turn f() -- by whatever name -- into a module-le
More
pos = getPos(b,a[row][col])
Have you heard of tuple unpacking? If you write this as
target_row, target_col = getPos(b, a_row_col)
you can avoid indexing in the next line, using targer_row instead of pos[0]
and target_col instead of pos[1].
More
for col in range(len(a[0])):
If you rewrite these two loops as
for row, a_row in enumerate(a):
for col, a_row_col in enumerate(a_row):
you can use a_row_col instead of a[row][col] on the next line, which saves
a bunch of indexing.
More
def f(self):
Another bad choice of name. What's the point of writing a program if it's
hard to read? (I know, if you just want the solution, it doesn't matter.
But if you want to brag about your code, it does. :)
More
def __init__(self, state, previous, g, h, dir):
Wow. Very poor choice of names for 'g' and 'h'.
More
END = [[1,2,3],[4,5,6],[7,8,0]]
If you use a tuple of tuples intead of a list of lists to represent the
puzzle state, a number of things become easier: e.g. you can use the state
directly as a dict key rather than its str(), and you don't have to use
deepcopy() to create a modified state.
More
1]}
You really should be using tuples instead of lists for the coordinates.
More
I had a hard time deciding on the vote. On the plus side, this is a fairly
complex algorithm and the implementation is correct as far as I can verify.
It also illustrates the A* algorithm nicely. On the minus side, the code is
riddled with poor style (pep8.py gave me *44* error message for this file
More
def checkio(labyrinth):
# I'm definetely a fan of Dijkstra's (!!!)
Funny, Dijkstra is a person, but you seem to be using his name exclusively
to refer the algorithm he published. :-)
# Object oriented Dijkstra: each cell contains 3 fields
# (and I will
More
return approach.min_diff(stones)
For extra points, you could run both solutions in parallel threads and
stop whenever the first one is done.
More
approach = min(ALGORITHMS, key=lambda algorithm: algorithm.time(stones))
This is cute, but you don't know if the time functions give calibrated
results -- the constant factors could be quite different for the two
algorithms. I guess it doesn't matter too much, as long as it selects
the
More
'''
It's pretty random that you're using ''' for the docstring here and
""" elsewhere. I recommend using """ everywhere for docstrings.
Also, my favorite formatting puts the text on the same line as the
opening """, *and* puts the closing """ on the same line if (and
*only* if) there's
More
best = math.floor(total / 2)
You can use // again here. You can then remove "import math" from
your code -- I always get a little sad seeing the (floating-point)
math module imported by code that fundamentally deals with integers.
More
table[index(x, y)] = v
PS. I'm not going to verify that this actually works; I'd get a
headache. I trust that this is what Wikipedia told you to do. :-)
More
for x, s in enumerate(stones, 1):
Your choice to start x at 1 seems odd -- in the code below, "x - 1"
occurs twice while "x" alone occurs only once.
More