Why can't LLM's solve this coding problem?
GPT4, Claude 3 Opus, Gemini Advanced, and Meta AI all fail at this, which surprised me. Even after pointing out the flaws in their initial solutions, I can't get them to give me a correct one. They clearly understand the problem specification, and the correct solution is only 8 lines of code. Curious if anyone has a theory why they find this so hard.
Here is the prompt:
I need a function that takes two integer parameters named TOTAL and MAX. The function should return a list of integers, each between 1 and MAX inclusive, that sum to TOTAL. The list should contain as few elements as possible, but the numbers within it should also be as close to each other as possible, and be in descending order. Examples:
TOTAL = 7, MAX = 1, RESULT = [1, 1, 1, 1, 1, 1, 1]
TOTAL = 7, MAX = 2, RESULT = [2, 2, 2, 1]
TOTAL = 7, MAX = 3, RESULT = [3, 2, 2]
TOTAL = 7, MAX = 4, RESULT = [4, 3]
TOTAL = 7, MAX = 5, RESULT = [4, 3]
TOTAL = 7, MAX = 6, RESULT = [4, 3]
TOTAL = 7, MAX = 7, RESULT = [7]
TOTAL = 7, MAX = 8, RESULT = [7]
Please write the function in python.They usually return an incorrect solution that adds MAX until the final element, resulting in e.g. [6, 1] for TOTAL=7,MAX=6 instead of the correct [4, 3]. The correct solution is:
def distribute(total, max):
count = total // max
remainder = total % max
if remainder == 0:
return [max] * count
base = total // (count + 1)
left_over = total % (count + 1)
return [(base + 1 if i <= left_over else base) for i in range(1, count + 2)]