Skip to content

[Darts Approach Docs] Various fixes and improvements#4187

Open
Yrahcaz7 wants to merge 1 commit into
exercism:mainfrom
Yrahcaz7:darts-approaches-cleanup
Open

[Darts Approach Docs] Various fixes and improvements#4187
Yrahcaz7 wants to merge 1 commit into
exercism:mainfrom
Yrahcaz7:darts-approaches-cleanup

Conversation

@Yrahcaz7
Copy link
Copy Markdown
Contributor

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

@Yrahcaz7
Copy link
Copy Markdown
Contributor Author

Yrahcaz7 commented May 16, 2026

@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
Copy link
Copy Markdown
Member

BethanyG commented May 16, 2026

@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.

@Yrahcaz7
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
Copy link
Copy Markdown
Member

BethanyG commented May 16, 2026

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

@Yrahcaz7
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.

@BethanyG
Copy link
Copy Markdown
Member

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

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