Assignment Task:

You are required to solve and write Python code for the task below; and produce a written report to describe and analyse the problem you are solving, explain your solution to the problem, and detail how you have tested and evaluated your solution.

You must use Python version 3 for this assignment; submissions using any other programming langauge or Python version will not be marked and could result in a mark of zero for this assignment.

Task (Magic Square Puzzle):

  • Write a Python program to validate a given Magic Square puzzle board.
  • Background:
    • A typical Magic Square is a n x n square grid filled with integers 1 to n2.
    • The integers should be placed in the grid so that each row, each column, and each of the two diagonals all add up to the same value.
    • Conditions state that no single integer can be used more than once in the grid.
    • The grid can vary in size, but must be a square formation (3x3, 4x4, 5x5, etc.).

A valid magic square: see image.

Examples of some valid Magic Square puzzle boards: see image.

  • The puzzle boards will be supplied to the program in the form of a simple text file, for example: see image.
  • The program should prompt the user for the filename of a text file to open - some Python error handling should be used in case the user enters the name of a file that does not exist.
  • The program should read the contents of the given text file and store this in a suitable Python data structure.
  • Appropriate validation should be in place to ensure the supplied file contains a complete n x n square grid of digits, and ensure the digits are all integers in the range 1 to n2; a suitable error message should be returned where necessary.
  • Assuming a suitable n x n square grid of integers is successfully loaded into the chosen data structure, further checks should then be performed to ensure that the sums of each row, each column, and each of the two diagonals are all equal.
  • If any errors are detected in the puzzle board then details of these errors should be given to the user.
  • If the puzzle board passed all the validation checks, then it should be written into a new text file using the same format as the original input file (see example text files above) - the name for this new text file should be the original filename entered by the user prefixed with "VALID_".
  • You should try to make your Python program as efficient as you can - consider segmenting your code into functions where appropriate for particular tasks.

Report:

  • Produce a written report in a Microsoft Word document (or similar) to describe your program.
  • The report must contain the following sections:
    • Introduction - this should contain some background information, generally summarise the problem being solved in this task and demonstrate clear understanding of the problems.
    • Solution Explanation - this is the main body of your report where you should describe your design and solution to the problem/task outlined in the introduction, and explain any key aspects to the program. You can also use this section to explain your design rationale for certain parts of your code (i.e. why you have done something in a certain way).
    • Testing - explain how you have tested your program, and if appropriate give some sample data and output examples from the program. There should be evidence of your program being tested given either in this section or in the appendix and referred to from this section.
    • Conclusion - use this section to evaluate your work (i.e. your solution to the problem described in the introduction), for example, what has worked well, what would you like to change next time.
    • Appendix - you should copy and paste your entire program code into this final section of the report.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.