Spaces:
Sleeping
Sleeping
File size: 4,515 Bytes
1b95eb6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | from utils import to_tuple_set, is_connected
def validate_input(dimension, blocks, max_steps):
"""
Validate request input before running the compaction algorithm.
"""
if dimension not in [2, 3]:
return {
"success": False,
"message": "Dimension must be 2 or 3.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "INVALID_DIMENSION"
}
if not blocks:
return {
"success": False,
"message": "Blocks cannot be empty.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "EMPTY_CONFIGURATION"
}
if not isinstance(max_steps, int) or max_steps <= 0:
return {
"success": False,
"message": "max_steps must be a positive integer.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "INVALID_MAX_STEPS"
}
for block in blocks:
if not isinstance(block, list):
return {
"success": False,
"message": "Each block must be a list of coordinates.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "INVALID_BLOCK_FORMAT"
}
if len(block) != dimension:
return {
"success": False,
"message": f"Each block must have exactly {dimension} coordinates.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "INVALID_BLOCK_DIMENSION"
}
for value in block:
if not isinstance(value, int):
return {
"success": False,
"message": "All coordinates must be integers.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "INVALID_COORDINATE_TYPE"
}
if value < 0:
return {
"success": False,
"message": "Negative coordinates are not supported.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "NEGATIVE_COORDINATES"
}
blocks_set = to_tuple_set(blocks)
if len(blocks_set) != len(blocks):
return {
"success": False,
"message": "Duplicate block coordinates found.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "DUPLICATE_BLOCKS"
}
if not is_connected(blocks_set, dimension):
return {
"success": False,
"message": "Initial configuration is not connected.",
"dimension": dimension,
"algorithm": "greedy_potential",
"initial": None,
"steps": [],
"final": None,
"total_steps": 0,
"status": "invalid_input",
"error_code": "NOT_CONNECTED"
}
return None |