Download tools/math.py from andrei-sferdian/Agents_Course_Final_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/tools/math.py
- Command line
-
hf download hf://spaces/andrei-sferdian/Agents_Course_Final_Assignment/tools/math.py
-
curl -L -o math.py https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/tools/math.py
1.37 kB
| import json | |
| from langchain_core.tools import tool | |
| def check_commutativity(table_data: list[list[str]]) -> str: | |
| """ | |
| Checks the commutativity of a given table represented as a 2D list. | |
| Args: | |
| table_data (list[list[str]]): A 2D list representing the table to check. | |
| Should not contain the header row, only the data rows. | |
| Returns: | |
| str: A JSON-formatted string containing: | |
| - "commutative" (bool): True if the table is commutative, False otherwise. | |
| - "non_commutative_pairs" (list[tuple[int, int]]): A list of tuples | |
| representing the indices of non-commutative pairs in the table. | |
| If the table is commutative, this list will be empty. | |
| If an error occurs during processing, the function returns an error message. | |
| """ | |
| result = set() | |
| try: | |
| for i, row in enumerate(table_data): | |
| for j, _ in enumerate(row[:i]): | |
| if table_data[i][j] != table_data[j][i]: | |
| result.add((i, j)) | |
| result.add((j, i)) | |
| return json.dumps( | |
| { | |
| "commutative": len(result) == 0, | |
| "non_commutative_pairs": list(result), | |
| } | |
| ) | |
| except Exception as e: | |
| return f"Error: {e}" | |
| if __name__ == "__main__": | |
| pass | |