Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4330 Assignment 4 Write your code for the following problems in a single file named: hw4-lastname.py Please: name your file in exactly this way; lowercase
4330 Assignment 4 Write your code for the following problems in a single file named: hw4-lastname.py Please: name your file in exactly this way; lowercase 'hw', a dash (not an underscore), and NO SPACES in the filename! You must complete this problem according to the "Coding Process which follows. It will be graded according to that rubric. Follow Steps I, II, and III of that process before you begin writing any code at all - it will help you answer many questions you may have about the problem (1) (20 points) Write a function named sum_of_squares (M) which takes a single argument M (a positive integer), and prints out all solutions to the equation M = a + b2, with a, b EN where N = {1,2,3,...}. Additionally, the function should return the number of solutions At the bottom of the file, insert the following snippet of code which you may use to test your function (and I will use to test your function): n=1 while n >= 1: n = int(input ("Enter a positive integer n: ")) numsq = sum_of_squares (n) print("{0} can be written as a sum of squares in {1} ways.". format(n, numsq)) Hints: For example, if M = 50 there are three solutions to M = 2 +62, with a, b E N: 50 = 12 + 72, 50 52 +52, 50 72 +12 - = So in this case, your function should print those three solutions and return the value 3. You can 'nest' loops, as in the following code snippet; run it, and see what it does. for a in range (1,10): for b in range (1,10): print("a={0}, b={1}".format(a,b)) Also note: since = is the assignment operator in Python, there is a different operator == for testing equality: if a**2 + b**2 == n: print("{0}**2 + {1}** 2 {2}.".format(a,b,n)) 4330 A Structured Coding Process 2 This describes the process that you are to apply to every assignment this semester, after the first two. It will be used as a grading rubric for your assignments as well. In particular, note that: 60% of the available credit does not depend explcitly on any programming at all. A program (working or not) without (I), (II), and (III) as described below can only earn up to 40% of the available credit. (I) Understand the problem (20%): Read and think about the problem description, and work out an example of your own choos- ing by hand. It should be a 'small' enough example that working it out by hand is reasonable, yet "large enough to capture most things that you think might happen. Include this in com- ments at the top of your Python program file. (II) Develop an approach: (20%): Explain, in words, how to solve a general instance of the problem by hand. Include this in comments at the top of your Python program file. (III) Test your approach by hand: (20%): Take the method you just described, and apply it to a different example by hand. Verify that it works correctly. Include this in comments at the top of your Python program file. (IV) Code it: (20%): Translate your "in words" method into Python code. (V) Test and debug: (20%): Attempt to use your code on the examples you already worked out by hand, and several more. Try hard to think of examples where something could fail. If it fails on any of those, determine whether the problem comes from (III) or (IV), by adding print statements, if necessary, and working the example by hand. You will be required to go through these steps for each assignment from Assignment 3 onward. You must clearly indicate what you've done for Steps (I), (II), and (III) in comments at the top of your Python program file. The code itself will be used to grade steps (IV) and (V). 4330 Assignment 4 Write your code for the following problems in a single file named: hw4-lastname.py Please: name your file in exactly this way; lowercase 'hw', a dash (not an underscore), and NO SPACES in the filename! You must complete this problem according to the "Coding Process which follows. It will be graded according to that rubric. Follow Steps I, II, and III of that process before you begin writing any code at all - it will help you answer many questions you may have about the problem (1) (20 points) Write a function named sum_of_squares (M) which takes a single argument M (a positive integer), and prints out all solutions to the equation M = a + b2, with a, b EN where N = {1,2,3,...}. Additionally, the function should return the number of solutions At the bottom of the file, insert the following snippet of code which you may use to test your function (and I will use to test your function): n=1 while n >= 1: n = int(input ("Enter a positive integer n: ")) numsq = sum_of_squares (n) print("{0} can be written as a sum of squares in {1} ways.". format(n, numsq)) Hints: For example, if M = 50 there are three solutions to M = 2 +62, with a, b E N: 50 = 12 + 72, 50 52 +52, 50 72 +12 - = So in this case, your function should print those three solutions and return the value 3. You can 'nest' loops, as in the following code snippet; run it, and see what it does. for a in range (1,10): for b in range (1,10): print("a={0}, b={1}".format(a,b)) Also note: since = is the assignment operator in Python, there is a different operator == for testing equality: if a**2 + b**2 == n: print("{0}**2 + {1}** 2 {2}.".format(a,b,n)) 4330 A Structured Coding Process 2 This describes the process that you are to apply to every assignment this semester, after the first two. It will be used as a grading rubric for your assignments as well. In particular, note that: 60% of the available credit does not depend explcitly on any programming at all. A program (working or not) without (I), (II), and (III) as described below can only earn up to 40% of the available credit. (I) Understand the problem (20%): Read and think about the problem description, and work out an example of your own choos- ing by hand. It should be a 'small' enough example that working it out by hand is reasonable, yet "large enough to capture most things that you think might happen. Include this in com- ments at the top of your Python program file. (II) Develop an approach: (20%): Explain, in words, how to solve a general instance of the problem by hand. Include this in comments at the top of your Python program file. (III) Test your approach by hand: (20%): Take the method you just described, and apply it to a different example by hand. Verify that it works correctly. Include this in comments at the top of your Python program file. (IV) Code it: (20%): Translate your "in words" method into Python code. (V) Test and debug: (20%): Attempt to use your code on the examples you already worked out by hand, and several more. Try hard to think of examples where something could fail. If it fails on any of those, determine whether the problem comes from (III) or (IV), by adding print statements, if necessary, and working the example by hand. You will be required to go through these steps for each assignment from Assignment 3 onward. You must clearly indicate what you've done for Steps (I), (II), and (III) in comments at the top of your Python program file. The code itself will be used to grade steps (IV) and (V)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started