Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############################################################################
- ## Unit One Starting Point
- ## DO NOT CHANGE THE NAMES OF THE PROGRAMS!!!!!
- ## Replace None with your code. ##
- ## If you don't submit one of these programs, leave None in place. ##
- ## almostEqual is provided ##
- ##############################################################################
- import math, sys
- def almostEqual(x,y):
- return abs(x-y)<10**-10
- def isPerfectSquare(n):
- if abs(n)!=n:return False
- elif int(math.sqrt(n))==math.sqrt(n):return True
- return False
- def isFactor(f,n):
- if f==0:return False
- if n==0:return True
- return n%f==0
- def isMultiple(n,m):
- return isFactor(n,m)
- def kthDigit(n,k):
- return abs(abs(math.floor(abs(n)/(10**(k))))-abs((math.floor(abs(n)/(10**(k+1)))*10)))
- def distance(x1,y1,x2,y2):
- return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
- def isLegalTriangle(s1,s2,s3):
- if s1+s2>s3 and s2+s3>s1 and s1+s3>s2 and abs(s1)==s1 and abs(s2)==s2 and abs(s3)==s3:
- return True
- else:
- return False
- def isRightTriangle(x1, y1, x2, y2, x3, y3):
- ListUn = [abs(distance(x1, y1, x2, y2)), abs(distance(x1, y1, x3, y3)), abs(distance(x3, y3, x2, y2))]
- hyp = sorted(ListUn)[2]
- leg1 = sorted(ListUn)[0]
- leg2 = sorted(ListUn)[1]
- if almostEqual((leg1**2+leg2**2),hyp**2):
- return True
- else:
- return False
- def triangleAreaByCoordinates(x1,y1,x2,y2,x3,y3):
- return abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2)
- def nthFibonacci(n):
- a,b=1,1
- if n==0 or n==1: return 1
- for i in range(1,n):
- a,b=b,a+b
- print(i,b)
- return b
- def fabricExcess(fabricInches):
- if fabricInches%36==0:
- return 0
- else:
- return 36 - fabricInches % 36
- def nearestBusStop(street):
- if street%8<=4:
- return street-(street%8)
- else:
- return street+(8-street%8)
- def circlesIntersect(x1, y1, r1, x2, y2, r2):
- return None
- def nearestOdd(n):
- if almostEqual(abs(n)%2,0):
- return n-1
- elif almostEqual((abs(n)+1)%2,0):
- return n
- elif almostEqual(math.trunc((abs(n+1))%2),0):
- return math.trunc(n)
- else:
- return math.copysign(abs(n) +(1-((abs(n)+1)%2))+1,n)
- def eggCartons(eggs):
- if eggs%12==0:
- return eggs//12
- else:
- return eggs//12 + 1
- def pascalsTriangleValue(row,col):
- value = (math.factorial(row)) / (math.factorial(col) * math.factorial(row - col))
- return value
- def isPerfectCube(x):
- return None
- def isEvenPositiveInt(n):
- if type(n) != int or n == 0:
- return False
- elif almostEqual(0,n%2) and almostEqual(abs(n),n):
- return True
- else:
- return False
- def celsiusToFahrenheit(degrees):
- return (((degrees*9)/5)+32)
- def fahrenheitToCelsius(degrees):
- return (((degrees-32)*5)/9)
- def areCollinear(x1, y1, x2, y2, x3, y3):
- if (x2 - x1)==0 or (x3 - x2)==0:
- return None
- slope1 = (y2 - y1) / (x2 - x1)
- slope2 = (y3 - y2) / (x3 - x2)
- if slope1 == slope2:
- return True
- else:
- return False
- def numberOfPoolBalls(rows):
- a,b=1,0
- for n in range(10):
- a,b=b,a+b
- def numberOfPoolBallRows(balls):
- return None
- def sphereVolumeFromSurfaceArea(surfaceArea):
- return None
- def setKthDigit(n, k, d):
- return None
- def cosineZerosCount(r):
- return None
- def riverCruiseUpstreamTime(totalTime, totalDistance, riverCurrent):
- return None
- def rectanglesOverlap(l1, t1, w1, h1, l2, t2, w2, h2):
- return None
- def lineIntersection(m1, b1, m2, b2):
- return None
- def triangleArea(s1, s2, s3):
- return None
- def threeLinesArea(m1, b1, m2, b2, m3, b3):
- return None
- def isSpecialQuadrilateral(x1,y1,x2,y2,x3,y3,x4,y4):
- return None
- #############################################################
- def digitCount(n):
- count = 0
- while n>0:
- count+=1
- n=n//10
- return count
- def nthPerfectNumber(n):
- return None
- def mostFrequentDigit(n):
- prev = '0'
- consec = 1
- highcount = 1
- highchar = 0
- for i in str(n):
- if (i == prev):
- consec=consec+1
- if (consec > highcount):
- highcount = consec
- highchar = int(i)
- elif (consec == highcount):
- if (int(i) > int(highchar)):
- highchar = int(i)
- else:
- consec=1
- prev = i
- return highchar
- def longestDigitRun(n):
- prev = 0
- current = 0
- runCount = 1
- consecutiveRun = 1
- longestDigit = 9
- for i in str(n):
- current = int(i)
- if (current == prev):
- runCount = runCount + 1
- else:
- runCount = 1
- if (runCount > consecutiveRun):
- consecutiveRun = runCount
- longestDigit = current
- elif (runCount == consecutiveRun):
- if (current < longestDigit):
- longestDigit = current
- prev = current
- return longestDigit
- def hasConsecutiveDigits(n):
- prev = '0'
- consec = 1
- highcount = 1
- highchar = '0'
- for i in str(n):
- if (i == prev):
- consec=consec+1
- if (consec > highcount):
- highcount = consec
- highchar = i
- elif (consec == highcount):
- if (int(i) > int(highchar)):
- highchar = i
- else:
- consec=1
- prev = i
- if (highcount > 1):
- return 1
- else:
- return 0
- def isPrime(n):
- for i in range(2,int(math.sqrt(n))):
- if (isFactor(i,n)):
- return False
- return True
- def isPandigital(n):
- digits = set()
- while n:
- digits.add(n % 10)
- n //= 10
- return digits == set(range(10))
- def gcd(n, m):
- # Using iterative Euclidean algorithm. *shrug*
- if n < m: n, m = m, n
- while m: n, m = m, n % m
- return n
- # def cosineError(x, k):
- # accum = 0
- # for i in range(k + 1):
- # accum += (-x) ** i / math.factorial(i)
- # return abs(math.cos(x) - accum)
- # print cosineError(1.2,2)
- # def isRotation(x, y):
- # return isFactor(x,y)
- # sx, sy = str(x), str(y)
- # l = max(len(sx), len(sy))
- # for i in range(l):
- # if sx[i:] + sx[:i] == sy: return True
- # return False
- # def longestIncreasingRun(n):
- # cr, r = None, None
- # for d in map(int, str(n)):
- # if cr is None or d != cr % 10 + 1:
- # cr = d
- # else:
- # cr = cr * 10 + d
- # # Longer numbers are automatically bigger.
- # if r is None or cr > r:
- # r = cr
- # return cr
- def cosineError(x,k):
- return None
- def isRotation(x,y):
- return None
- def longestDigitRun(n):
- return None
- def longestIncreasingRun(n):
- return None
- def nthPalindromicPrime(n):
- return None
- def nthLeftTruncatablePrime(n):
- return None
- def nthPowerfulNumber(n):
- return None
- def isStrobogrammaticNumber(n):
- return None
- def nthHappyNumber(n):
- return None
- def nthHappyPrime(n):
- return None
- def nearestKaprekarNumber(n):
- return None
- def nthCarolPrime(n):
- return None
- def integral(f, a, b, N):
- return None
- #############################################################################
- ##DO NOT ALTER ANYTHING BELOW THIS LINE
- #############################################################################
- def testIsPrime():
- global total
- assert isPrime(17)
- assert isPrime(23)
- assert not(isPrime(24))
- print('isPrime...Passed...5pts')
- total+=5
- def testDigitCount():
- global total
- assert(digitCount(3) == 1)
- assert(digitCount(33) == 2)
- print('digitCount...Passed...5 pts')
- total+=5
- def testHasConsecutiveDigits():
- global total
- assert(hasConsecutiveDigits(123456789) == False)
- assert(hasConsecutiveDigits(33) == True)
- print('hasConsecutiveDigits...Passed...8 pts')
- total+=8
- def testIsPandigital():
- global total
- assert isPandigital(0)==False
- assert isPandigital(123456789)==False
- assert isPandigital(1234567890)==True
- assert isPandigital(1234444444444567890)==True
- assert isPandigital(112344444456789)==False
- print('isPandigital...Passed...8 pts')
- total+=8
- def testMostFrequentDigit():
- global total
- assert mostFrequentDigit(0)==0
- assert mostFrequentDigit(1223)==2
- assert mostFrequentDigit(12233)==3
- assert mostFrequentDigit(-12233)==3
- print('mostFrequentDigit...Passed...10 pts')
- total+=10
- def testNthPerfectNumber():
- global total
- assert(nthPerfectNumber(0) == 6)
- assert(nthPerfectNumber(1) == 28)
- assert(nthPerfectNumber(2) == 496)
- print('nthPerfectNumber...Passed...10 pts')
- total+=10
- def testGcd():
- global total
- assert(gcd(3, 3) == 3)
- assert(gcd(3**6, 3**6) == 3**6)
- assert(gcd(3**6, 2**6) == 1)
- assert (gcd(2*3*4*5,3*5)==15)
- print('gcd...Passed...5 pts')
- total+=5
- def testCosineError():
- global total
- assert(almostEqual(cosineError(0, 0), abs(math.cos(0) - 1)))
- assert(almostEqual(cosineError(1, 0), abs(math.cos(1) - 1)))
- x = 1.2
- guess = 1 - x**2/2 + x**4/(4*3*2)
- error = abs(math.cos(x) - guess)
- assert(almostEqual(cosineError(x, 2), error))
- print('cosineError...Passed 10 pts')
- total+=10
- def testIsRotation():
- global total
- assert isRotation(2,2)==True
- assert isRotation(23456,23456)==True
- assert isRotation(3412,1234)==True
- assert isRotation(321,3210)==True
- assert isRotation(23,34)==False
- print('isRotation...Passed...10 pts')
- total+=10
- def testLongestDigitRun():
- global total
- assert longestDigitRun(2)==2
- assert longestDigitRun(23)==2
- assert longestDigitRun(2341)==1
- assert longestDigitRun(223344556677)==2
- assert longestDigitRun(2233445556677)==5
- print('longestDigitRun...Passed...15 pts')
- total+=15
- def testLongestIncreasingRun():
- global total
- assert longestIncreasingRun(2341)==234
- assert longestIncreasingRun(2345)==2345
- assert longestIncreasingRun(54321)==5
- print('longestIncreasingRun...Passed...10 pts')
- total+=15
- def testNthPalindromicPrime():
- global total
- assert nthPalindromicPrime(0)==2
- assert nthPalindromicPrime(4)==11
- assert nthPalindromicPrime(10)==313
- print('nthPalindromicPrime...Passed...10 pts')
- total+=10
- def testNthLeftTruncatablePrime():
- global total
- assert nthLeftTruncatablePrime(0)==2
- assert nthLeftTruncatablePrime(1)==3
- assert nthLeftTruncatablePrime(4)==13
- assert nthLeftTruncatablePrime(10)==53
- print('nthLeftTruncatablePrime...Passed...5 pts')
- total+=8
- def testNthPowerfulNumber():
- global total
- assert nthPowerfulNumber(0)==1
- assert nthPowerfulNumber(10)==64
- print('nthPowerfulNumber...Passed...8 pts')
- total+=8
- def testIsStrobogrammaticNumber():
- global total
- assert isStrobogrammaticNumber(1)==True
- assert isStrobogrammaticNumber(609)==True
- assert isStrobogrammaticNumber(6889)==True
- assert isStrobogrammaticNumber(6880)==False
- print('isStrobogrammaticNumber...Passed...')
- total+=10
- def testNthHappyNumber():
- global total
- assert(nthHappyNumber(0) == 1)
- assert(nthHappyNumber(1) == 7)
- assert(nthHappyNumber(2) == 10)
- assert(nthHappyNumber(3) == 13)
- print('nthHappyNumber...Passed...8pts')
- total+=8
- def testNthHappyPrime():
- global total
- assert(nthHappyPrime(0) == 7)
- assert(nthHappyPrime(1) == 13)
- assert(nthHappyPrime(2) == 19)
- print('nthHappyPrime...Passed...8 pts')
- total+=8
- def testNearestKaprekarNumber():
- global total
- assert(nearestKaprekarNumber(1) == 1)
- assert(nearestKaprekarNumber(0) == 1)
- assert(nearestKaprekarNumber(-1) == 1)
- assert(nearestKaprekarNumber(-2) == 1)
- print('nearestKaprekarNumber...Passed...20 pts')
- total+=20
- def testNthCarolPrime():
- global total
- assert(nthCarolPrime(0) == 7)
- assert(nthCarolPrime(1) == 47)
- assert(nthCarolPrime(2) == 223)
- assert(nthCarolPrime(3) == 3967)
- print('nthCarolPrime...Passed...10 pts')
- total+=10
- def almostEqual(d1, d2):
- # use a relaxed almostEqual here, just 10**-4
- epsilon = 10**-4
- return (abs(d2 - d1) < epsilon)
- def f1(x): return 42
- def i1(x): return 42*x
- def f2(x): return 2*x + 1
- def i2(x): return x**2 + x
- def testIntegral():
- global total
- assert(almostEqual(integral(f1, -5, +5, 1), (i1(+5)-i1(-5))))
- assert(almostEqual(integral(f1, -5, +5, 10), (i1(+5)-i1(-5))))
- assert(almostEqual(integral(f2, 1, 2, 1), 4))
- assert(almostEqual(integral(f2, 1, 2, 250), (i2(2)-i2(1))))
- print('integral...Passed...15 pts')
- total+=15
- #############################################################################
- #############################################################################
- def testAll():
- if isPrime(1)!=None:
- testIsPrime()
- pass
- if digitCount(1)!=None:
- testDigitCount()
- pass
- if hasConsecutiveDigits(1)!=None:
- testHasConsecutiveDigits()
- pass
- if mostFrequentDigit(1)!=None:
- testMostFrequentDigit()
- pass
- if isPandigital(1)!=None:
- testIsPandigital()
- pass
- if nthPerfectNumber(0)!=None:
- testNthPerfectNumber()
- pass
- if gcd(1,1)!=None:
- testGcd()
- pass
- if cosineError(1,1)!=None:
- testCosineError()
- pass
- if isRotation(1,1)!=None:
- testIsRotation()
- pass
- if longestDigitRun(1)!=None:
- testLongestDigitRun()
- pass
- if longestIncreasingRun(1)!=None:
- testLongestIncreasingRun()
- pass
- if nthPalindromicPrime(0)!=None:
- testNthPalindromicPrime()
- pass
- if nthLeftTruncatablePrime(0)!=None:
- testNthLeftTruncatablePrime()
- pass
- if nthPowerfulNumber(1)!=None:
- testNthPowerfulNumber()
- pass
- if isStrobogrammaticNumber(1)!=None:
- testIsStrobogrammaticNumber()
- pass
- if nthHappyNumber(1)!=None:
- testNthHappyNumber()
- pass
- if nthHappyPrime(1)!=None:
- testNthHappyPrime()
- pass
- if nearestKaprekarNumber(1)!=None:
- testNearestKaprekarNumber()
- pass
- if nthCarolPrime(1)!=None:
- testNthCarolPrime()
- pass
- if integral(f1,1,1,1)!=None:
- testIntegral()
- pass
- total=0
- testAll()
- print()
- print('Total Score...',total)
- #############################################################################
- ##DO NOT ALTER ANYTHING BELOW THIS LINE
- #############################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement