FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Darts Approach Docs] Various fixes and improvements by Yrahcaz7 · Pull Request #4187 · exercism/python · GitHub

Repository navigation

[Darts Approach Docs] Various fixes and improvements - #4187

Merged
BethanyG merged 2 commits into
exercism:mainfrom
Yrahcaz7:darts-approaches-cleanup
May 18, 2026
Merged

BethanyG merged 2 commits into
exercism:mainfrom
Yrahcaz7:darts-approaches-cleanup

Conversation

Copy link
Copy Markdown
Contributor

Applied the fixes and improvements to the Darts exercise's approach docs that were discussed here.

Yrahcaz7 commented May 16, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

@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... list doesn't seem to have something like dict.get().

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.

BethanyG commented May 16, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

@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... list doesn't seem to have something like dict.get().

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 list in memory. Lemme play with it. ooooh (you can tell I am tired) nevermind. I am an idiot. I will noodle on it.

Copy link
Copy Markdown
Contributor Author

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.

BethanyG commented May 16, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

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 result

I 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 dict.

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

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

I'm beat. Time to watch bad TV and knit. See you fresh in the AM. 💙

Yrahcaz7 commented May 17, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

New idea:

from math import inf

def score(x_coord, y_coord):
    throw = x_coord**2 + y_coord**2
    rules = {1: 10, 25: 5, 100: 1, inf: 0}
    
    return [point for distance, point in
                rules.items() if throw <= distance][0] # <-- Have to specify index 0.

Fairly clean, but requires an import.

Copy link
Copy Markdown
Member

Works for me. 😄

BethanyG left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Wow. That was a LOT. Nice work. 😄

Copy link
Copy Markdown
Member

Worried that if you or I give it one more read, there will be changes. Shall we let it sit for a bit for rereivew, or are you ready to merge?

Copy link
Copy Markdown
Contributor Author

I think it would probably be a good idea to wait for a day and then re-review. Looking at the changes again with fresh eyes might spot previously-unseen mistake(s). (I don't trust myself enough to believe that there aren't any on this big of a change.)

Copy link
Copy Markdown
Member

Heya @Yrahcaz7

I just went over this again and am not seeing anything more to correct. So if you are happy with this, I will merge it! Thanks again for all the hard work here. 🙂

Copy link
Copy Markdown
Contributor Author

I looked it over one more time just in case, but it looks good! Feel free to merge!

BethanyG merged commit 1350f35 into exercism:main May 18, 2026
7 checks passed
Yrahcaz7 deleted the darts-approaches-cleanup branch May 18, 2026 20:18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL