Module 3 Programming Assignement issue

Hi @M4a,

There is an issue with GPT 4o for C2 (it is pointing to 3.5), we are working on it with Coursera and we soon will have it fixed.

This was an interesting assignment and took a bit of time for me.
But solved it.

  • Got Magic summation is equal to: 50 with unit test pass in assignment 1. Took a lot of time to understand what’s going on. Main culprit was the division operator and not actively watching which env I have activated when I see an error. Once I knew what the exact issues are, I rewrote a very clear prompt and this time both in lab GPT-4o and Gemini 1.5 pro were able to clearly provide the correct code in few shot prompts. You definitely cant go in blindly without some knowledge of Python2 vs Python3 differences.
  • Got the dependency one correct very fast : Assignment 2. This was easy.

moving to Course 3.

Unfortunately i am still having trouble with Assignment 1 (Assignment 2 was indeed easy):

  1. I updated those parts where all the necessary information is mentioned in the code comments.
  2. The division issue was mentioned in the community so i told the LLM to keep an eye on it.

Now my code works, but as some others, i get different sums.

It seems the LLM’s swing around these topics without finding any working solution:
To ensure consistent results across Python versions, let’s make sure:

  1. Index Handling: Remove indices correctly without affecting subsequent indices.
  2. Random Number Generation: Ensure randomness is consistent.

So, any hints where the problem might be? :thinking:

A couple of suggestions:

  1. Check your indexing. For me, the first version of code that the llm generated was using sorted list of indices to remove. In the py2.7 implementation, that is not the case. It deletes the elements inplace, so indices shift after deletion.
  2. Check the range for your indices to remove. Mine was off by 1 so I matched it to py2.7 implementation.
  3. Division - you can launch the consoles for py2 or py3 and run code snippets to see the difference. Try 3/2 in py2 vs py3 and you will see the difference. You will have to find the alternative operator in py3 which gives the same results as py2.
    image
  4. When you find the operator, start a new thread with the LLM and ask it to always use the new operator for the solution. I noticed that to pass the unit test, LLM would start using rounding and adjusting indices etc. It is not required.
  5. Add debug statements and try to log something like this:
    Initial list: [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]
    Indices to remove: {1, 3, 5, 6, 7, 9, 12, 14, 15, 16, 17, 19, 20, 21, 22, 23, 25, 28, 29}
    deleting index 1
    updated list = [1, 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]
    deleting index 3
    updated list = [1, 3, 4, 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]
    deleting index 5
    updated list = [1, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
    deleting index 6
    updated list = [1, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
    deleting index 7
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
    deleting index 9
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
    deleting index 12
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
    deleting index 14
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 17, 18, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30]
    deleting index 15
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 17, 18, 20, 21, 23, 25, 26, 27, 28, 29, 30]
    deleting index 16
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 17, 18, 20, 21, 23, 25, 27, 28, 29, 30]
    deleting index 17
    updated list = [1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 17, 18, 20, 21, 23, 25, 27, 29, 30]
    step 1: 3/1 = 3
    step 2: 4/3 = 1
    step 3: 6/4 = 1
    step 4: 7/6 = 1
    step 5: 9/7 = 1
    step 6: 11/9 = 1
    step 7: 13/11 = 1
    step 8: 14/13 = 1
    step 9: 16/14 = 1
    step 10: 17/16 = 1
    step 11: 18/17 = 1
    step 12: 20/18 = 1
    step 13: 21/20 = 1
    step 14: 23/21 = 1
    step 15: 25/23 = 1
    step 16: 27/25 = 1
    step 17: 29/27 = 1
    step 18: 30/29 = 1
    Magic summation is equal to: 50.
1 Like

There is an issue with the part_1 assignment script itself. Original script for Python 2.7 is using set() function to generate indices_to_remove and then iterates over that set, without applying any sorting to it.

Here is what LLM has to say about iteration order in sets in 2.7 and 3.6:

  • In Python 2.7, the order of elements when iterating over a set is arbitrary.
  • In Python 3.6 and later, the insertion order is preserved during iteration (though this is an implementation detail and not guaranteed by the language specification until Python 3.7).

So there is basically no way to guarantee that the iteration order over indices_to_remove will be the same in both versions, unless you can change the 2.7 version, which is prohibited in this assignment.
The fact that the scripts might (and do) behave the same way and produce the same result for n = 30, seed = 10 arguments and pass unit tests attached to this assignment is purely coincidental. You can try the passing solution with the arguments n = 10, seed = 3 and see what happens.

1 Like

Bernard Leong was right.

For part 1
You must use the ChatGPT provided by the course, which is running ChatGPT 3.5, and then the answer is correct.
I don’t see the science behind it, but it works.
I wasted all the afternoon until I found Leong’s advice.

For Part 2
I used Chat GPT o1-mini, because the ChatGPT provided by the course was not accurate and I got the right solution working with Chat GPT o1-mini

The python27 code has bugs in indice generation and removal logic. For the test case, I get 42.

chatGPT explains:

  • Consistent Deletion Order: Since sets preserve insertion order in Python 3.6’s CPython, deleting elements in the order they were added ensures that the deletions occur predictably.
  • Alignment with Test Cases: Your test cases likely expect deletions to happen in the order indices were generated (i.e., the order they were inserted into the set). Preserving this order ensures that the final summation aligns with expected results.

That is correct.

The assignment, however, asks for the following:
“Your objective is to update the function for compatibility with Python 3, maintaining its current functionality as it operates in Python 2.”

As I read it, “maintaining its current functionality” implies getting the same result for any pair of input parameters in both Python 2 and Python 3 versions, and that’s where you’re running into an issue of different set implementations in different versions.

2 Likes

Though it’s noted as fixed earlier in this thread, when I submit the assignment, part 2 returns:

  • Grader Error: Grader feedback not found

Visit the Discussion forum to see if your peers are experiencing or have found resolutions for similar errors. If the error isn’t resolved in 24 hours, please reach out to Coursera through our Help Center.

My submission.tar.gz contains df_convert.py, gen_employee_schedule.py, internal_stats.py, and requirements.txt that works in the terminal. Not sure what it’s complaining about.

Explanation of Changes:

  1. Maintained Original Logic:
  • Kept the structure and logic of the original Python 2 code to ensure the behavior remains consistent.
  1. Python 3 Compatibility:
  • Updated print statements to use the Python 3 syntax with parentheses.
  • Replaced it.next() with next(it) to use the Python 3 iterator protocol.
  1. Preserved Indexing:
  • Ensured indices in indices_to_remove start from 1, matching the original code.
  • Adjusted the deletion of elements to reflect the original behavior, even if it might seem unconventional.
  1. Integer Division:
  • Used // for integer division to mimic Python 2’s behavior in Python 3.

Testing with n = 3 and seed = 23:

Step-by-Step Execution:

  1. Set the Seed:

python

Copy code

numpy.random.seed(23)
  1. Generate num_indices_to_remove:

python

Copy code

num_indices_to_remove = int(numpy.random.random() * n) + 1
# First random number: ~0.517
# num_indices_to_remove = int(0.517 * 3) + 1 = 1 + 1 = 2
  1. Generate indices_to_remove:

python

Copy code

indices_to_remove = set(int(numpy.random.random() * n) + 1 for _ in range(num_indices_to_remove))
# Next random numbers: ~0.947, ~0.765
# Indices: int(0.947 * 3) + 1 = 2 + 1 = 3
#          int(0.765 * 3) + 1 = 2 + 1 = 3
# indices_to_remove = {3}
  1. Initial magic_list:

python

Copy code

magic_list = [1, 2, 3]
  1. Remove Elements:
  • Attempt to remove magic_list[idx] for idx in indices_to_remove.
  • Note: Since idx ranges from 1 to n, and list indices start at 0, when idx = 3, we check if 3 < len(magic_list), which is False for a list of length 3.
  • Therefore, no elements are removed in this case.
  1. Modify Remaining Elements:

python

Copy code

# i = 0
magic_list[0] = magic_list[1] // magic_list[0] = 2 // 1 = 2
# i = 1
magic_list[1] = magic_list[2] // magic_list[1] = 3 // 2 = 1
# magic_list = [2, 1, 3]
  1. Compute Sum:

python

Copy code

magic_summation_value = 2 + 1 + 3 = 6
  1. Output:

plaintext

Copy code

Magic summation is equal to: 6.

