Advertisement
britneybeatey

homework 2

Jul 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. # SAMS 2018, Programming Sections A and B
  2. #########################################
  3. # Full name: Britney Beatey
  4. # Andrew ID: bbeatey
  5. #########################################
  6.  
  7. # DUE DATE: Sunday July 15TH, 5pm.
  8. # SUBMIT THIS FILE TO AUTOLAB. LATE SUBMISSIONS WILL NOT BE ACCEPTED!
  9.  
  10. # For the functions below, erase the "return 42" line and write the
  11. # appropriate piece of code instead.
  12.  
  13. # IMPORTANT NOTE:
  14. # **You are not allowed to import any modules, use strings, lists, or recursion. **
  15.  
  16.  
  17. # Use the following function to compare floats.
  18. def almostEqual(d1, d2):
  19. epsilon = 10**-8
  20. return (abs(d2 - d1) < epsilon)
  21.  
  22.  
  23. #### PROBLEM 1 ####
  24.  
  25. # Given an integer n, return the number of digits of n.
  26. def digitCount(n):
  27. n = abs(n)
  28. count = 0
  29. if n == 0:
  30. return 1
  31. while (n>0):
  32. n = n // 10
  33. count = count + 1
  34. return count
  35.  
  36. #### PROBLEM 2 ####
  37.  
  38. # Given an integer n, return the sum of its digits.
  39. def digitSum(n):
  40. n = abs(n)
  41. sum = 0
  42. if n == 0:
  43. return 0
  44. while (n>0):
  45. sum = sum + (n % 10)
  46. n = n // 10
  47. return sum
  48.  
  49.  
  50. #### PROBLEM 3 ####
  51.  
  52. # Definition: For a positive integer n, n factorial, denoted n!,
  53. # is the product n*(n-1)*(n-2)*...*1. If n = 0, then define 0! as 1.
  54. # Given an integer n (which you can assume is non-negative),
  55. # return n! (n factorial).
  56. def factorial(n):
  57. if (n <= 0):
  58. factorial = 1
  59. return 1
  60. else:
  61. factorial = 1
  62. for i in range (1, n+1):
  63. while (n>1):
  64. factorial = factorial * n
  65. n = n - 1
  66. return factorial
  67.  
  68. #### PROBLEM 4 ####
  69.  
  70. # Definition: We say that m is a factor of n if m divides n without a remainder.
  71. # Given an integer n (which you can assume is positive),
  72. # return the smallest factor of n larger than 1.
  73. # If n is 1, then you should return 1.
  74. def smallestFactor(n):
  75. n = abs(n)
  76. factor = 2
  77. factor = abs(factor)
  78. if n == 0:
  79. return ("none")
  80. if n == 1:
  81. return 1
  82. while n > 1:
  83. if n % factor == 0:
  84. return factor
  85. else:
  86. n % factor != 0
  87. factor = factor + 1
  88. return factor
  89.  
  90. #### PROBLEM 5 ####
  91.  
  92. # We saw in class that .1 + .1 + .1 is not equal to .3, but .1 + .1 + .1 + .1
  93. # is equal to .4. This function takes a positive float with one decimal place
  94. # (e.g., 0.1, 1.2, 7.0) and returns True if that float is exactly equal to the
  95. # sum of 10 times that number of .1's; False otherwise.
  96. def decimalEqual(n):
  97. n = abs(n)
  98. decimal = 0
  99. for i in range (int(n*10)):
  100. decimal = decimal + 0.1
  101. if decimal == n:
  102. return ("true")
  103. else:
  104. return ("false")
  105.  
  106. #### PROBLEM 6 ####
  107.  
  108. # The input is two numbers base and exp. You can assume exp is an int but base
  109. # can be a float. Return base**exp. You are not allowed to use the built-in
  110. # operator ** or the function pow.
  111. def myPower(base, exp):
  112. power = abs(exp)
  113. result = result
  114. if exp == 0:
  115. return 1
  116. for i in range (0,exp):
  117. result = result * base
  118. if (power>0):
  119. return result
  120. else:
  121. return 1/result
  122.  
  123. #### PROBLEM 7 ####
  124.  
  125. # Read the first paragraph of:
  126. # https://en.wikipedia.org/wiki/Happy_number
  127. # After some thought, we see that no matter what number we start with,
  128. # when we keep replacing the number by the sum of the squares of its digits,
  129. # we'll always either arrive at 4 (unhappy) or at 1 (happy).
  130. # Given an integer n, return True if n is happy, and False otherwise.
  131. # Note that all numbers less than 1 are not happy.
  132. # Hint: You may first want to write a function that given an integer,
  133. # returns the sum of the squares of its digits.
  134. def isHappyNumber(n):
  135. return 42
  136.  
  137.  
  138.  
  139. ######################################################################
  140. # ignore_rest: The autograder will ignore all code below here
  141. ######################################################################
  142.  
  143.  
  144. import math
  145.  
  146. def testDigitCount():
  147. print("Testing digitCount()...", end="")
  148. assert(digitCount(0) == 1)
  149. assert(digitCount(1) == 1)
  150. assert(digitCount(9) == 1)
  151. assert(digitCount(10) == 2)
  152. assert(digitCount(1001) == 4)
  153. assert(digitCount(999) == 3)
  154. assert(digitCount(-1) == 1)
  155. assert(digitCount(-123) == 3)
  156. assert(digitCount(-123456789) == 9)
  157. print("Passed. (Add more tests to be more sure!)")
  158.  
  159. def testDigitSum():
  160. print("Testing digitSum()...", end="")
  161. assert(digitSum(0) == 0)
  162. assert(digitSum(1) == 1)
  163. assert(digitSum(2) == 2)
  164. assert(digitSum(11) == 2)
  165. assert(digitSum(111) == 3)
  166. assert(digitSum(123) == 6)
  167. assert(digitSum(123456789) == sum(range(10)))
  168. assert(digitSum(-1) == 1)
  169. assert(digitSum(-2) == 2)
  170. assert(digitSum(-123456789) == sum(range(10)))
  171. print("Passed. (Add more tests to be more sure!)")
  172.  
  173. def testFactorial():
  174. print("Testing factorial()...", end="")
  175. assert(factorial(0) == math.factorial(0))
  176. assert(factorial(1) == math.factorial(1))
  177. assert(factorial(2) == math.factorial(2))
  178. assert(factorial(3) == math.factorial(3))
  179. assert(factorial(4) == math.factorial(4))
  180. assert(factorial(5) == math.factorial(5))
  181. assert(factorial(10) == math.factorial(10))
  182. print("Passed. (Add more tests to be more sure!)")
  183.  
  184. def testSmallestFactor():
  185. print("Testing smallestFactor()...", end="")
  186. assert(smallestFactor(1) == 1)
  187. assert(smallestFactor(2) == 2)
  188. assert(smallestFactor(3) == 3)
  189. assert(smallestFactor(4) == 2)
  190. assert(smallestFactor(5) == 5)
  191. assert(smallestFactor(6) == 2)
  192. assert(smallestFactor(7) == 7)
  193. assert(smallestFactor(8) == 2)
  194. assert(smallestFactor(9) == 3)
  195. assert(smallestFactor(251*991) == 251)
  196. print("Passed. (Add more tests to be more sure!)")
  197.  
  198. def testDecimalEqual():
  199. print("Testing decimalEqual()...", end="")
  200. assert(decimalEqual(.1))
  201. assert(decimalEqual(.2))
  202. assert(decimalEqual(.4))
  203. assert(decimalEqual(.5))
  204. assert(decimalEqual(1.2))
  205. assert(decimalEqual(4.5))
  206. assert(decimalEqual(19.0))
  207. assert(decimalEqual(.2))
  208. assert(not decimalEqual(.3))
  209. assert(not decimalEqual(.8))
  210. assert(not decimalEqual(1.0))
  211. print("Passed. (Add more tests to be more sure!)")
  212.  
  213. def testMyPower():
  214. print("Testing myPower()...", end="")
  215. assert(myPower(0,0) == pow(0,0))
  216. assert(myPower(0,1) == pow(0,1))
  217. assert(myPower(1,0) == pow(1,0))
  218. assert(myPower(1,1) == pow(1,1))
  219. assert(myPower(2,0) == pow(2,0))
  220. assert(myPower(0,2) == pow(0,2))
  221. assert(myPower(2,1) == pow(2,1))
  222. assert(myPower(1,2) == pow(1,2))
  223. assert(myPower(10,5) == pow(10,5))
  224. assert(myPower(3,10) == pow(3,10))
  225. assert(myPower(1,-1) == pow(1,-1))
  226. assert(myPower(2,-1) == pow(2,-1))
  227. assert(myPower(1,-2) == pow(1,-2))
  228. assert(myPower(10,-5) == pow(10,-5))
  229. assert(myPower(3,-10) == pow(3,-10))
  230. assert(almostEqual(myPower(1/10,-5), pow(1/10,-5)))
  231. assert(almostEqual(myPower(1/3,-10), pow(1/3,-10)))
  232. assert(almostEqual(myPower(-1/10,-5), pow(-1/10,-5)))
  233. assert(almostEqual(myPower(-1/3,-10), pow(-1/3,-10)))
  234. print("Passed. (Add more tests to be more sure!)")
  235.  
  236. def testIsHappyNumber():
  237. print("Testing isHappyNumber()...", end="")
  238. assert(isHappyNumber(-7) == False)
  239. assert(isHappyNumber(1) == True)
  240. assert(isHappyNumber(2) == False)
  241. assert(isHappyNumber(97) == True)
  242. assert(isHappyNumber(98) == False)
  243. assert(isHappyNumber(404) == True)
  244. assert(isHappyNumber(405) == False)
  245. print("Passed. (Add more tests to be more sure!)")
  246.  
  247.  
  248.  
  249. def testAll():
  250. testDigitCount()
  251. testDigitSum()
  252. testFactorial()
  253. testSmallestFactor()
  254. testDecimalEqual()
  255. testMyPower()
  256. testIsHappyNumber()
  257.  
  258. testAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement