t_area.py.xhtml 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => 't_area.py',
  21. 'subtitle' => 'Written in <span title="Programming Fundamentals">CS 1101</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalized on 2017-02-08',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <img class="weblog-header-image" src="/img/CC_BY-SA_4.0/y.st./coursework/CS1101/t_area.py.png" alt="The output of the program is &quot;96&quot;." width="671" height="183" />
  25. <blockquote>
  26. <pre># Every good program should begin with a license comment that declares the file
  27. # as being covered by a free software license.
  28. #
  29. # This program is free software: you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation, either version 3 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License
  40. # along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.
  41. # These are the dimentions of our triangle.
  42. base = 12
  43. height = 16
  44. # The formula specified by the assignment was this:
  45. #
  46. ## area = 1/2 * base * height
  47. #
  48. # However, Python uses integer division if both numbers are integers. Basically,
  49. # that means that:
  50. #
  51. ## 1 / 2 == 0
  52. #
  53. # It&apos;s the same as if you used this:
  54. #
  55. ## math.floor(1.0 / 2.0)
  56. #
  57. # We could modify the formula to use floating-point decimals instead of
  58. # integers, but that that would have the unintended effect of causing our
  59. # printed value to be a floating-point decimal. In many cases, that would be the
  60. # correct behaviour, but our answer is an integer. We might as well keep it as
  61. # an integer. Instead of changing the formula, we could also use:
  62. #
  63. ## from __future__ import division
  64. #
  65. # Again though, this would have the effect of changing our printed integer into
  66. # a floating-point decimal.
  67. area = base * height / 2
  68. # If we&apos;d acquired a floating-point answer instead of an integer answer, we&apos;d
  69. # see &quot;.0&quot; appended to the answer when we printed it.
  70. print area
  71. </pre>
  72. </blockquote>
  73. END
  74. );