Why the Previous Code Gave Incorrect Output:

  • Indexing Differences:
    • The indices in indices_to_remove did not match those in the original code due to differences in how random indices were generated and how elements were deleted from the list.
  • Modification of magic_list:
    • The way elements were removed and modified in the list didn’t reflect the logic of the original code, leading to different results.

Key Takeaways:

  • When Updating Code, Preserve Original Logic:
    • It’s crucial to keep the original algorithm’s behavior intact, even if there are unconventional practices.
  • Pay Attention to Indexing:
    • Indexing errors can lead to significant discrepancies in the output, especially when dealing with deletions from lists.
  • Random Number Generation:
    • Ensure that the method of generating random numbers matches the original code, especially when seeds are used for reproducibility.

How to Run the Script:

  1. Save the Script:
  • Save the code in a file named magic_summation.py.
  1. Run the Script:

bash

Copy code

python magic_summation.py 3 23
  1. Expected Output:

plaintext

Copy code

Magic summation is equal to: 6.

Further Testing:

Feel free to test the script with different values of n and seed to ensure it behaves as expected.

Example:

bash

Copy code

python magic_summation.py 5 42

Expected Output:

plaintext

Copy code

Magic summation is equal to: 7.
1 Like

To resolve this, we need to:

  • Random Number Generator:
  • Using RandomState: Switched back to numpy.random.RandomState(seed) to use the legacy random number generator (MT19937), ensuring consistent random numbers across different NumPy versions.
  • Indices Generation and Deletion:
  • Indices Range from 1 to n Inclusive: The indices in indices_to_remove are from 1 to n inclusive, matching the original code.
  • Replicating Original Deletion Behavior: When deleting elements from magic_list, we use the indices as-is, without adjusting for zero-based indexing. This replicates the original code’s off-by-one error.
  • Avoiding Index Errors: The condition if idx < len(magic_list) ensures we don’t attempt to delete an index that’s out of range.
  • Consistency with Original Code:
  • Maintaining Original Logic: By keeping the indices and deletion logic the same as the original code, including any off-by-one inconsistencies, we ensure that the program produces the expected output.

Thank you aitarmak! this is helpful! At end of the day, we still need print to help us debug and solve the issue instead of GPT :grinning:

I used ChatGPT o1-preview to solve the assignment_part_1. The suggested changes by [Olu_Idiakhoa] mentioned here did the trick. Thanks!

I am stuck on exactly same thing; anyone could get past this issue?
any post helping to solve it? I failed to solve it myself so far (with different LLMs) and did not see any post that hints to solution of this issue.

Hey all,

I’ve updated the assignment to remove the set function, and performing a “manual” deduplication of the list.

I have also improved the unittests as well as fixed one value that was wrong (the last expected value as 50 but actually it should be 46).

You all may refresh your workspace to get a new copy it it.

I would also suggest you all to explicitly tell an LLM to only make the necessary changes in the code to run it in Pyhon 3. If you let it, it will perform several changes and it will increase the likelihood of changing the final value.

2 Likes

@Lucas, can we put the “refresh your workspace” instructions here on the course forum? Maybe in a Resources “FAQ” for this course?

Currently they’re sort of hidden away on Coursera.

@lucas.coutinho @TMosh
I think there is some issue with the grader with Assignment 1. It still says Failed Test, but the result is the same for me when I run the python27 and python 3 versions. The result from the grader is a float value, that looks incorrect.

I have refreshed the workspace and fetched the latest updated version of the code.

image

For now, I have edited my code to do a division(/) instead of integer division (//). This passes through the grader.

@kapilvarshney

Thanks for catching this. It is fixed now.

Hello I’m still having issues on the grading, where i get the same result using both the python3 and the python2 functions but the grader expects something else.

At the moment I get this from the grader:
Failed test case: magic_summation executed properly, but output is incorrect for parameters n = 19 and seed = 27.
Expected:
33,
but got:
29.

but both functions give me the same answer:
(assignment) jovyan@12933eed2428:~/work/assignment_part_1$ conda activate python27
(python27) jovyan@12933eed2428:~/work/assignment_part_1$ python magic_summation_python27.py
Magic summation is equal to: 29.
(python27) jovyan@12933eed2428:~/work/assignment_part_1$ conda activate assignment
(assignment) jovyan@12933eed2428:~/work/assignment_part_1$ python magic_summation.py
Magic summation is equal to: 29.