Why are all LLMs consistently wrong on this simple Python function?

Hello all, recently, I have been working on consumer/task workload distribution system. As part of it, there is a simple function, which tries to find a suitable consumer to assign new tasks to. It works checking if there are consumers with unassigned tasks. Then, it finds the first consumer which is working on a different from the goal task and returns it. If no such consumer can be found, it returns None.

I have a unit test with 11 test cases for this function.

Implementation of the function is given below:

def modify_assignments( consumer_to_task: Dict[str, str], consumers_order: List[str], target: str, skip_keys: List[str] ) -> Optional[str]: # Check for unassigned first for k in consumers_order: if k in skip_keys: continue if k not in consumer_to_task: return k # Then select the first with different value for k in consumers_order: if k in skip_keys: continue if consumer_to_task.get(k) != target: return k return None

Interestingly, when I asked for code review, all LLMs consistently tried to say that this can be turned into one for loop. Their alternative implementations all failed some test cases, though – because to know if there are unassigned consumers, we need to iterate over all of them first, then check for key with a different values. However, this simple fact evades all major LLMs:

Gemini: The two loops can be combined into a single loop for a more concise implementation, although this might slightly reduce readability for some. A single loop would check for the absence of a key first, and if present, check for the value.

ChatGPT: You’re scanning twice. You could merge both loops by prioritizing missing keys first, then mismatched.

Claude: Efficiency consideration: The function iterates through order twice. You could combine both checks into a single loop:

Why would all LLMs consistently fail for such a simple function? I can provide their alternatives and the unit tests, if you are interested.

submitted by /u/dev-ai to r/Python
[link] [comments]


Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *