[Tutor] What's in a name?
Keith Winston
keithwins at gmail.com
Fri Jan 3 09:26:58 CET 2014
On Fri, Jan 3, 2014 at 2:42 AM, Danny Yoo <dyoo at hashcollision.org> wrote:
>
> I hope you don't take offense. But I actually do not understand
> print_candl_info(). I thought I did! But then I realized I was
> deluding myself. I don't not understand it after all, and that's
> after staring at it for more than about thirty minutes.
>
>
I dare you to try to offend me, while offering helpful comments! Okay, I
take that back. Anyway, no I don't.
> Take that a "canary in the coalmine" kind of comment. eval() can
> encourages habits that hurt program readability in a deep way.
>
> Fair enough.
> where we first unroll the original's outer loop out so that there are
> no eval()s anywhere. This hurts program length a little, but we can
> deal with that. Do you agree so far that the program above preserves
> the meaning of what you had before?
>
>
Ha ha, you must understand, because you recreated the original form of the
code. First, realize that this is all a learning exercise for me, I am not
actually obsessed by Chutes & Ladders. So I was trying to figure out if I
could do this thing of iterating over a list of (functions, lists,
dictionaries) and print both some output related to the object, and the
objects name. But I think you now understand that. In this _particular_
case, I've got that little list_index kluge, which is probably good, since
it sort of forces me to try to solve a more general case (or get other
people to!... er, thanks!)
>
> If we can assume that the rewritten code "means" the same thing, then
> we can eliminate the duplication by doing something like this:
>
> ########################################################
> def print_candl_info(garray):
> game_array, chute_nums, ladder_nums = {}, chutes, ladders
> for i in chute_nums.keys(): chute_nums[i] = 0
> for i in ladder_nums.keys(): ladder_nums[i] = 0
>
> summarize_game("chutes", chute_nums, 4, garray)
> summarize_game("ladders", ladder_nums, 5, garray)
>
>
I see what you did there.
> Hmmm. I don't understand why chute_nums is assigned to chutes, nor
> ladder_nums to ladders, and I don't see game_array being used here
> yet. We can absorb that:
>
>
>
Yes, game_array was left over, sorry. But chutes and ladders are
dictionaries that should remain unchanged, for the purpose of future games.
chute_nums = chutes only b/c that was an easy way to fill in all the keys:
I then zero out the values, and increment the values for every time any
given chute or ladder is traversed during a game (this information is in
game[4] and game[5], each of which are lists of the chutes or ladders
traversed in each given game). I hope that's clear. So I think you can't
lose that, or you'd have to fill in those keys differently (which is how I
did it at first, but this approach seemed cleaner).
########################################################
> def print_candl_info(garray):
> summarize_game("chutes", chutes, 4, garray)
> summarize_game("ladders", ladders, 5, garray)
>
> def summarize_game(game_name, game_nums, game_index, garray):
> for i in game_nums.keys(): game_nums[i] = 0
>
> for corl in game_nums:
> for game in garray:
> if corl in game[game_index]:
> game_nums[corl] += 1
> print("total ", game_name, "= ", sum(game_nums.values()))
> ########################################################
>
>
This is definitely an improvement, IMO. Summarize_game is _almost_ a really
good name, but it isn't really summarizing a single game: it's iterating
over all the games in garray (the array of games), and adding up all the
particular chutes and ladders occurrences, if I haven't already made that
clear... It's sneaky like that. I'm tempted to say summarize_candl, but
candl is getting boring... I'll leave it for now.
> As a side note: the code above has more latitude than the original
> because it can print out a friendlier game name. That is, for
> example, you can do this:
>
> ##################################################
> summarize_game("OOOO Chutes! OOOO", chutes, 4, garray)
> summarize_game("HHHH Ladders! HHHH", ladders, 5, garray)
> ##################################################
>
Yep, true that.
Thanks Danny, this was instructive. Very kind of you.
--
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140103/3b0b9b44/attachment.html>
More information about the Tutor
mailing list