|
| 1 | +############################################################# |
| 2 | +# This file contains all functions required for this chapter |
| 3 | +############################################################# |
| 4 | + |
| 5 | + |
| 6 | +# Converts from Celsius to Fahrenheit |
| 7 | +def celsiusToFahrenheit(celsius): |
| 8 | + fahrenheit = (9 / 5) * celsius + 32 |
| 9 | + return fahrenheit |
| 10 | + |
| 11 | + |
| 12 | +# Converts from Fahrenheit to Celsius |
| 13 | +def fahrenheitToCelsius(fahrenheit): |
| 14 | + celsius = (5 / 9) * (fahrenheit - 32) |
| 15 | + return celsius |
| 16 | + |
| 17 | + |
| 18 | +# Converts from feet to meters |
| 19 | +def footToMeter(foot): |
| 20 | + meter = 0.305 * foot |
| 21 | + return meter |
| 22 | + |
| 23 | + |
| 24 | +# Converts from meters to feet |
| 25 | +def meterToFoot(meter): |
| 26 | + foot = meter / 0.305 |
| 27 | + return foot |
| 28 | + |
| 29 | + |
| 30 | +# Check whether number is prime |
| 31 | +def isPrime(number): |
| 32 | + divisor = 2 |
| 33 | + while divisor <= number / 2: |
| 34 | + if number % divisor == 0: |
| 35 | + # If true, number is not prime |
| 36 | + return False # number is not a prime |
| 37 | + divisor += 1 |
| 38 | + return True # number is prime |
| 39 | + |
| 40 | + |
| 41 | +def computeCommission(salesAmount): |
| 42 | + commission = 0 |
| 43 | + if salesAmount > 10000: |
| 44 | + commission = (5000 * 0.08) + (5000 * 0.1) + (salesAmount - 10000) * 0.12 |
| 45 | + elif salesAmount > 5000: |
| 46 | + commission = 5000 * 0.08 + (salesAmount - 5000) * 0.1 |
| 47 | + else: |
| 48 | + commission = salesAmount * 0.08 |
| 49 | + |
| 50 | + return commission |
| 51 | + |
| 52 | + |
| 53 | +def computePi(i): |
| 54 | + sum = 0 |
| 55 | + for j in range(1, i + 1): |
| 56 | + sum += ((-1) ** (j + 1)) / (2 * j - 1) |
| 57 | + |
| 58 | + pi = 4 * sum |
| 59 | + return pi |
| 60 | + |
| 61 | + |
| 62 | +def computeTax(status, taxableIncome): |
| 63 | + tax = 0 |
| 64 | + if status == 0: |
| 65 | + if taxableIncome <= 8350: |
| 66 | + tax = taxableIncome * 0.10 |
| 67 | + elif taxableIncome <= 33950: |
| 68 | + tax = 8350 * 0.10 + taxableIncome - 8350 * 0.15 |
| 69 | + elif taxableIncome <= 82250: |
| 70 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + taxableIncome - 33950 * 0.25 |
| 71 | + elif taxableIncome <= 171550: |
| 72 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 82250 - 33950 \ |
| 73 | + * 0.25 + taxableIncome - 82250 * 0.28 |
| 74 | + elif taxableIncome <= 372950: |
| 75 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 82250 - 33950 \ |
| 76 | + * 0.25 + 171550 - 82250 * 0.28 + taxableIncome - 171550 * 0.33 |
| 77 | + else: |
| 78 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 82250 - 33950 \ |
| 79 | + * 0.25 + 171550 - 82250 * 0.28 + 372950 - 171550 \ |
| 80 | + * 0.33 + taxableIncome - 372950 * 0.35 |
| 81 | + elif status == 1: |
| 82 | + if taxableIncome <= 16700: |
| 83 | + tax = taxableIncome * 0.10 |
| 84 | + elif taxableIncome <= 67900: |
| 85 | + tax = 16700 * 0.10 + taxableIncome - 16700 * 0.15 |
| 86 | + elif taxableIncome <= 137050: |
| 87 | + tax = 16700 * 0.10 + 67900 - 16700 * 0.15 \ |
| 88 | + + taxableIncome - 67900 * 0.25 |
| 89 | + elif taxableIncome <= 208850: |
| 90 | + tax = 16700 * 0.10 + 67900 - 16700 * 0.15 + 137050 - 67900 \ |
| 91 | + * 0.25 + taxableIncome - 137050 * 0.28 |
| 92 | + elif taxableIncome <= 372950: |
| 93 | + tax = 16700 * 0.10 + 67900 - 16700 * 0.15 + 137050 - 67900 \ |
| 94 | + * 0.25 + 208850 - 137050 * 0.28 \ |
| 95 | + + taxableIncome - 208850 * 0.33 |
| 96 | + else: |
| 97 | + tax = 16700 * 0.10 + 67900 - 16700 * 0.15 + 137050 - 67900 \ |
| 98 | + * 0.25 + 208850 - 137050 * 0.28 + 372950 - 208850 \ |
| 99 | + * 0.33 + taxableIncome - 372950 * 0.35 |
| 100 | + elif status == 2: |
| 101 | + if taxableIncome <= 8350: |
| 102 | + tax = taxableIncome * 0.10 |
| 103 | + elif taxableIncome <= 33950: |
| 104 | + tax = 8350 * 0.10 + taxableIncome - 8350 * 0.15 |
| 105 | + elif taxableIncome <= 68525: |
| 106 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 \ |
| 107 | + + taxableIncome - 33950 * 0.25 |
| 108 | + elif taxableIncome <= 104425: |
| 109 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 68525 - 33950 \ |
| 110 | + * 0.25 + taxableIncome - 68525 * 0.28 |
| 111 | + elif taxableIncome <= 186475: |
| 112 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 68525 - 33950 \ |
| 113 | + * 0.25 + 104425 - 68525 * 0.28 \ |
| 114 | + + taxableIncome - 104425 * 0.33 |
| 115 | + else: |
| 116 | + tax = 8350 * 0.10 + 33950 - 8350 * 0.15 + 68525 - 33950 \ |
| 117 | + * 0.25 + 104425 - 68525 * 0.28 + 186475 - 104425 \ |
| 118 | + * 0.33 + taxableIncome - 186475 * 0.35 |
| 119 | + elif status == 3: |
| 120 | + if taxableIncome <= 11950: |
| 121 | + tax = taxableIncome * 0.10 |
| 122 | + elif taxableIncome <= 45500: |
| 123 | + tax = 11950 * 0.10 + taxableIncome - 11950 * 0.15 |
| 124 | + elif taxableIncome <= 117450: |
| 125 | + tax = 11950 * 0.10 + 45500 - 11950 * 0.15 \ |
| 126 | + + taxableIncome - 45500 * 0.25 |
| 127 | + elif taxableIncome <= 190200: |
| 128 | + tax = 11950 * 0.10 + 45500 - 11950 * 0.15 + 117450 - 45500 \ |
| 129 | + * 0.25 + taxableIncome - 117450 * 0.28 |
| 130 | + elif taxableIncome <= 372950: |
| 131 | + tax = 11950 * 0.10 + 45500 - 11950 * 0.15 + 117450 - 45500 \ |
| 132 | + * 0.25 + 190200 - 117450 * 0.28 \ |
| 133 | + + taxableIncome - 190200 * 0.33 |
| 134 | + else: |
| 135 | + tax = 11950 * 0.10 + 45500 - 11950 * 0.15 + 117450 - 45500 \ |
| 136 | + * 0.25 + 190200 - 117450 * 0.28 + 372950 - 190200 \ |
| 137 | + * 0.33 + taxableIncome - 372950 * 0.35 |
| 138 | + else: |
| 139 | + print("Error: invalid status") |
| 140 | + return 0 |
| 141 | + |
| 142 | + return tax |
| 143 | + |
| 144 | + |
| 145 | +def numberOfDaysInAYear(year): |
| 146 | + if isLeapYear(year): |
| 147 | + return 366 |
| 148 | + else: |
| 149 | + return 365 |
| 150 | + |
| 151 | + |
| 152 | +# Determine if it is a leap year * |
| 153 | +def isLeapYear(year): |
| 154 | + return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) |
| 155 | + |
| 156 | + |
| 157 | +# Returns true if the sum of any two sides is |
| 158 | +# greater than the third side. |
| 159 | +def isValid(side1, side2, side3): |
| 160 | + b1 = side1 + side2 > side3 |
| 161 | + b2 = side2 + side3 > side1 |
| 162 | + b3 = side1 + side3 > side2 |
| 163 | + return (b1 and b2 and b3) |
| 164 | + |
| 165 | + |
| 166 | +# Returns the area of the triangle. |
| 167 | +def area(side1, side2, side3): |
| 168 | + if isValid(side1, side2, side3): |
| 169 | + s = (side1 + side2 + side3) / 2 |
| 170 | + area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5 |
| 171 | + return area |
| 172 | + |
| 173 | + |
| 174 | +# Return true if point (x2, y2) is on the left side of the |
| 175 | +# directed line from (x0, y0) to (x1, y1) |
| 176 | +def leftOfTheLine(x0, y0, x1, y1, x2, y2): |
| 177 | + d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) |
| 178 | + return d > 0 |
| 179 | + |
| 180 | + |
| 181 | +# Return true if point (x2, y2) is on the same |
| 182 | +# line from (x0, y0) to (x1, y1) |
| 183 | +def onTheSameLine(x0, y0, x1, y1, x2, y2): |
| 184 | + d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) |
| 185 | + return d == 0 |
| 186 | + |
| 187 | + |
| 188 | +# Return true if point (x2, y2) is on the |
| 189 | +# line segment from (x0, y0) to (x1, y1) |
| 190 | +def onTheLineSegment(x0, y0, x1, y1, x2, y2): |
| 191 | + d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) |
| 192 | + return d < 0 |
| 193 | + |
| 194 | + |
| 195 | +def sqrt(n): |
| 196 | + lastGuess = n |
| 197 | + init = 0.0001 |
| 198 | + nextGuess = (lastGuess + (n / lastGuess)) / 2 |
| 199 | + while (lastGuess - nextGuess) >= init: |
| 200 | + lastGuess = nextGuess |
| 201 | + nextGuess = (lastGuess + (n / lastGuess)) / 2 |
| 202 | + |
| 203 | + return nextGuess |
| 204 | + |
| 205 | + |
| 206 | +def isPalindrome(number): |
| 207 | + return number == reverse(number) |
| 208 | + |
| 209 | + |
| 210 | +# Return the reversal of an integer, i.e. reverse(456) returns 654 |
| 211 | +def reverse(number): |
| 212 | + result = 0 |
| 213 | + while number != 0: |
| 214 | + remainder = number % 10 |
| 215 | + result = result * 10 + remainder |
| 216 | + number = number // 10 |
| 217 | + |
| 218 | + return result |
0 commit comments