Skip to content

Conversation

@princemuel
Copy link
Contributor

@princemuel princemuel commented Jul 28, 2025

Issue: Current implementation creates an intermediate list from a set, which is redundant.

Before:

return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) == 26

After:

return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26

Why this is better:

  • Eliminates double iteration: Old version iterates once for set(), again for list comprehension
  • Removes unnecessary list creation: No need to convert set → list just to count
  • Better memory usage: Generator expression feeds directly into set constructor
  • Same time complexity but more efficient constant factors

The old approach first deduplicates all the characters, then filters alphabetic ones. The new approach filters while building the set, which is the natural order of operations for this problem.

Issue: Current implementation creates an intermediate list from a set, which is redundant and inefficient.

**Before:**
```python
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) == 26
```
**After:**
```python
return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
```
Why this is better:
- Eliminates double iteration: Old version iterates once for set(), again for list comprehension
- Removes unnecessary list creation: No need to convert set → list just to count
- Better memory usage: Generator expression feeds directly into set constructor
- Same time complexity but more efficient constant factors

The old approach first deduplicates all the characters, then filters alphabetic ones. The new approach filters while building the set, which is the natural order of operations for this problem.
@princemuel princemuel changed the title perf: optimize is_pangram by fixing unnecessary list creation python/pangram: fix redundant double iteration in dig deeper solution Jul 28, 2025
@princemuel princemuel changed the title python/pangram: fix redundant double iteration in dig deeper solution [Pangram]: fix redundant double iteration in dig deeper solution Jul 28, 2025
@princemuel
Copy link
Contributor Author

princemuel commented Jul 28, 2025

the approach's content Set with len will also have to be updated

@BethanyG
Copy link
Member

Hi @princemuel,

Thanks for catching this and for submitting a PR. 😄

The code looks good, but I agree that the text for this approach needs updating as well. In particular:

  • Bullet 2 references & links to list comprehensions, but this is now a *set()* comprehension - you can use another link, I've put this one here as an example. I think there may be a PEP or other "official" docs link - just can't find it at the moment.

  • Bullet 4 could use some clarification, since it is assuming the ascii letters and not Unicode or Unicode/ascii mix. I think we should be sticking with ascii-only here, and being clear about it.

The snippet.txt for this particular approach will need updating.

The approaches introduction.md for this exercise will need updating for both the code and any related text.

I think there is also updating work to do on the performance article and benchmarking code, since the timings will have to be re-run now that the code for one of the approaches has changed.

Let me know once those changes have been made, and I can run everything through the CI and review it. 🎉

@princemuel
Copy link
Contributor Author

Hi @BethanyG

I just saw this. I'm currently swamped with work at the moment but I'd like to work on it over the weekend.

@princemuel
Copy link
Contributor Author

princemuel commented Aug 3, 2025

@BethanyG

Hiya 👋🏼

So I've updated the relevant docs, code and links 🤖

Please review and let me know if there are any changes to be made.

Copy link
Member

@BethanyG BethanyG left a comment

Choose a reason for hiding this comment

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

@princemuel Heya!

Apologies for the delay in review. This looks good, and I have approved it.

Thanks so much for doing this. 😄 I've added you in as a contributor for both the approach and the benchmarking. That should up your contribution rep. Please let me know if you see any issues or have any questions.

I will merge to the website shortly.

@BethanyG BethanyG merged commit 0f0c01e into exercism:main Aug 6, 2025
8 checks passed
@princemuel princemuel deleted the patch-1 branch August 7, 2025 09:50
@princemuel
Copy link
Contributor Author

@BethanyG Hiya 👋🏼

Thanks for making me responsible for this PR.

I hadn’t planned to implement it myself, but taking ownership turned out to be a great learning experience.

Really appreciate the trust and the chance to contribute more deeply.

Looking forward to helping out more where I can.

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