by Colin Vout

#### Published Sunday May 09 2021 (link)

“Sure, I can help. From this hotel it’s nine blocks to the diner; from there it’s five blocks to the library, and then six blocks back here. I guess instead you could come back from the diner to the museum — that’s four blocks — and then seven blocks back here. Or three blocks to the gallery and then eight blocks back here.”

“So the diner’s straight along here?”

“No sir, it’s not straight along one road for any of these trips; I just meant so many edges of blocks. The city’s on a square grid, and all these places are on corners, but, fact is, none of them is on the same street or avenue as any other one.”

How many blocks between the library and the museum, museum and gallery, and gallery and library?

From → Uncategorized

Here is a solution using a recursive function to replace a large chunk of inline code in my previous solution in the hope of reducing the number of lines of code. In the event, there was no improvement. In this problem, the order in which the solution is found is indifferent which would indicate that a recursive function is not necessarily the best fit.

Hi John,

As you say, it was longer and no different in speed.

The easiest way of increasing speed is to avoid looking in all four quadrants of the grid which would make it around five times faster.

In terms of length, it rather depends on what count as lines since it would be a pity to loose all comments in pursuit of a ‘fewest lines’ objective. And in my experience reducing the number of code lines in Python can easily end up reducing the clarity of the code involved.

Nevertheless, I do think that using a dictionary in place of a list, avoiding some list indexing and compressing some of the comments can shorten your code without impacting on its clarity:

Hi Brian,
Thanks for doing a rework of my second post. I’ll study your version with interest, in particular your use of a dictionary as opposed to a list. I had gotten the impression that dictionary access must be slower than list access. My lines 12,13 could have made CC into a tuple instead of a list but having to use ‘tuple(‘ twice instead of the single opening square brace character puts me off. CC as a tuple would I think be slightly faster.

I did at one point limit the ‘Diner’ coordinate search to the NE quadrant but felt this was a bit of a fudge. I wasn’t trying for speed with this teaser.

When counting lines of code, I don’t count comments and count continuation lines of a statement as additional lines of code.

I agree: too much compaction can make code harder to comprehend.

HI John,

Dictionaries are fundamental in Python since every aspect of the language uses them. For example, every time you define a variable it becomes a key in a dictionary that points to the variables current value. And because they are so fundamental a lot of effort goes into making them fast. Dictionary access will only be slower than list access only in rare circumstances and if a list is of any significant length, dictionary access will on average be a great deal faster. On average for 100 items a dictionary is typically six times faster, for 1000 items about 15 times faster and for 10 million items, 500,000 times faster. So it generally pays to use dictionaries if you can. The downside of dictionaries is that they use a lot more memory than lists.

I understand the tuple inside a tuple issue and I, too, also often use lists to avoid this. There is also another subtle issue in that tuples inside tuples built using two left brackets in sequence ‘((‘ can mean that the inner tuple is not a tuple at all but a generator instead. So to be sure of getting a tuple inside a tuple it might be necessary to use the tuple(tuple( …)) construction.

Here is an even more pared down version with savings based on the fact that the variables ‘st’ and ‘av’ contain the same information as ‘res’ but in a transposed form. This allows ‘res’ to be removed from the ‘fp’ argument list if ‘st’ and ‘av’ are kept in order as a list or tuple.

I post a very basic approach. I whish I could have got my pos()-function just a little more compact not to speak of the cumbersome way the program checks that no institution is in the same street or avenue. But in the end of the day, it gets the answer based on all the eight possible locations..

I shall try to figure out how Brian make use of the ‘class-object’ above. That will be a fundamental concept to learn.

Hi Erling,

Its nice to see a different approach based on set intersection. You could use the following to reduce the length of the code:

and