English subtitles

← Proc Solution - Intro to Computer Science

Get Embed Code
6 Languages

Showing Revision 6 created 05/24/2016 by Udacity Robot.

  1. In order to solve this problem, you need to have a good understanding
  2. of what the original function, proc, does.
  3. So let's take the two cases that really embody the output of this function.
  4. There are 2 inputs, but the output only depends on the value of A.
  5. Specifically, the value of the function test on a.
  6. If test(a) is true, then we return the second parameter b.
  7. If test(a) is false, we return the first parameter a.
  8. That's what we need to check in each of these functions. Let's start looking at them.
  9. In proc1, if test(x), which is the first parameter is true we return y.
  10. This matches with the original function.
  11. If test of the fist parameter is true, we return the second, as we do in proc1.
  12. If test of the first parameter is false, we return that parameter x.
  13. Test x is false, which is equivalent to test a is false,
  14. and in both cases we return a and x,
  15. which are just different names for the same variable in this function.
  16. This is equivalent.
  17. Let's look at this second function--proc2.
  18. Notice here that this condition is based on the value of b.
  19. We know this can't possibly be equivalent to proc, our original function,
  20. because the output here is not dependent on the value of b.
  21. We can tell right away that this is false. This is not equivalent to our original function.
  22. Let's look at proc3.
  23. The structure here is a little different, but let's work our way through it both examples.
  24. In the event that test(a) is true, we set result to b and re-return result.
  25. As our original function did, if test(a) is true, we return b.
  26. If test(a) is false, we simply return result, which was set at the beginning to a.
  27. So the overall functionality is that if test(a) is true, we return a.
  28. This matches with our original function definition.
  29. So this is equivalent to proc.
  30. Now we have the last function--proc4.
  31. It might throw you off that you see 'udacity' in quotes here, which is not present
  32. in any other function, but it's important we work our way through the problem.
  33. If test(a) is true, then not test(a) is false.
  34. We return b.
  35. If test(a) is true, we return b.
  36. That matches with the original definition.
  37. If test(a) is false, not test(a) is true,
  38. so we set b to 'udacity,' but then we return a.
  39. If test(a) is false, we change the value of b,
  40. but we don't do anything with it--we just return a.
  41. The result is the same as our original function proc.