Python Reference sheet: Goal: Top-Down Design of a program1. Go over multiple ways to solve a few of the coding bat problems. 2. Designing a function, and breaking it up into subproblems. QUESTION: What is an algorithm? Many of the functions and programs you write are simple enough to complete without designing them. This is because you can easily understand the whole algorithm. A formula, or a simple set of if statements don't seem to require a design. As you write more complex code, you cannot understand the whole algorithm or process all of the consequences at once. This is why it is a good idea to DESIGN your program rather than just hack it together by writing code until it works, even though it may seem like a waste of time for now. This includes writing out your ideas on paper. Your design will be in plain English, which makes it faster and easier to write than actual code. It is also easier to find errors in your logic, and faster to fix them rather than trying to work with a computer language. 3. Consider the following function: DO NOT WRITE CODE until instructed. magicPair(n,m) Look at the ones digit, and tens digit of both n and m. Larger places are ignored. (The hundreds digits or greater are ignored and not considered part of the problem) In order to be a magic pair, at least one of the two digits of n must match m. If there are any matching digits, the numbers are a magic pair only if the sum of the matching digits, equals the sum of the non matching digits. magicPair(12,10) -> True (the 1's match, and the other two add up to 2) magicPair(43,54) -> True (the 4's match, and the other two add up to 8) magicPair(53,31) -> True special case: magicPair(13,13) -> False (the 1's match and 3's match but don't add up to eachother) Top down design: A plain English plan for your program. ***Write out your design, and answer the questions. DO NOT WRITE CODE until you do this.*** 1. TASK: Write your steps out in English, tell the steps to your rubber ducky. QUESTION: Qb. There are some calculations that must be made, and it would be much easier to read code if you named the calculations by creating a function. What helper function(s) would be nice to have before you write your function? 2. Show your steps to two neighbors, and see if you both agree on each other's algorithm. Do you agree on your helper functions? 3. Write test cases for your program. Not my test cases. 4. Write the function, and use your test cases. You must demonstrate via test cases that it works as described. 5. Submit the problem on codingbat in the custom section of problems for the class: codingbat.com/home/konstans@stuy.edu/all |