8
Guido van Rossum
Last seen 8 years ago
Member for 11 years, 1 month, 1 day
Difficulty Normal
Cool solution! Very clever use of str.split() and a strict interpretation of the input format description (it never actually says that there's exactly one space between each part of the error argument :-). Agreed with all of veki's comments, but those are really just nit-picks, and the code is jus
More
Nice use of defaultdict(set), and I like the optimization of subtracting the best trace from all_nodes. Also, you used the logic I recommended to Sim0000 for putting the cycle detection at the end. But you lose half a point for using reduce(). :-)
More
Nice one! Not a word too many. Still, I couldn't help thinking of what may be an improvement: check for a cycle before putting the path into the queue. It would be something like
for next in all_node:
if next not in path and {path[-1], next} in network:
q.append(path + (next
More
I don't think this solution is particularly clear -- I like your projective math version better, and nickie's simplified version even better (as a puzzle spike).
Some software engineering / readability comments (which is mostly my angle these days):
"len2" is an odd name -- I'd call it distanc
More
I love that you're defining a class here that can be reused for other similar problems.
Why the renaming of x/y to u/v in __init__()? (Why not just name the arguments u and v as well?)
Isn't there some normalization you can apply so that comparing becomes simpler? (Sorry, I've forgotten all my
More
This solution is awesome! I just have one (software engineering) suggestion -- find a better name for the function L(). Maybe just collinear()? (Note that the word has two ells.) Then you can rename the variable just 'row'. Oh, and the arguments should better be called p, q, r instead of x, y, z --
More
It's interesting that you prefer making several passes over the input
string just to avoid splitting it in case you have a horizontal match.
In my own solution I first broke the text up in lines and then
searched each line for the word. In the extreme, your approach will
find the horizontal matc
More
All in all this code is pretty good.
import math
import itertools
import heapq
class Field(object):
def __init__(self, field):
Would be nice if the argument name was field_map (matching the call).
self._w = len(field[0])
self._h = len
More
continue
Why use 'continue' here? You could just as well revers the condition in the
if:
if not (...original condition...):
...add node...
I bet if you then use the distributive law on the condition it'll become
even clearer.
More
for node in adj:
str(node.state) in openSet.keys() and openSet[str(node.state)].f() <
node.f():
Nit: to check whether a dict has a certain key, just write "if key in dict"
rather than "if key in dict.keys()".
More
closedSet = {}
Why are these called sets when they are dicts? And why are they called
"open" and "closed"? (Maybe because that's what the Wikipedia page calls
them? That would explain several of your other poor naming choices as well.
:-)
After tracing through the code with pdb for a whil
More
#add the start node
Node(puzzle,puzzle,0,euclidianCost(puzzle,END), "")}
More
newState[newPos[0]][newPos[1]] = 0
euclidianCost(newState, END), dir)]
More
newState[newPos[0]][newPos[1]] = 0
I'd use a tuple of tuples to represent states (as I said earlier) and write
a helper function to compute a new state with a position replaced. The call
could be something like
new_state = state_replace(new_state, empty_row, empty_col,
node_st
More
for dir in DIRECTIONS.keys():
len(node.state[0]) :
I'd compute the lengths once outside the loop.
More
for dir in DIRECTIONS.keys():
DIRECTIONS[dir][1])
Use tuple unpacking here too. Then this line can become:
new_row, new_col = empty_row + dir_row, empty_col + dir_col
and all uses of newPos[0] and newPos[1] below become just new_row and
new_col. Less indexing, more meaningful variabl
More
for dir in DIRECTIONS.keys():
To iterate over the keys of a dict you can just write "for dir in
DIRECTIONS:" -- but in this case it's even better to iterate over the items:
for dir, (dir_row, dir_col) in DIRECTIONS.items():
More
emptyPos= getPos(node.state,0)
I'd use tuple unpacking here again to give emptyPos[0] and empytyPos[1]
separate names, e.g.
empty_row, empty_col = getPos(node.state, 0)
More