Skip to content

London | 25-SDC-July | Rihanna Poursoltani | Sprint 2 | Improve code with precomputing#89

Open
RihannaP wants to merge 3 commits intoCodeYourFuture:mainfrom
RihannaP:sprint2-improve-with-precomputing
Open

London | 25-SDC-July | Rihanna Poursoltani | Sprint 2 | Improve code with precomputing#89
RihannaP wants to merge 3 commits intoCodeYourFuture:mainfrom
RihannaP:sprint2-improve-with-precomputing

Conversation

@RihannaP
Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

For optimising I tried to

  • Store the list and the compare only with neighbours
  • I added a set of all char and then check if a lowercase letter exists

Questions

No question

@RihannaP RihannaP added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Jan 30, 2026
Copy link
Copy Markdown

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

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

Code looks good. Can you explain using big-O notation why your approach is better?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Feb 15, 2026
@RihannaP
Copy link
Copy Markdown
Author

RihannaP commented Mar 3, 2026

Code looks good. Can you explain using big-O notation why your approach is better?

Using a set makes membership checks O(1) on average. Creating the set takes O(n) and we iterate over at most n unique characters, so the overall complexity is O(n).

@RihannaP RihannaP added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Mar 3, 2026
@cjyuan
Copy link
Copy Markdown

cjyuan commented Mar 4, 2026

Code looks good. Can you explain using big-O notation why your approach is better?

Using a set makes membership checks O(1) on average. Creating the set takes O(n) and we iterate over at most n unique characters, so the overall complexity is O(n).

Can you also do the same for common_prefix.py?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Complete Volunteer to add when work is complete and all review comments have been addressed. labels Mar 4, 2026
@RihannaP
Copy link
Copy Markdown
Author

RihannaP commented Mar 4, 2026

Can you also do the same for common_prefix.py?

Sorting the strings takes O(n log n). After sorting, we only compare adjacent strings, and each prefix comparison takes O(m) ( m is the length of the shorter string). This results in O(n·m) for the comparisons.

Overall complexity is O(n log n + n·m), which is better than comparing every pair of strings (O(n²·m)).

@cjyuan
Copy link
Copy Markdown

cjyuan commented Mar 4, 2026

If we are factoring in the length of the strings, m, then the complexity of sorting won't just be O(n log n).
n log n measures only the number of comparisons needed.

@RihannaP
Copy link
Copy Markdown
Author

RihannaP commented Mar 4, 2026

If we are factoring in the length of the strings, m, then the complexity of sorting won't just be O(n log n). n log n measures only the number of comparisons needed.

You’re right. so sorting and comparison cost is O(m · n log n)
Adjust scan O(n · m) so overall:
O(n log n · m + n · m)= O(n log n · m)

@cjyuan
Copy link
Copy Markdown

cjyuan commented Mar 4, 2026

Yes. O(m · n log n)

O(n log n · m) may be misinterpreted as O(n log (n · m)).

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Complexity The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants