Advertisement
DocNumFo

Classwork

Feb 15th, 2017
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.82 KB | None | 0 0
  1. ##############################################################################
  2. ##    Unit One Starting Point
  3. ##    DO NOT CHANGE THE NAMES OF THE PROGRAMS!!!!!
  4. ##    Replace None with your code.                                          ##
  5. ##    If you don't submit one of these programs, leave None in place.       ##
  6. ##    almostEqual is provided                                               ##
  7. ##############################################################################
  8.  
  9. import math, sys
  10.  
  11. def almostEqual(x,y):
  12.     return abs(x-y)<10**-10
  13.  
  14. def isPerfectSquare(n):
  15.     if abs(n)!=n:return False
  16.     elif int(math.sqrt(n))==math.sqrt(n):return True
  17.     return False
  18.  
  19. def isFactor(f,n):
  20.     if f==0:return False
  21.     if n==0:return True
  22.     return n%f==0
  23.    
  24.  
  25. def isMultiple(n,m):
  26.     return isFactor(n,m)
  27.        
  28.    
  29. def kthDigit(n,k):
  30.     return abs(abs(math.floor(abs(n)/(10**(k))))-abs((math.floor(abs(n)/(10**(k+1)))*10)))
  31.  
  32. def distance(x1,y1,x2,y2):
  33.     return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
  34.  
  35. def isLegalTriangle(s1,s2,s3):
  36.     if s1+s2>s3 and s2+s3>s1 and s1+s3>s2 and abs(s1)==s1 and abs(s2)==s2 and  abs(s3)==s3:
  37.         return True
  38.     else:
  39.         return False
  40.    
  41. def isRightTriangle(x1, y1, x2, y2, x3, y3):
  42.     ListUn = [abs(distance(x1, y1, x2, y2)), abs(distance(x1, y1, x3, y3)), abs(distance(x3, y3, x2, y2))]
  43.    
  44.     hyp = sorted(ListUn)[2]
  45.     leg1 = sorted(ListUn)[0]
  46.     leg2 = sorted(ListUn)[1]
  47.     if almostEqual((leg1**2+leg2**2),hyp**2):
  48.         return True
  49.     else:
  50.         return False
  51.  
  52. def triangleAreaByCoordinates(x1,y1,x2,y2,x3,y3):
  53.     return abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2)
  54.  
  55. def nthFibonacci(n):
  56.     a,b=1,1
  57.     if n==0 or n==1: return 1
  58.     for i in range(1,n):
  59.         a,b=b,a+b
  60.     print(i,b)
  61.     return b
  62.  
  63.  
  64. def fabricExcess(fabricInches):
  65.     if fabricInches%36==0:
  66.         return 0
  67.     else:
  68.         return 36 - fabricInches % 36
  69.  
  70. def nearestBusStop(street):
  71.     if street%8<=4:
  72.         return street-(street%8)
  73.     else:
  74.         return street+(8-street%8)
  75. def circlesIntersect(x1, y1, r1, x2, y2, r2):
  76.     return None
  77.  
  78. def nearestOdd(n):
  79.     if almostEqual(abs(n)%2,0):
  80.         return n-1
  81.     elif almostEqual((abs(n)+1)%2,0):
  82.         return n
  83.     elif almostEqual(math.trunc((abs(n+1))%2),0):
  84.         return math.trunc(n)
  85.     else:
  86.         return math.copysign(abs(n) +(1-((abs(n)+1)%2))+1,n)
  87.  
  88. def eggCartons(eggs):
  89.     if eggs%12==0:
  90.         return eggs//12
  91.     else:
  92.         return eggs//12 + 1
  93.  
  94. def pascalsTriangleValue(row,col):
  95.     value = (math.factorial(row)) / (math.factorial(col) * math.factorial(row - col))
  96.     return value
  97.  
  98. def isPerfectCube(x):
  99.     return None
  100.  
  101. def isEvenPositiveInt(n):
  102.     if type(n) != int or n == 0:
  103.         return False
  104.     elif almostEqual(0,n%2) and almostEqual(abs(n),n):
  105.         return True
  106.     else:
  107.         return False
  108.  
  109. def celsiusToFahrenheit(degrees):
  110.     return (((degrees*9)/5)+32)
  111.  
  112. def fahrenheitToCelsius(degrees):
  113.     return (((degrees-32)*5)/9)
  114.  
  115. def areCollinear(x1, y1, x2, y2, x3, y3):
  116.     if (x2 - x1)==0 or (x3 - x2)==0:
  117.         return None
  118.     slope1 = (y2 - y1) / (x2 - x1)
  119.     slope2 = (y3 - y2) / (x3 - x2)
  120.     if slope1 == slope2:
  121.         return True
  122.     else:
  123.         return False
  124. def numberOfPoolBalls(rows):
  125.     a,b=1,0
  126.     for n in range(10):
  127.         a,b=b,a+b
  128.  
  129. def numberOfPoolBallRows(balls):
  130.     return None
  131.  
  132. def sphereVolumeFromSurfaceArea(surfaceArea):
  133.     return None
  134.  
  135. def setKthDigit(n, k, d):
  136.     return None
  137.  
  138. def cosineZerosCount(r):
  139.     return None
  140.  
  141. def riverCruiseUpstreamTime(totalTime, totalDistance, riverCurrent):
  142.     return None
  143.  
  144. def rectanglesOverlap(l1, t1, w1, h1, l2, t2, w2, h2):
  145.     return None
  146.  
  147. def lineIntersection(m1, b1, m2, b2):
  148.     return None
  149.  
  150. def triangleArea(s1, s2, s3):
  151.     return None
  152.  
  153. def threeLinesArea(m1, b1, m2, b2, m3, b3):
  154.     return None
  155.  
  156. def isSpecialQuadrilateral(x1,y1,x2,y2,x3,y3,x4,y4):
  157.     return None
  158. #############################################################
  159.  
  160.  
  161. def digitCount(n):
  162.     count = 0
  163.     while n>0:
  164.         count+=1
  165.         n=n//10
  166.     return count
  167.  
  168. def nthPerfectNumber(n):
  169.     return None
  170.  
  171. def mostFrequentDigit(n):
  172.    prev = '0'
  173.    consec = 1
  174.    highcount = 1
  175.    highchar = 0
  176.    for i in str(n):
  177.        if (i == prev):
  178.            consec=consec+1
  179.            if (consec > highcount):
  180.                highcount = consec
  181.                highchar = int(i)
  182.            elif (consec == highcount):
  183.                if  (int(i) > int(highchar)):
  184.                    highchar = int(i)
  185.        else:
  186.            consec=1
  187.        prev = i
  188.    return highchar
  189.  
  190. def longestDigitRun(n):
  191.    prev = 0
  192.    current = 0
  193.    runCount = 1
  194.    consecutiveRun = 1
  195.    longestDigit = 9
  196.    for i in str(n):
  197.        current = int(i)
  198.        if (current == prev):
  199.            runCount = runCount + 1
  200.        else:
  201.            runCount = 1
  202.        if (runCount > consecutiveRun):
  203.            consecutiveRun = runCount
  204.            longestDigit = current
  205.        elif (runCount == consecutiveRun):
  206.            if (current < longestDigit):
  207.                longestDigit = current    
  208.        prev = current
  209.    return longestDigit    
  210.    
  211. def hasConsecutiveDigits(n):
  212.    prev = '0'
  213.    consec = 1
  214.    highcount = 1
  215.    highchar = '0'
  216.    for i in str(n):
  217.        if (i == prev):
  218.            consec=consec+1
  219.            if (consec > highcount):
  220.                highcount = consec
  221.                highchar = i
  222.            elif (consec == highcount):
  223.                if  (int(i) > int(highchar)):
  224.                    highchar = i
  225.        else:
  226.            consec=1
  227.        prev = i
  228.    if (highcount > 1):
  229.        return 1
  230.    else:
  231.        return 0
  232.  
  233. def isPrime(n):
  234.    for i in range(2,int(math.sqrt(n))):    
  235.        if (isFactor(i,n)):
  236.            return False
  237.    return True
  238.  
  239. def isPandigital(n):
  240.    digits = set()
  241.    while n:
  242.        digits.add(n % 10)
  243.        n //= 10
  244.    return digits == set(range(10))
  245. def gcd(n, m):
  246.   # Using iterative Euclidean algorithm. *shrug*
  247.   if n < m: n, m = m, n
  248.   while m: n, m = m, n % m
  249.   return n
  250.  
  251. # def cosineError(x, k):
  252. #     accum = 0
  253. #     for i in range(k + 1):
  254. #         accum += (-x) ** i / math.factorial(i)
  255. #     return abs(math.cos(x) - accum)
  256. # print cosineError(1.2,2)
  257. # def isRotation(x, y):
  258. #     return isFactor(x,y)
  259.    # sx, sy = str(x), str(y)
  260.    # l = max(len(sx), len(sy))
  261.    # for i in range(l):
  262.    #     if sx[i:] + sx[:i] == sy: return True
  263.    # return False
  264.  
  265.  
  266. # def longestIncreasingRun(n):
  267. #     cr, r = None, None
  268. #     for d in map(int, str(n)):
  269. #         if cr is None or d != cr % 10 + 1:
  270. #             cr = d
  271. #         else:
  272. #             cr = cr * 10 + d
  273. #         # Longer numbers are automatically bigger.
  274. #         if r is None or cr > r:
  275. #             r = cr
  276. #     return cr
  277.  
  278. def cosineError(x,k):
  279.     return None
  280.  
  281. def isRotation(x,y):
  282.     return None
  283.  
  284. def longestDigitRun(n):
  285.     return None
  286.  
  287. def longestIncreasingRun(n):
  288.     return None
  289.  
  290. def nthPalindromicPrime(n):
  291.     return None
  292.  
  293. def nthLeftTruncatablePrime(n):
  294.     return None
  295.  
  296. def nthPowerfulNumber(n):
  297.     return None
  298.  
  299. def isStrobogrammaticNumber(n):
  300.     return None
  301.  
  302. def nthHappyNumber(n):
  303.     return None
  304.  
  305. def nthHappyPrime(n):
  306.     return None
  307.  
  308. def nearestKaprekarNumber(n):
  309.     return None
  310.  
  311. def nthCarolPrime(n):
  312.     return None
  313.  
  314. def integral(f, a, b, N):
  315.     return None
  316.  
  317. #############################################################################
  318. ##DO NOT ALTER ANYTHING BELOW THIS LINE
  319. #############################################################################
  320. def testIsPrime():
  321.     global total
  322.     assert isPrime(17)
  323.     assert isPrime(23)
  324.     assert not(isPrime(24))
  325.     print('isPrime...Passed...5pts')
  326.     total+=5
  327.    
  328. def testDigitCount():
  329.     global total
  330.     assert(digitCount(3) == 1)
  331.     assert(digitCount(33) == 2)
  332.     print('digitCount...Passed...5 pts')
  333.     total+=5
  334.  
  335. def testHasConsecutiveDigits():
  336.     global total
  337.     assert(hasConsecutiveDigits(123456789) == False)
  338.     assert(hasConsecutiveDigits(33) == True)
  339.     print('hasConsecutiveDigits...Passed...8 pts')
  340.     total+=8
  341.  
  342. def testIsPandigital():
  343.     global total
  344.     assert isPandigital(0)==False
  345.     assert isPandigital(123456789)==False
  346.     assert isPandigital(1234567890)==True
  347.     assert isPandigital(1234444444444567890)==True
  348.     assert isPandigital(112344444456789)==False
  349.     print('isPandigital...Passed...8 pts')
  350.     total+=8
  351.  
  352.  
  353. def testMostFrequentDigit():
  354.     global total
  355.     assert mostFrequentDigit(0)==0
  356.     assert mostFrequentDigit(1223)==2
  357.     assert mostFrequentDigit(12233)==3
  358.     assert mostFrequentDigit(-12233)==3
  359.     print('mostFrequentDigit...Passed...10 pts')
  360.     total+=10
  361.  
  362. def testNthPerfectNumber():
  363.     global total
  364.     assert(nthPerfectNumber(0) == 6)
  365.     assert(nthPerfectNumber(1) == 28)
  366.     assert(nthPerfectNumber(2) == 496)  
  367.     print('nthPerfectNumber...Passed...10 pts')
  368.     total+=10
  369.  
  370. def testGcd():
  371.     global total
  372.     assert(gcd(3, 3) == 3)
  373.     assert(gcd(3**6, 3**6) == 3**6)
  374.     assert(gcd(3**6, 2**6) == 1)
  375.     assert (gcd(2*3*4*5,3*5)==15)
  376.     print('gcd...Passed...5 pts')
  377.     total+=5
  378.  
  379. def testCosineError():
  380.     global total
  381.     assert(almostEqual(cosineError(0, 0), abs(math.cos(0) - 1)))
  382.     assert(almostEqual(cosineError(1, 0), abs(math.cos(1) - 1)))
  383.     x = 1.2
  384.     guess = 1 - x**2/2 + x**4/(4*3*2)
  385.     error = abs(math.cos(x) - guess)
  386.     assert(almostEqual(cosineError(x, 2), error))
  387.     print('cosineError...Passed 10 pts')
  388.     total+=10
  389.  
  390. def testIsRotation():
  391.     global total
  392.     assert isRotation(2,2)==True
  393.     assert isRotation(23456,23456)==True
  394.     assert isRotation(3412,1234)==True
  395.     assert isRotation(321,3210)==True
  396.     assert isRotation(23,34)==False
  397.     print('isRotation...Passed...10 pts')
  398.     total+=10
  399.  
  400. def testLongestDigitRun():
  401.     global total
  402.     assert longestDigitRun(2)==2
  403.     assert longestDigitRun(23)==2
  404.     assert longestDigitRun(2341)==1
  405.     assert longestDigitRun(223344556677)==2
  406.     assert longestDigitRun(2233445556677)==5
  407.     print('longestDigitRun...Passed...15 pts')
  408.     total+=15
  409.  
  410. def testLongestIncreasingRun():
  411.     global total
  412.     assert longestIncreasingRun(2341)==234
  413.     assert longestIncreasingRun(2345)==2345
  414.     assert longestIncreasingRun(54321)==5
  415.     print('longestIncreasingRun...Passed...10 pts')
  416.     total+=15
  417.  
  418. def testNthPalindromicPrime():
  419.     global total
  420.     assert nthPalindromicPrime(0)==2
  421.     assert nthPalindromicPrime(4)==11
  422.     assert nthPalindromicPrime(10)==313
  423.     print('nthPalindromicPrime...Passed...10 pts')
  424.     total+=10
  425.  
  426. def testNthLeftTruncatablePrime():
  427.     global total
  428.     assert nthLeftTruncatablePrime(0)==2
  429.     assert nthLeftTruncatablePrime(1)==3
  430.     assert nthLeftTruncatablePrime(4)==13
  431.     assert nthLeftTruncatablePrime(10)==53
  432.     print('nthLeftTruncatablePrime...Passed...5 pts')
  433.     total+=8
  434.  
  435. def testNthPowerfulNumber():
  436.     global total
  437.     assert nthPowerfulNumber(0)==1
  438.     assert nthPowerfulNumber(10)==64
  439.     print('nthPowerfulNumber...Passed...8 pts')
  440.     total+=8
  441.  
  442. def testIsStrobogrammaticNumber():
  443.     global total
  444.     assert isStrobogrammaticNumber(1)==True
  445.     assert isStrobogrammaticNumber(609)==True
  446.     assert isStrobogrammaticNumber(6889)==True
  447.     assert isStrobogrammaticNumber(6880)==False
  448.     print('isStrobogrammaticNumber...Passed...')
  449.     total+=10
  450.  
  451. def testNthHappyNumber():
  452.     global total
  453.     assert(nthHappyNumber(0) == 1)
  454.     assert(nthHappyNumber(1) == 7)
  455.     assert(nthHappyNumber(2) == 10)
  456.     assert(nthHappyNumber(3) == 13)
  457.     print('nthHappyNumber...Passed...8pts')
  458.     total+=8
  459.  
  460. def testNthHappyPrime():
  461.     global total
  462.     assert(nthHappyPrime(0) == 7)
  463.     assert(nthHappyPrime(1) == 13)
  464.     assert(nthHappyPrime(2) == 19)
  465.     print('nthHappyPrime...Passed...8 pts')
  466.     total+=8
  467.  
  468. def testNearestKaprekarNumber():
  469.     global total
  470.     assert(nearestKaprekarNumber(1) == 1)
  471.     assert(nearestKaprekarNumber(0) == 1)
  472.     assert(nearestKaprekarNumber(-1) == 1)
  473.     assert(nearestKaprekarNumber(-2) == 1)
  474.     print('nearestKaprekarNumber...Passed...20 pts')
  475.     total+=20
  476.  
  477. def testNthCarolPrime():
  478.     global total
  479.     assert(nthCarolPrime(0) == 7)
  480.     assert(nthCarolPrime(1) == 47)
  481.     assert(nthCarolPrime(2) == 223)
  482.     assert(nthCarolPrime(3) == 3967)
  483.     print('nthCarolPrime...Passed...10 pts')
  484.     total+=10
  485.  
  486. def almostEqual(d1, d2):
  487.     # use a relaxed almostEqual here, just 10**-4
  488.     epsilon = 10**-4
  489.     return (abs(d2 - d1) < epsilon)
  490.  
  491. def f1(x): return 42
  492. def i1(x): return 42*x
  493. def f2(x): return 2*x  + 1
  494. def i2(x): return x**2 + x
  495. def testIntegral():
  496.     global total
  497.     assert(almostEqual(integral(f1, -5, +5, 1), (i1(+5)-i1(-5))))
  498.     assert(almostEqual(integral(f1, -5, +5, 10), (i1(+5)-i1(-5))))
  499.     assert(almostEqual(integral(f2, 1, 2, 1), 4))
  500.     assert(almostEqual(integral(f2, 1, 2, 250), (i2(2)-i2(1))))
  501.     print('integral...Passed...15 pts')
  502.     total+=15
  503. #############################################################################
  504. #############################################################################
  505.  
  506. def testAll():
  507.     if isPrime(1)!=None:
  508.         testIsPrime()
  509.         pass              
  510.     if digitCount(1)!=None:
  511.         testDigitCount()
  512.         pass
  513.     if hasConsecutiveDigits(1)!=None:
  514.         testHasConsecutiveDigits()
  515.         pass
  516.     if mostFrequentDigit(1)!=None:
  517.         testMostFrequentDigit()
  518.         pass
  519.     if isPandigital(1)!=None:
  520.         testIsPandigital()
  521.         pass
  522.     if nthPerfectNumber(0)!=None:
  523.         testNthPerfectNumber()
  524.         pass
  525.     if gcd(1,1)!=None:
  526.         testGcd()
  527.         pass
  528.     if cosineError(1,1)!=None:
  529.         testCosineError()
  530.         pass
  531.     if isRotation(1,1)!=None:
  532.         testIsRotation()
  533.         pass
  534.     if longestDigitRun(1)!=None:
  535.         testLongestDigitRun()
  536.         pass
  537.     if longestIncreasingRun(1)!=None:
  538.         testLongestIncreasingRun()
  539.         pass
  540.     if nthPalindromicPrime(0)!=None:
  541.         testNthPalindromicPrime()
  542.         pass
  543.     if nthLeftTruncatablePrime(0)!=None:
  544.         testNthLeftTruncatablePrime()
  545.         pass
  546.     if nthPowerfulNumber(1)!=None:
  547.         testNthPowerfulNumber()
  548.         pass
  549.     if isStrobogrammaticNumber(1)!=None:
  550.         testIsStrobogrammaticNumber()
  551.         pass
  552.     if nthHappyNumber(1)!=None:
  553.         testNthHappyNumber()
  554.         pass
  555.     if nthHappyPrime(1)!=None:
  556.         testNthHappyPrime()
  557.         pass
  558.     if nearestKaprekarNumber(1)!=None:
  559.         testNearestKaprekarNumber()
  560.         pass
  561.     if nthCarolPrime(1)!=None:
  562.         testNthCarolPrime()
  563.         pass
  564.     if integral(f1,1,1,1)!=None:
  565.         testIntegral()
  566.         pass
  567.            
  568. total=0
  569. testAll()
  570. print()
  571. print('Total Score...',total)
  572.  
  573.  
  574.  
  575.  
  576. #############################################################################
  577. ##DO NOT ALTER ANYTHING BELOW THIS LINE
  578. #############################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement