Repository navigation
NW| 26-Jul-SDC | Ahmad Hmedan | Sprint 1 | Analyse and Refactor Functions #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AhmadHmedann
wants to merge
12
commits into
CodeYourFuture:main
Choose a base branch
from
AhmadHmedann:Sprint-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
60b84b1
refactor: calculate sum and product in one loop
AhmadHmedann 80e93a0
refactor: use Set for faster common item lookup
AhmadHmedann 9d38350
refactor: optimize pair sum lookup with set
AhmadHmedann 89cabfa
refactor: remove duplicates using Set
AhmadHmedann f0379fa
refactor: calculate sum and product in one loop
AhmadHmedann 59beb9f
refactor: optimize common item lookup with sets
AhmadHmedann 216b403
refactor: optimize pair sum lookup with set
AhmadHmedann 71bb632
refactor: remove duplicates while preserving order
AhmadHmedann 09f1520
style: format common items code
AhmadHmedann 9572046
refactor: use set intersection for common item.
AhmadHmedann 6fd957a
style: add braces
AhmadHmedann db207da
style: fix spacing in sum and product function
AhmadHmedann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
56 changes: 32 additions & 24 deletions
56
Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,39 @@ | ||
| from typing import Dict, List | ||
|
|
||
| # def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: | ||
| # """ | ||
| # Calculate the sum and product of integers in a list. | ||
|
|
||
| def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: | ||
| """ | ||
| Calculate the sum and product of integers in a list. | ||
| # Note: the sum is every number added together | ||
| # and the product is every number multiplied together | ||
| # so for example: [2, 3, 5] would return | ||
| # { | ||
| # "sum": 10, // 2 + 3 + 5 | ||
| # "product": 30 // 2 * 3 * 5 | ||
| # } | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Optimal time complexity: | ||
| # """ | ||
| # # Edge case: empty list | ||
| # if not input_numbers: | ||
| # return {"sum": 0, "product": 1} | ||
|
|
||
| # sum = 0 | ||
| # for current_number in input_numbers: | ||
| # sum += current_number | ||
|
|
||
| Note: the sum is every number added together | ||
| and the product is every number multiplied together | ||
| so for example: [2, 3, 5] would return | ||
| { | ||
| "sum": 10, // 2 + 3 + 5 | ||
| "product": 30 // 2 * 3 * 5 | ||
| } | ||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| """ | ||
| # Edge case: empty list | ||
| if not input_numbers: | ||
| return {"sum": 0, "product": 1} | ||
| # product = 1 | ||
| # for current_number in input_numbers: | ||
| # product *= current_number | ||
|
|
||
| sum = 0 | ||
| for current_number in input_numbers: | ||
| sum += current_number | ||
|
|
||
| product = 1 | ||
| for current_number in input_numbers: | ||
| product *= current_number | ||
| # return {"sum": sum, "product": product} | ||
| def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: | ||
| total: int = 0 | ||
| product: int = 1 | ||
| for num in input_numbers: | ||
| total += num | ||
| product *= num | ||
|
|
||
| return {"sum": sum, "product": product} | ||
| return {"sum": total, "product": product} |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the complexity of the original code? (The Python implementation is different from the JS implementation)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
original
Time Complexity: O(n * m * k), worst case O(n^3)
Two nested loops compare the sequences, and checking
"i not in common_items" can require scanning the result list.
new
Time Complexity : O(n+m)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why enlarged the text? It looks like 'yelling".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that. I didn’t realise that adding
#would make the line render as a large heading. It looked small while I was typing it, so I didn’t notice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you didn't meant it.
I always use the "Preview" feature to check what I typed on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback. From now on, I’ll use the Preview feature to check my comments before posting them.