[Darts Approach Docs] Various fixes and improvements#4187
Conversation
|
@BethanyG I feel like there's a better way to formulate this variant of the dict and generator approach, but nothing's coming to mind... def score(x_coord, y_coord):
throw = x_coord**2 + y_coord**2
rules = {1: 10, 25: 5, 100: 1}
return ([point for distance, point in
rules.items() if throw <= distance]
or [0])[0] # <-- Have to specify index 0. |
That's also not a generator. It is making a |
|
Yeah, most of the variants of the dict and generator approach actually use generator expressions, but for some reason this one is just a list comprehension. I'll try to take a look at it again tomorrow if I have time. |
|
I feel like this is every bit as awkward (guess it follows the text about needing an index tho): def score(x_coord, y_coord):
throw = x_coord**2 + y_coord**2
rules = {1: 10, 25: 5, 100: 1, 1000: 0}
result = [rules.get(distance, 0) for distance in
rules.keys() if throw <= distance][0]
return resultI guess we could unpack it as a generator? def score(x_coord, y_coord):
throw = x_coord**2 + y_coord**2
rules = {1: 10, 25: 5, 100: 1, 1000: 0}
result = (rules.get(distance, 0) for distance in
rules.keys() if throw <= distance)
return next(result)Feels entirely over-engineered and clever. Also requires a 4th entry in the This works too, but now I need a shower to wash off the ick: def score(x_coord, y_coord):
throw = x_coord**2 + y_coord**2
rules = {1: 10, 25: 5, 100: 1, 1000: 0}
return next(rules.get(distance, 0) for distance in rules.keys() if throw <= distance)... def score(x_coord, y_coord):
throw = x_coord**2 + y_coord**2
rules = {1: 10, 25: 5, 100: 1}
return next(rules.get(distance, 0) for distance in
(1, 25, 100, 1000) if throw <= distance)Or def score(x_coord, y_coord):
throw = x_coord**2 + y_coord**2
rules = {1: 10, 25: 5, 100: 1}
result = next(rules.get(distance, 0) for distance in
(1, 25, 100, 1000) if throw <= distance)
return result |
|
Yeah that last one isn't very readable. Also half the point of changing it in the first place was to get rid of the fourth dict entry 😅. Hopefully I'll have time to come back to this tomorrow... Maybe one of us will have a new idea to try by then. |
|
I'm beat. Time to watch bad TV and knit. See you fresh in the AM. 💙 |
Applied the fixes and improvements to the Darts exercise's approach docs that were discussed here.