From 2b3d13f264fc8d3ac1dbf828cd440cfbeda5b197 Mon Sep 17 00:00:00 2001 From: Akhila21-6 Date: Sun, 19 Oct 2025 18:57:43 +0530 Subject: [PATCH 1/8] docs: add doctests for min_cost_string_conversion --- strings/min_cost_string_conversion.py | 216 ++++++++++++-------------- 1 file changed, 97 insertions(+), 119 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 87eb5189e16a..762c7aa1e9f2 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -1,12 +1,48 @@ """ Algorithm for calculating the most cost-efficient sequence for converting one string -into another. -The only allowed operations are ---- Cost to copy a character is copy_cost ---- Cost to replace a character is replace_cost ---- Cost to delete a character is delete_cost ---- Cost to insert a character is insert_cost +into another (Levenshtein distance). +The allowed operations are insertion, deletion, or substitution of a single character. """ +import doctest + + +def min_cost_string_conversion(str1: str, str2: str) -> int: + """ + Calculates the minimum cost (standard Levenshtein distance) to convert str1 to str2. + The cost of insertion, deletion, and substitution is 1. The cost of a copy is 0. + + This is a user-friendly wrapper around the more detailed compute_transform_tables. + + Args: + str1: The source string. + str2: The target string. + + Returns: + The minimum number of operations required to convert str1 to str2. + + Doctests: + >>> min_cost_string_conversion("apple", "apply") + 1 + >>> min_cost_string_conversion("sunday", "saturday") + 3 + >>> min_cost_string_conversion("test", "test") + 0 + >>> min_cost_string_conversion("", "") + 0 + >>> min_cost_string_conversion("kitten", "sitting") + 3 + >>> min_cost_string_conversion("flaw", "lawn") + 2 + """ + costs, _ = compute_transform_tables( + source_string=str1, + destination_string=str2, + copy_cost=0, + replace_cost=1, + delete_cost=1, + insert_cost=1, + ) + return costs[-1][-1] def compute_transform_tables( @@ -18,153 +54,95 @@ def compute_transform_tables( insert_cost: int, ) -> tuple[list[list[int]], list[list[str]]]: """ - Finds the most cost efficient sequence - for converting one string into another. - - >>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3) - >>> costs[0][:4] - [0, 3, 6, 9] - >>> costs[2][:4] - [6, 4, 3, 6] - >>> operations[0][:4] - ['0', 'Ic', 'Iu', 'It'] - >>> operations[3][:4] - ['Dt', 'Dt', 'Rtu', 'Ct'] - - >>> compute_transform_tables("", "", 1, 2, 3, 3) - ([[0]], [['0']]) + Finds the most cost efficient sequence for converting one string into another + using dynamic programming with specified costs. + + Doctests: + >>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3) + >>> costs[0][:4] + [0, 3, 6, 9] + >>> costs[2][:4] + [6, 4, 3, 6] + >>> operations[0][:4] + ['0', 'Ic', 'Iu', 'It'] + >>> operations[3][:4] + ['Dt', 'Dt', 'Rtu', 'Ct'] + >>> compute_transform_tables("", "", 1, 2, 3, 3) + ([[0]], [['0']]) """ - source_seq = list(source_string) - destination_seq = list(destination_string) - len_source_seq = len(source_seq) - len_destination_seq = len(destination_seq) + len_source_seq = len(source_string) + len_destination_seq = len(destination_string) costs = [ [0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) ] ops = [ - ["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) + ["0" for _ in range(len_destination_seq + 1)] + for _ in range(len_source_seq + 1) ] for i in range(1, len_source_seq + 1): costs[i][0] = i * delete_cost - ops[i][0] = f"D{source_seq[i - 1]}" + ops[i][0] = f"D{source_string[i - 1]}" - for i in range(1, len_destination_seq + 1): - costs[0][i] = i * insert_cost - ops[0][i] = f"I{destination_seq[i - 1]}" + for j in range(1, len_destination_seq + 1): + costs[0][j] = j * insert_cost + ops[0][j] = f"I{destination_string[j - 1]}" for i in range(1, len_source_seq + 1): for j in range(1, len_destination_seq + 1): - if source_seq[i - 1] == destination_seq[j - 1]: + # Cost of copying or replacing the current character + if source_string[i - 1] == destination_string[j - 1]: costs[i][j] = costs[i - 1][j - 1] + copy_cost - ops[i][j] = f"C{source_seq[i - 1]}" + ops[i][j] = f"C{source_string[i - 1]}" else: costs[i][j] = costs[i - 1][j - 1] + replace_cost - ops[i][j] = f"R{source_seq[i - 1]}" + str(destination_seq[j - 1]) + ops[i][j] = f"R{source_string[i - 1]}{destination_string[j - 1]}" + # Check if deleting from source is cheaper if costs[i - 1][j] + delete_cost < costs[i][j]: costs[i][j] = costs[i - 1][j] + delete_cost - ops[i][j] = f"D{source_seq[i - 1]}" + ops[i][j] = f"D{source_string[i - 1]}" + # Check if inserting into destination is cheaper if costs[i][j - 1] + insert_cost < costs[i][j]: costs[i][j] = costs[i][j - 1] + insert_cost - ops[i][j] = f"I{destination_seq[j - 1]}" + ops[i][j] = f"I{destination_string[j - 1]}" return costs, ops def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: """ - Assembles the transformations based on the ops table. - - >>> ops = [['0', 'Ic', 'Iu', 'It'], - ... ['Dc', 'Cc', 'Iu', 'It'], - ... ['Da', 'Da', 'Rau', 'Rat'], - ... ['Dt', 'Dt', 'Rtu', 'Ct']] - >>> x = len(ops) - 1 - >>> y = len(ops[0]) - 1 - >>> assemble_transformation(ops, x, y) - ['Cc', 'Rau', 'Ct'] - - >>> ops1 = [['0']] - >>> x1 = len(ops1) - 1 - >>> y1 = len(ops1[0]) - 1 - >>> assemble_transformation(ops1, x1, y1) - [] - - >>> ops2 = [['0', 'I1', 'I2', 'I3'], - ... ['D1', 'C1', 'I2', 'I3'], - ... ['D2', 'D2', 'R23', 'R23']] - >>> x2 = len(ops2) - 1 - >>> y2 = len(ops2[0]) - 1 - >>> assemble_transformation(ops2, x2, y2) - ['C1', 'I2', 'R23'] + Assembles the list of transformations based on the ops table. + + Doctests: + >>> ops = [['0', 'Ic', 'Iu', 'It'], + ... ['Dc', 'Cc', 'Iu', 'It'], + ... ['Da', 'Da', 'Rau', 'Rat'], + ... ['Dt', 'Dt', 'Rtu', 'Ct']] + >>> x = len(ops) - 1 + >>> y = len(ops[0]) - 1 + >>> assemble_transformation(ops, x, y) + ['Cc', 'Rau', 'Ct'] + >>> ops1 = [['0']] + >>> x1 = len(ops1) - 1 + >>> y1 = len(ops1[0]) - 1 + >>> assemble_transformation(ops1, x1, y1) + [] """ if i == 0 and j == 0: return [] - elif ops[i][j][0] in {"C", "R"}: + op_code = ops[i][j][0] + if op_code in {"C", "R"}: seq = assemble_transformation(ops, i - 1, j - 1) - seq.append(ops[i][j]) - return seq - elif ops[i][j][0] == "D": + elif op_code == "D": seq = assemble_transformation(ops, i - 1, j) - seq.append(ops[i][j]) - return seq - else: + else: # op_code == "I" seq = assemble_transformation(ops, i, j - 1) - seq.append(ops[i][j]) - return seq + seq.append(ops[i][j]) + return seq if __name__ == "__main__": - _, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2) - - m = len(operations) - n = len(operations[0]) - sequence = assemble_transformation(operations, m - 1, n - 1) - - string = list("Python") - i = 0 - cost = 0 - - with open("min_cost.txt", "w") as file: - for op in sequence: - print("".join(string)) - - if op[0] == "C": - file.write("%-16s" % "Copy %c" % op[1]) # noqa: UP031 - file.write("\t\t\t" + "".join(string)) - file.write("\r\n") - - cost -= 1 - elif op[0] == "R": - string[i] = op[2] - - file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))) # noqa: UP031 - file.write("\t\t" + "".join(string)) - file.write("\r\n") - - cost += 1 - elif op[0] == "D": - string.pop(i) - - file.write("%-16s" % "Delete %c" % op[1]) # noqa: UP031 - file.write("\t\t\t" + "".join(string)) - file.write("\r\n") - - cost += 2 - else: - string.insert(i, op[1]) - - file.write("%-16s" % "Insert %c" % op[1]) # noqa: UP031 - file.write("\t\t\t" + "".join(string)) - file.write("\r\n") - - cost += 2 - - i += 1 - - print("".join(string)) - print("Cost: ", cost) - - file.write("\r\nMinimum cost: " + str(cost)) + # Run the doctests in this file + doctest.testmod() From 4f6c516a8ddf0ed87877b9ba7c6b62071ade0d53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 Oct 2025 13:39:20 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/min_cost_string_conversion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 762c7aa1e9f2..06c1266fb03d 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -3,6 +3,7 @@ into another (Levenshtein distance). The allowed operations are insertion, deletion, or substitution of a single character. """ + import doctest @@ -76,8 +77,7 @@ def compute_transform_tables( [0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) ] ops = [ - ["0" for _ in range(len_destination_seq + 1)] - for _ in range(len_source_seq + 1) + ["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) ] for i in range(1, len_source_seq + 1): From 68d08f8bfe29472499dec169ca8282209e934073 Mon Sep 17 00:00:00 2001 From: Akhila21-6 Date: Mon, 20 Oct 2025 07:43:51 +0530 Subject: [PATCH 3/8] docs: add doctests for min_cost_string_conversion --- strings/min_cost_string_conversion.py | 36 +-------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 762c7aa1e9f2..52d67391ae4f 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -31,8 +31,6 @@ def min_cost_string_conversion(str1: str, str2: str) -> int: 0 >>> min_cost_string_conversion("kitten", "sitting") 3 - >>> min_cost_string_conversion("flaw", "lawn") - 2 """ costs, _ = compute_transform_tables( source_string=str1, @@ -56,19 +54,6 @@ def compute_transform_tables( """ Finds the most cost efficient sequence for converting one string into another using dynamic programming with specified costs. - - Doctests: - >>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3) - >>> costs[0][:4] - [0, 3, 6, 9] - >>> costs[2][:4] - [6, 4, 3, 6] - >>> operations[0][:4] - ['0', 'Ic', 'Iu', 'It'] - >>> operations[3][:4] - ['Dt', 'Dt', 'Rtu', 'Ct'] - >>> compute_transform_tables("", "", 1, 2, 3, 3) - ([[0]], [['0']]) """ len_source_seq = len(source_string) len_destination_seq = len(destination_string) @@ -90,7 +75,6 @@ def compute_transform_tables( for i in range(1, len_source_seq + 1): for j in range(1, len_destination_seq + 1): - # Cost of copying or replacing the current character if source_string[i - 1] == destination_string[j - 1]: costs[i][j] = costs[i - 1][j - 1] + copy_cost ops[i][j] = f"C{source_string[i - 1]}" @@ -98,12 +82,10 @@ def compute_transform_tables( costs[i][j] = costs[i - 1][j - 1] + replace_cost ops[i][j] = f"R{source_string[i - 1]}{destination_string[j - 1]}" - # Check if deleting from source is cheaper if costs[i - 1][j] + delete_cost < costs[i][j]: costs[i][j] = costs[i - 1][j] + delete_cost ops[i][j] = f"D{source_string[i - 1]}" - # Check if inserting into destination is cheaper if costs[i][j - 1] + insert_cost < costs[i][j]: costs[i][j] = costs[i][j - 1] + insert_cost ops[i][j] = f"I{destination_string[j - 1]}" @@ -114,21 +96,6 @@ def compute_transform_tables( def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: """ Assembles the list of transformations based on the ops table. - - Doctests: - >>> ops = [['0', 'Ic', 'Iu', 'It'], - ... ['Dc', 'Cc', 'Iu', 'It'], - ... ['Da', 'Da', 'Rau', 'Rat'], - ... ['Dt', 'Dt', 'Rtu', 'Ct']] - >>> x = len(ops) - 1 - >>> y = len(ops[0]) - 1 - >>> assemble_transformation(ops, x, y) - ['Cc', 'Rau', 'Ct'] - >>> ops1 = [['0']] - >>> x1 = len(ops1) - 1 - >>> y1 = len(ops1[0]) - 1 - >>> assemble_transformation(ops1, x1, y1) - [] """ if i == 0 and j == 0: return [] @@ -144,5 +111,4 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: if __name__ == "__main__": - # Run the doctests in this file - doctest.testmod() + doctest.testmod() \ No newline at end of file From a30f6b726584c5ac06d53cbf736d9721e8441d5f Mon Sep 17 00:00:00 2001 From: Akhila21-6 Date: Tue, 21 Oct 2025 22:32:56 +0530 Subject: [PATCH 4/8] feat: add snake water gun game --- other/snake_water_gun.py | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 other/snake_water_gun.py diff --git a/other/snake_water_gun.py b/other/snake_water_gun.py new file mode 100644 index 000000000000..fbb72d9c9daa --- /dev/null +++ b/other/snake_water_gun.py @@ -0,0 +1,69 @@ +""" +A simple implementation of the Snake, Water, Gun game. +""" +import random +import doctest + + +def snake_water_gun(player_choice: str, computer_choice: str) -> str: + """ + Determines the winner of a Snake, Water, Gun game round. + + Args: + player_choice: The player's choice ('s' for snake, 'w' for water, 'g' for gun). + computer_choice: The computer's choice. + + Returns: + A string indicating the result: "Player wins!", "Computer wins!", or "It's a draw!". + + Doctests: + >>> snake_water_gun('s', 'w') + 'Player wins!' + >>> snake_water_gun('w', 'g') + 'Player wins!' + >>> snake_water_gun('g', 's') + 'Player wins!' + >>> snake_water_gun('w', 's') + 'Computer wins!' + >>> snake_water_gun('s', 's') + "It's a draw!" + """ + if player_choice == computer_choice: + return "It's a draw!" + + if ( + (player_choice == "s" and computer_choice == "w") + or (player_choice == "w" and computer_choice == "g") + or (player_choice == "g" and computer_choice == "s") + ): + return "Player wins!" + else: + return "Computer wins!" + + +def main(): + """ + Main function to run the Snake, Water, Gun game. + """ + print("--- Snake, Water, Gun Game ---") + player_input = ( + input("Enter your choice (s for snake, w for water, g for gun): ").lower().strip() + ) + + if player_input not in ["s", "w", "g"]: + print("Invalid choice. Please choose 's', 'w', or 'g'.") + return + + choices = ["s", "w", "g"] + computer_input = random.choice(choices) + + print(f"\nYou chose: {player_input}") + print(f"Computer chose: {computer_input}\n") + + result = snake_water_gun(player_input, computer_input) + print(result) + + +if __name__ == "__main__": + doctest.testmod() # Run the doctests + main() \ No newline at end of file From cb64f9d3ea4c19f69d60567f21cfbc41a0229195 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Oct 2025 17:07:14 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/snake_water_gun.py | 7 +++++-- strings/min_cost_string_conversion.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/other/snake_water_gun.py b/other/snake_water_gun.py index fbb72d9c9daa..9a4769a90d0a 100644 --- a/other/snake_water_gun.py +++ b/other/snake_water_gun.py @@ -1,6 +1,7 @@ """ A simple implementation of the Snake, Water, Gun game. """ + import random import doctest @@ -47,7 +48,9 @@ def main(): """ print("--- Snake, Water, Gun Game ---") player_input = ( - input("Enter your choice (s for snake, w for water, g for gun): ").lower().strip() + input("Enter your choice (s for snake, w for water, g for gun): ") + .lower() + .strip() ) if player_input not in ["s", "w", "g"]: @@ -66,4 +69,4 @@ def main(): if __name__ == "__main__": doctest.testmod() # Run the doctests - main() \ No newline at end of file + main() diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 0ddf6bbb30eb..1915514747e9 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -111,4 +111,4 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: if __name__ == "__main__": - doctest.testmod() \ No newline at end of file + doctest.testmod() From 66be858daae03640ee943120eb1a44062506f1a3 Mon Sep 17 00:00:00 2001 From: Akhila21-6 Date: Wed, 22 Oct 2025 21:28:02 +0530 Subject: [PATCH 6/8] fix: address ruff linting issues --- other/snake_water_gun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/snake_water_gun.py b/other/snake_water_gun.py index fbb72d9c9daa..264e3bb32eb5 100644 --- a/other/snake_water_gun.py +++ b/other/snake_water_gun.py @@ -1,8 +1,8 @@ """ A simple implementation of the Snake, Water, Gun game. """ -import random import doctest +import random def snake_water_gun(player_choice: str, computer_choice: str) -> str: @@ -14,7 +14,7 @@ def snake_water_gun(player_choice: str, computer_choice: str) -> str: computer_choice: The computer's choice. Returns: - A string indicating the result: "Player wins!", "Computer wins!", or "It's a draw!". + Result: "Player wins!", "Computer wins!", or "It's a draw!". Doctests: >>> snake_water_gun('s', 'w') From 9bfb9abcc144ecf3a66c785f94cc5b3419410469 Mon Sep 17 00:00:00 2001 From: Akhila21-6 Date: Wed, 22 Oct 2025 21:46:05 +0530 Subject: [PATCH 7/8] fix: resolve syntax error for build_docs --- other/snake_water_gun.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/other/snake_water_gun.py b/other/snake_water_gun.py index e4cf17eb78d8..264e3bb32eb5 100644 --- a/other/snake_water_gun.py +++ b/other/snake_water_gun.py @@ -1,11 +1,6 @@ """ A simple implementation of the Snake, Water, Gun game. """ -<<<<<<< HEAD -======= - -import random ->>>>>>> cb64f9d3ea4c19f69d60567f21cfbc41a0229195 import doctest import random @@ -52,9 +47,7 @@ def main(): """ print("--- Snake, Water, Gun Game ---") player_input = ( - input("Enter your choice (s for snake, w for water, g for gun): ") - .lower() - .strip() + input("Enter your choice (s for snake, w for water, g for gun): ").lower().strip() ) if player_input not in ["s", "w", "g"]: @@ -73,4 +66,4 @@ def main(): if __name__ == "__main__": doctest.testmod() # Run the doctests - main() + main() \ No newline at end of file From e50ff0aca6228f15632b2c86e4e4a61563115f03 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 16:16:34 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/snake_water_gun.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/other/snake_water_gun.py b/other/snake_water_gun.py index 264e3bb32eb5..5476e2988d9c 100644 --- a/other/snake_water_gun.py +++ b/other/snake_water_gun.py @@ -1,6 +1,7 @@ """ A simple implementation of the Snake, Water, Gun game. """ + import doctest import random @@ -47,7 +48,9 @@ def main(): """ print("--- Snake, Water, Gun Game ---") player_input = ( - input("Enter your choice (s for snake, w for water, g for gun): ").lower().strip() + input("Enter your choice (s for snake, w for water, g for gun): ") + .lower() + .strip() ) if player_input not in ["s", "w", "g"]: @@ -66,4 +69,4 @@ def main(): if __name__ == "__main__": doctest.testmod() # Run the doctests - main() \ No newline at end of file + main()