From 975d6af087321adc81e8964dab0eeddd46db2381 Mon Sep 17 00:00:00 2001 From: shaman Date: Fri, 27 Oct 2023 17:18:38 +0530 Subject: [PATCH 01/11] spiralmatrix --- dynamic_programming/spiralmatrix.py | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 dynamic_programming/spiralmatrix.py diff --git a/dynamic_programming/spiralmatrix.py b/dynamic_programming/spiralmatrix.py new file mode 100644 index 000000000000..b0d92a847f21 --- /dev/null +++ b/dynamic_programming/spiralmatrix.py @@ -0,0 +1,43 @@ +def spiral_order(matrix): + result = [] + if not matrix: + return result + + rows, cols = len(matrix), len(matrix[0]) + top, bottom, left, right = 0, rows - 1, 0, cols - 1 + + while top <= bottom and left <= right: + # Traverse from left to right + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + + # Traverse from top to bottom + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + + if top <= bottom: + # Traverse from right to left + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + if left <= right: + # Traverse from bottom to top + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 + + return result + +# Example usage: +matrix = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16] +] + +result = spiral_order(matrix) +print(' '.join(map(str, result))) From e3806082ec6dadae771901712632dcc80f009af2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:52:31 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/spiralmatrix.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/spiralmatrix.py b/dynamic_programming/spiralmatrix.py index b0d92a847f21..6dfbc9399a65 100644 --- a/dynamic_programming/spiralmatrix.py +++ b/dynamic_programming/spiralmatrix.py @@ -31,13 +31,9 @@ def spiral_order(matrix): return result + # Example usage: -matrix = [ - [1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12], - [13, 14, 15, 16] -] +matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] result = spiral_order(matrix) -print(' '.join(map(str, result))) +print(" ".join(map(str, result))) From 9facd99d963a301e362dd1e4be5e485fdc39ab7c Mon Sep 17 00:00:00 2001 From: shaman Date: Fri, 27 Oct 2023 22:42:30 +0530 Subject: [PATCH 03/11] graphcolouring --- divide_and_conquer/graphcoloring.py | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 divide_and_conquer/graphcoloring.py diff --git a/divide_and_conquer/graphcoloring.py b/divide_and_conquer/graphcoloring.py new file mode 100644 index 000000000000..cfa1d64e2573 --- /dev/null +++ b/divide_and_conquer/graphcoloring.py @@ -0,0 +1,46 @@ +from collections import defaultdict +class Graph: + def __init__(self, subjects): + self.subjects = subjects + self.graph = defaultdict(list) + def add_edge(self, subject1, subject2): + self.graph[subject1].append(subject2) + self.graph[subject2].append(subject1) + def graph_coloring(self): + color_map = {} + available_colors = set(range(1, len(self.subjects) + 1)) + for subject in self.subjects: + used_colors = set() + + for neighbor in self.graph[subject]: + if neighbor in color_map: + used_colors.add(color_map[neighbor]) + + available_color = available_colors - used_colors + if available_color: + color_map[subject] = min(available_color) + else: +# If no available color, assign a new color + color_map[subject] = len(available_colors) + 1 + available_colors.add(color_map[subject]) + return color_map + def get_minimum_time_slots(self): + color_map = self.graph_coloring() + return max(color_map.values()) +# Example usage +subjects = ['Math', 'Physics', 'Chemistry', 'Biology'] +students = { +'Math': ['Alice', 'Bob', 'Charlie'], +'Physics': ['Alice', 'Charlie', 'David'], +'Chemistry': ['Bob', 'Charlie', 'Eve'], +'Biology': ['Alice', 'David', 'Eve'] +} +graph = Graph(subjects) +graph.add_edge('Math', 'Physics') +graph.add_edge('Math', 'Chemistry') +graph.add_edge('Physics', 'Chemistry') +graph.add_edge('Physics', 'Biology') + + +minimum_time_slots = graph.get_minimum_time_slots() +print(f"Minimum time slots needed: {minimum_time_slots}") \ No newline at end of file From f0f248b6e2fafc3a728fba61a33bbf2d4e565972 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:15:42 +0000 Subject: [PATCH 04/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/graphcoloring.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/divide_and_conquer/graphcoloring.py b/divide_and_conquer/graphcoloring.py index cfa1d64e2573..2c42913fd781 100644 --- a/divide_and_conquer/graphcoloring.py +++ b/divide_and_conquer/graphcoloring.py @@ -1,11 +1,15 @@ from collections import defaultdict + + class Graph: def __init__(self, subjects): self.subjects = subjects self.graph = defaultdict(list) + def add_edge(self, subject1, subject2): self.graph[subject1].append(subject2) self.graph[subject2].append(subject1) + def graph_coloring(self): color_map = {} available_colors = set(range(1, len(self.subjects) + 1)) @@ -20,27 +24,30 @@ def graph_coloring(self): if available_color: color_map[subject] = min(available_color) else: -# If no available color, assign a new color + # If no available color, assign a new color color_map[subject] = len(available_colors) + 1 available_colors.add(color_map[subject]) return color_map + def get_minimum_time_slots(self): color_map = self.graph_coloring() return max(color_map.values()) + + # Example usage -subjects = ['Math', 'Physics', 'Chemistry', 'Biology'] +subjects = ["Math", "Physics", "Chemistry", "Biology"] students = { -'Math': ['Alice', 'Bob', 'Charlie'], -'Physics': ['Alice', 'Charlie', 'David'], -'Chemistry': ['Bob', 'Charlie', 'Eve'], -'Biology': ['Alice', 'David', 'Eve'] + "Math": ["Alice", "Bob", "Charlie"], + "Physics": ["Alice", "Charlie", "David"], + "Chemistry": ["Bob", "Charlie", "Eve"], + "Biology": ["Alice", "David", "Eve"], } graph = Graph(subjects) -graph.add_edge('Math', 'Physics') -graph.add_edge('Math', 'Chemistry') -graph.add_edge('Physics', 'Chemistry') -graph.add_edge('Physics', 'Biology') +graph.add_edge("Math", "Physics") +graph.add_edge("Math", "Chemistry") +graph.add_edge("Physics", "Chemistry") +graph.add_edge("Physics", "Biology") minimum_time_slots = graph.get_minimum_time_slots() -print(f"Minimum time slots needed: {minimum_time_slots}") \ No newline at end of file +print(f"Minimum time slots needed: {minimum_time_slots}") From f76c91c440744f8787f98b1d65fbed3cb082fa75 Mon Sep 17 00:00:00 2001 From: shaman Date: Fri, 27 Oct 2023 22:52:11 +0530 Subject: [PATCH 05/11] graphcolor --- divide_and_conquer/{graphcoloring.py => graphcolor.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename divide_and_conquer/{graphcoloring.py => graphcolor.py} (100%) diff --git a/divide_and_conquer/graphcoloring.py b/divide_and_conquer/graphcolor.py similarity index 100% rename from divide_and_conquer/graphcoloring.py rename to divide_and_conquer/graphcolor.py From 4d1d54c984e925ba4d93e11bd78396ba6624db53 Mon Sep 17 00:00:00 2001 From: shaman Date: Fri, 27 Oct 2023 23:05:52 +0530 Subject: [PATCH 06/11] graph color --- divide_and_conquer/graphcolor.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/divide_and_conquer/graphcolor.py b/divide_and_conquer/graphcolor.py index 2c42913fd781..7b3de5fd85af 100644 --- a/divide_and_conquer/graphcolor.py +++ b/divide_and_conquer/graphcolor.py @@ -1,16 +1,16 @@ from collections import defaultdict - +from typing import Dict, List class Graph: - def __init__(self, subjects): + def __init__(self, subjects: List[str]) -> None: self.subjects = subjects self.graph = defaultdict(list) - def add_edge(self, subject1, subject2): + def add_edge(self, subject1: str, subject2: str) -> None: self.graph[subject1].append(subject2) self.graph[subject2].append(subject1) - def graph_coloring(self): + def graph_coloring(self) -> Dict[str, int]: color_map = {} available_colors = set(range(1, len(self.subjects) + 1)) for subject in self.subjects: @@ -21,15 +21,15 @@ def graph_coloring(self): used_colors.add(color_map[neighbor]) available_color = available_colors - used_colors - if available_color: - color_map[subject] = min(available_color) - else: - # If no available color, assign a new color - color_map[subject] = len(available_colors) + 1 - available_colors.add(color_map[subject]) + if available_color: + color_map[subject] = min(available_color) + else: + # If no available color, assign a new color + color_map[subject] = len(available_colors) + 1 + available_colors.add(color_map[subject]) return color_map - def get_minimum_time_slots(self): + def get_minimum_time_slots(self) -> int: color_map = self.graph_coloring() return max(color_map.values()) @@ -48,6 +48,5 @@ def get_minimum_time_slots(self): graph.add_edge("Physics", "Chemistry") graph.add_edge("Physics", "Biology") - minimum_time_slots = graph.get_minimum_time_slots() print(f"Minimum time slots needed: {minimum_time_slots}") From 401b47105f9da490fcb706292094b937cdf98aee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:37:10 +0000 Subject: [PATCH 07/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/graphcolor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/divide_and_conquer/graphcolor.py b/divide_and_conquer/graphcolor.py index 7b3de5fd85af..e99d830de343 100644 --- a/divide_and_conquer/graphcolor.py +++ b/divide_and_conquer/graphcolor.py @@ -1,6 +1,7 @@ from collections import defaultdict from typing import Dict, List + class Graph: def __init__(self, subjects: List[str]) -> None: self.subjects = subjects From b8c2629a9e22b00dfef836f2eace09f2bec97c17 Mon Sep 17 00:00:00 2001 From: shaman Date: Fri, 27 Oct 2023 23:10:16 +0530 Subject: [PATCH 08/11] graphcolor --- divide_and_conquer/graphcolor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/divide_and_conquer/graphcolor.py b/divide_and_conquer/graphcolor.py index e99d830de343..1eceea840968 100644 --- a/divide_and_conquer/graphcolor.py +++ b/divide_and_conquer/graphcolor.py @@ -1,9 +1,9 @@ from collections import defaultdict -from typing import Dict, List +from typing import dict, list class Graph: - def __init__(self, subjects: List[str]) -> None: + def __init__(self, subjects: list[str]) -> None: self.subjects = subjects self.graph = defaultdict(list) @@ -11,7 +11,7 @@ def add_edge(self, subject1: str, subject2: str) -> None: self.graph[subject1].append(subject2) self.graph[subject2].append(subject1) - def graph_coloring(self) -> Dict[str, int]: + def graph_coloring(self) -> dict[str, int]: color_map = {} available_colors = set(range(1, len(self.subjects) + 1)) for subject in self.subjects: From 4b65227f7e276c4336ad0bb3b3758a67734a9b9e Mon Sep 17 00:00:00 2001 From: shaman Date: Fri, 27 Oct 2023 23:19:13 +0530 Subject: [PATCH 09/11] graphcolor --- divide_and_conquer/graphcolor.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/divide_and_conquer/graphcolor.py b/divide_and_conquer/graphcolor.py index 1eceea840968..a5897782e25b 100644 --- a/divide_and_conquer/graphcolor.py +++ b/divide_and_conquer/graphcolor.py @@ -1,13 +1,23 @@ from collections import defaultdict from typing import dict, list - class Graph: def __init__(self, subjects: list[str]) -> None: + """ + Initialize a Graph instance with a list of subjects. + + :param subjects: A list of subjects to be represented in the graph. + """ self.subjects = subjects self.graph = defaultdict(list) def add_edge(self, subject1: str, subject2: str) -> None: + """ + Add an edge between two subjects in the graph. + + :param subject1: The first subject to connect. + :param subject2: The second subject to connect. + """ self.graph[subject1].append(subject2) self.graph[subject2].append(subject1) @@ -30,11 +40,11 @@ def graph_coloring(self) -> dict[str, int]: available_colors.add(color_map[subject]) return color_map + def get_minimum_time_slots(self) -> int: color_map = self.graph_coloring() return max(color_map.values()) - # Example usage subjects = ["Math", "Physics", "Chemistry", "Biology"] students = { @@ -49,5 +59,7 @@ def get_minimum_time_slots(self) -> int: graph.add_edge("Physics", "Chemistry") graph.add_edge("Physics", "Biology") -minimum_time_slots = graph.get_minimum_time_slots() -print(f"Minimum time slots needed: {minimum_time_slots}") +# Example doctest for add_edge method +if __name__ == "__main__": + import doctest + doctest.testmod() From 4b39eae3008cae24d67e58f815c01accb5389481 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:50:06 +0000 Subject: [PATCH 10/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/graphcolor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/divide_and_conquer/graphcolor.py b/divide_and_conquer/graphcolor.py index a5897782e25b..428a8ee74848 100644 --- a/divide_and_conquer/graphcolor.py +++ b/divide_and_conquer/graphcolor.py @@ -1,6 +1,7 @@ from collections import defaultdict from typing import dict, list + class Graph: def __init__(self, subjects: list[str]) -> None: """ @@ -40,11 +41,11 @@ def graph_coloring(self) -> dict[str, int]: available_colors.add(color_map[subject]) return color_map - def get_minimum_time_slots(self) -> int: color_map = self.graph_coloring() return max(color_map.values()) + # Example usage subjects = ["Math", "Physics", "Chemistry", "Biology"] students = { @@ -62,4 +63,5 @@ def get_minimum_time_slots(self) -> int: # Example doctest for add_edge method if __name__ == "__main__": import doctest + doctest.testmod() From ba3aadb519404ac47e6b5642334aee117d5f05bd Mon Sep 17 00:00:00 2001 From: shaman Date: Sat, 28 Oct 2023 10:51:52 +0530 Subject: [PATCH 11/11] triangularfuzzyset --- dynamic_programming/triangularfuzz.py | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 dynamic_programming/triangularfuzz.py diff --git a/dynamic_programming/triangularfuzz.py b/dynamic_programming/triangularfuzz.py new file mode 100644 index 000000000000..d4a5e59a5090 --- /dev/null +++ b/dynamic_programming/triangularfuzz.py @@ -0,0 +1,39 @@ +class TriangularFuzzySet: + def __init__(self, a, b, c): + """ + Initializes a triangular fuzzy set with the given parameters. + + :param a: Left point of the fuzzy set. + :param b: Peak point of the fuzzy set. + :param c: Right point of the fuzzy set. + """ + self.a = a + self.b = b + self.c = c + + def membership(self, x): + """ + Calculate the membership degree of a value 'x' in the fuzzy set. + + :param x: The input value to calculate the membership for. + :return: The membership degree of 'x' in the fuzzy set. + """ + if x < self.a or x > self.c: + return 0.0 + elif self.a <= x < self.b: + return (x - self.a) / (self.b - self.a) + elif self.b <= x <= self.c: + return (self.c - x) / (self.c - self.b) + else: + return 1.0 + +# Example usage: +if __name__ == "__main__": + # Create a triangular fuzzy set with parameters (a=2, b=4, c=6) + fuzzy_set = TriangularFuzzySet(2, 4, 6) + + # Calculate the membership degree of values in the set + values = [3, 4, 5, 6, 7] + for value in values: + membership_degree = fuzzy_set.membership(value) + print(f"Membership of {value} in the fuzzy set: {membership_degree}")