Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SAMS 2018, Programming Sections A and B
- #########################################
- # Full name: Britney Beatey
- # Andrew ID: bbeatey
- #########################################
- # DUE DATE: Sunday July 15TH, 5pm.
- # SUBMIT THIS FILE TO AUTOLAB. LATE SUBMISSIONS WILL NOT BE ACCEPTED!
- # For the functions below, erase the "return 42" line and write the
- # appropriate piece of code instead.
- # IMPORTANT NOTE:
- # **You are not allowed to import any modules, use strings, lists, or recursion. **
- # Use the following function to compare floats.
- def almostEqual(d1, d2):
- epsilon = 10**-8
- return (abs(d2 - d1) < epsilon)
- #### PROBLEM 1 ####
- # Given an integer n, return the number of digits of n.
- def digitCount(n):
- n = abs(n)
- count = 0
- if n == 0:
- return 1
- while (n>0):
- n = n // 10
- count = count + 1
- return count
- #### PROBLEM 2 ####
- # Given an integer n, return the sum of its digits.
- def digitSum(n):
- n = abs(n)
- sum = 0
- if n == 0:
- return 0
- while (n>0):
- sum = sum + (n % 10)
- n = n // 10
- return sum
- #### PROBLEM 3 ####
- # Definition: For a positive integer n, n factorial, denoted n!,
- # is the product n*(n-1)*(n-2)*...*1. If n = 0, then define 0! as 1.
- # Given an integer n (which you can assume is non-negative),
- # return n! (n factorial).
- def factorial(n):
- if (n <= 0):
- factorial = 1
- return 1
- else:
- factorial = 1
- for i in range (1, n+1):
- while (n>1):
- factorial = factorial * n
- n = n - 1
- return factorial
- #### PROBLEM 4 ####
- # Definition: We say that m is a factor of n if m divides n without a remainder.
- # Given an integer n (which you can assume is positive),
- # return the smallest factor of n larger than 1.
- # If n is 1, then you should return 1.
- def smallestFactor(n):
- n = abs(n)
- factor = 2
- factor = abs(factor)
- if n == 0:
- return ("none")
- if n == 1:
- return 1
- while n > 1:
- if n % factor == 0:
- return factor
- else:
- n % factor != 0
- factor = factor + 1
- return factor
- #### PROBLEM 5 ####
- # We saw in class that .1 + .1 + .1 is not equal to .3, but .1 + .1 + .1 + .1
- # is equal to .4. This function takes a positive float with one decimal place
- # (e.g., 0.1, 1.2, 7.0) and returns True if that float is exactly equal to the
- # sum of 10 times that number of .1's; False otherwise.
- def decimalEqual(n):
- n = abs(n)
- decimal = 0
- for i in range (int(n*10)):
- decimal = decimal + 0.1
- if decimal == n:
- return ("true")
- else:
- return ("false")
- #### PROBLEM 6 ####
- # The input is two numbers base and exp. You can assume exp is an int but base
- # can be a float. Return base**exp. You are not allowed to use the built-in
- # operator ** or the function pow.
- def myPower(base, exp):
- power = abs(exp)
- result = result
- if exp == 0:
- return 1
- for i in range (0,exp):
- result = result * base
- if (power>0):
- return result
- else:
- return 1/result
- #### PROBLEM 7 ####
- # Read the first paragraph of:
- # https://en.wikipedia.org/wiki/Happy_number
- # After some thought, we see that no matter what number we start with,
- # when we keep replacing the number by the sum of the squares of its digits,
- # we'll always either arrive at 4 (unhappy) or at 1 (happy).
- # Given an integer n, return True if n is happy, and False otherwise.
- # Note that all numbers less than 1 are not happy.
- # Hint: You may first want to write a function that given an integer,
- # returns the sum of the squares of its digits.
- def isHappyNumber(n):
- return 42
- ######################################################################
- # ignore_rest: The autograder will ignore all code below here
- ######################################################################
- import math
- def testDigitCount():
- print("Testing digitCount()...", end="")
- assert(digitCount(0) == 1)
- assert(digitCount(1) == 1)
- assert(digitCount(9) == 1)
- assert(digitCount(10) == 2)
- assert(digitCount(1001) == 4)
- assert(digitCount(999) == 3)
- assert(digitCount(-1) == 1)
- assert(digitCount(-123) == 3)
- assert(digitCount(-123456789) == 9)
- print("Passed. (Add more tests to be more sure!)")
- def testDigitSum():
- print("Testing digitSum()...", end="")
- assert(digitSum(0) == 0)
- assert(digitSum(1) == 1)
- assert(digitSum(2) == 2)
- assert(digitSum(11) == 2)
- assert(digitSum(111) == 3)
- assert(digitSum(123) == 6)
- assert(digitSum(123456789) == sum(range(10)))
- assert(digitSum(-1) == 1)
- assert(digitSum(-2) == 2)
- assert(digitSum(-123456789) == sum(range(10)))
- print("Passed. (Add more tests to be more sure!)")
- def testFactorial():
- print("Testing factorial()...", end="")
- assert(factorial(0) == math.factorial(0))
- assert(factorial(1) == math.factorial(1))
- assert(factorial(2) == math.factorial(2))
- assert(factorial(3) == math.factorial(3))
- assert(factorial(4) == math.factorial(4))
- assert(factorial(5) == math.factorial(5))
- assert(factorial(10) == math.factorial(10))
- print("Passed. (Add more tests to be more sure!)")
- def testSmallestFactor():
- print("Testing smallestFactor()...", end="")
- assert(smallestFactor(1) == 1)
- assert(smallestFactor(2) == 2)
- assert(smallestFactor(3) == 3)
- assert(smallestFactor(4) == 2)
- assert(smallestFactor(5) == 5)
- assert(smallestFactor(6) == 2)
- assert(smallestFactor(7) == 7)
- assert(smallestFactor(8) == 2)
- assert(smallestFactor(9) == 3)
- assert(smallestFactor(251*991) == 251)
- print("Passed. (Add more tests to be more sure!)")
- def testDecimalEqual():
- print("Testing decimalEqual()...", end="")
- assert(decimalEqual(.1))
- assert(decimalEqual(.2))
- assert(decimalEqual(.4))
- assert(decimalEqual(.5))
- assert(decimalEqual(1.2))
- assert(decimalEqual(4.5))
- assert(decimalEqual(19.0))
- assert(decimalEqual(.2))
- assert(not decimalEqual(.3))
- assert(not decimalEqual(.8))
- assert(not decimalEqual(1.0))
- print("Passed. (Add more tests to be more sure!)")
- def testMyPower():
- print("Testing myPower()...", end="")
- assert(myPower(0,0) == pow(0,0))
- assert(myPower(0,1) == pow(0,1))
- assert(myPower(1,0) == pow(1,0))
- assert(myPower(1,1) == pow(1,1))
- assert(myPower(2,0) == pow(2,0))
- assert(myPower(0,2) == pow(0,2))
- assert(myPower(2,1) == pow(2,1))
- assert(myPower(1,2) == pow(1,2))
- assert(myPower(10,5) == pow(10,5))
- assert(myPower(3,10) == pow(3,10))
- assert(myPower(1,-1) == pow(1,-1))
- assert(myPower(2,-1) == pow(2,-1))
- assert(myPower(1,-2) == pow(1,-2))
- assert(myPower(10,-5) == pow(10,-5))
- assert(myPower(3,-10) == pow(3,-10))
- assert(almostEqual(myPower(1/10,-5), pow(1/10,-5)))
- assert(almostEqual(myPower(1/3,-10), pow(1/3,-10)))
- assert(almostEqual(myPower(-1/10,-5), pow(-1/10,-5)))
- assert(almostEqual(myPower(-1/3,-10), pow(-1/3,-10)))
- print("Passed. (Add more tests to be more sure!)")
- def testIsHappyNumber():
- print("Testing isHappyNumber()...", end="")
- assert(isHappyNumber(-7) == False)
- assert(isHappyNumber(1) == True)
- assert(isHappyNumber(2) == False)
- assert(isHappyNumber(97) == True)
- assert(isHappyNumber(98) == False)
- assert(isHappyNumber(404) == True)
- assert(isHappyNumber(405) == False)
- print("Passed. (Add more tests to be more sure!)")
- def testAll():
- testDigitCount()
- testDigitSum()
- testFactorial()
- testSmallestFactor()
- testDecimalEqual()
- testMyPower()
- testIsHappyNumber()
- testAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement