-
-
Notifications
You must be signed in to change notification settings - Fork 29
Glasgow | 25-SDC-Nov | Nataliia Volkova | Sprint 1 | Big-O notation and refactoring in Python tasks 95-96 #162
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
Nataliia74
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
Nataliia74:combined-pr
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
3 commits
Select commit
Hold shift + click to select a range
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
64 changes: 42 additions & 22 deletions
64
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,51 @@ | ||
| 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. | ||
| 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 | ||
| # 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 | ||
|
|
||
| # 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]: | ||
| if not input_numbers: | ||
| return {"sum": 0, "product": 1} | ||
|
|
||
| sum = 0 | ||
| for current_number in input_numbers: | ||
| sum += current_number | ||
|
|
||
| product = 1 | ||
| for current_number in input_numbers: | ||
| product *= current_number | ||
|
|
||
| for num in input_numbers: | ||
| sum += num | ||
| product *= num | ||
|
|
||
| return {"sum": sum, "product": product} | ||
|
|
||
| # """ | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| # Optimal time complexity: O(n) | ||
| # """ | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,40 @@ | ||
| from typing import List, Sequence, TypeVar | ||
| from collections import OrderedDict | ||
|
|
||
| ItemType = TypeVar("ItemType") | ||
|
|
||
|
|
||
| # def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: | ||
| # """ | ||
| # Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
|
|
||
| # Time complexity: | ||
| # Space complexity: | ||
| # Optimal time complexity: | ||
| # """ | ||
| # unique_items = [] | ||
|
|
||
| # for value in values: | ||
| # is_duplicate = False | ||
| # for existing in unique_items: | ||
| # if value == existing: | ||
| # is_duplicate = True | ||
| # break | ||
| # if not is_duplicate: | ||
| # unique_items.append(value) | ||
|
|
||
| # return unique_items | ||
|
|
||
| def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: | ||
| """ | ||
| Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
|
|
||
| Time complexity: | ||
| Space complexity: | ||
| Optimal time complexity: | ||
| Time complexity: O(n) | ||
| Space complexity: O(n) | ||
| Optimal time complexity: O(n) | ||
| """ | ||
| unique_items = [] | ||
|
|
||
| for value in values: | ||
| is_duplicate = False | ||
| for existing in unique_items: | ||
| if value == existing: | ||
| is_duplicate = True | ||
| break | ||
| if not is_duplicate: | ||
| unique_items.append(value) | ||
|
|
||
| return unique_items | ||
| return list(OrderedDict.fromkeys(values)) | ||
|
|
||
| # Used OrderedDict to create a dictionary with the list elements as keys (preserves order) | ||
| # Then, convert it back to a list to remove duplicates | ||
| # https://www.w3resource.com/python-exercises/list-advanced/python-list-advanced-exercise-8.php#:~:text=The%20Python%20OrderedDict.,order%20of%20the%20remaining%20elements. |
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.
Please remove this merge conflict marker!