Compare commits

...

6 Commits

Author SHA1 Message Date
Hardik Pawar
5a8adc3ee7
Merge 14c9575c34 into e3f3d668be 2024-11-15 17:05:38 +05:30
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
Hardik Pawar
14c9575c34 Merge branch 'activity_selection_new_algo2' of https://github.com/Hardvan/Python into activity_selection_new_algo2 2024-10-02 12:42:26 +05:30
Hardik Pawar
57fbc0332e Rename x to activity 2024-10-02 12:42:24 +05:30
Hardvan
c81787e695 updating DIRECTORY.md 2024-10-02 07:11:19 +00:00
Hardik Pawar
2364494193 Add activity selection new algorithm 2024-10-02 12:41:04 +05:30
4 changed files with 56 additions and 3 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus - id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2 rev: v0.7.3
hooks: hooks:
- id: ruff - id: ruff
- id: ruff-format - id: ruff-format
@ -42,7 +42,7 @@ repos:
pass_filenames: false pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject - repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22 rev: v0.23
hooks: hooks:
- id: validate-pyproject - id: validate-pyproject

View File

@ -526,6 +526,7 @@
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py) * [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)
## Greedy Methods ## Greedy Methods
* [Activity Selection](greedy_methods/activity_selection.py)
* [Best Time To Buy And Sell Stock](greedy_methods/best_time_to_buy_and_sell_stock.py) * [Best Time To Buy And Sell Stock](greedy_methods/best_time_to_buy_and_sell_stock.py)
* [Fractional Cover Problem](greedy_methods/fractional_cover_problem.py) * [Fractional Cover Problem](greedy_methods/fractional_cover_problem.py)
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py) * [Fractional Knapsack](greedy_methods/fractional_knapsack.py)

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"): def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep." "Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115 return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17): def random_puzzle(assignments=17):

View File

@ -0,0 +1,52 @@
"""
The Activity Selection Problem is a classic problem in which a set of activities,
each with a start and end time, needs to be scheduled in such a way that
the maximum number of non-overlapping activities is selected.
This is a greedy algorithm where at each step,
we choose the activity that finishes the earliest
and does not conflict with previously selected activities.
Wikipedia: https://en.wikipedia.org/wiki/Activity_selection_problem
"""
def activity_selection(activities: list[tuple[int, int]]) -> list[tuple[int, int]]:
"""
Solve the Activity Selection Problem using a greedy algorithm by selecting
the maximum number of non-overlapping activities from a list of activities.
Parameters:
activities: A list of tuples where each tuple contains
the start and end times of an activity.
Returns:
A list of selected activities that are non-overlapping.
Example:
>>> activity_selection([(1, 3), (2, 5), (3, 9), (6, 8)])
[(1, 3), (6, 8)]
>>> activity_selection([(0, 6), (1, 4), (3, 5), (5, 7), (5, 9), (8, 9)])
[(1, 4), (5, 7), (8, 9)]
>>> activity_selection([(1, 2), (2, 4), (3, 5), (0, 6)])
[(1, 2), (2, 4)]
>>> activity_selection([(5, 9), (1, 2), (3, 4), (0, 6)])
[(1, 2), (3, 4), (5, 9)]
"""
# Step 1: Sort the activities by their end time
sorted_activities = sorted(activities, key=lambda activity: activity[1])
# Step 2: Select the first activity (the one that finishes the earliest)
# as the initial activity
selected_activities = [sorted_activities[0]]
# Step 3: Iterate through the sorted activities and select the ones
# that do not overlap with the last selected activity
for i in range(1, len(sorted_activities)):
if sorted_activities[i][0] >= selected_activities[-1][1]:
selected_activities.append(sorted_activities[i])
return selected_activities