tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
fix: small fixes for sep30 assignments
thecoded.prof
5 months ago
a2b72f8e
fc07492c
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+212
-178
12 changed files
expand all
collapse all
unified
split
python
assignments
sep30
level1
numberOfBricks.py
pizzas.py
pythagoreanTriple.py
triangleArea.py
level2
dotsOverlap.py
getGreen.py
isGray.py
nthFibonacciNumber.py
level3
numberOfSteps.py
rectanglesOverlap.py
triangeAreaByCoordinates.py
level4
blendColors.py
+13
-10
python/assignments/sep30/level1/numberOfBricks.py
···
1
1
from math import ceil
2
2
-
def numberOfBricks(steps):
3
3
-
return ceil((steps*(steps+1))/2)
2
2
+
3
3
+
4
4
+
def numberOfBricks(steps: int) -> int:
5
5
+
return ceil((steps * (steps + 1)) / 2)
4
6
5
5
-
print('Testing numberOfBricks()...', end='')
6
6
-
assert(numberOfBricks(0) == 0)
7
7
-
assert(numberOfBricks(1) == 1)
8
8
-
assert(numberOfBricks(2) == 3)
9
9
-
assert(numberOfBricks(3) == 6)
10
10
-
assert(numberOfBricks(4) == 10)
11
11
-
assert(numberOfBricks(10) == 55)
12
12
-
print('Passed!')
7
7
+
8
8
+
print("Testing numberOfBricks()...", end="")
9
9
+
assert numberOfBricks(0) == 0
10
10
+
assert numberOfBricks(1) == 1
11
11
+
assert numberOfBricks(2) == 3
12
12
+
assert numberOfBricks(3) == 6
13
13
+
assert numberOfBricks(4) == 10
14
14
+
assert numberOfBricks(10) == 55
15
15
+
print("Passed!")
+26
-24
python/assignments/sep30/level1/pizzas.py
···
1
1
-
def howManyPizzas(students, slicesPerStudent):
1
1
+
def howManyPizzas(students: int, slicesPerStudent: int) -> int:
2
2
neededSlices = students * slicesPerStudent
3
3
return (neededSlices + 7) // 8
4
4
5
5
-
def leftoverPizzaSlices(students, slicesPerStudent):
5
5
+
6
6
+
def leftoverPizzaSlices(students: int, slicesPerStudent: int) -> int:
6
7
neededSlices = students * slicesPerStudent
7
7
-
nearest8th = howManyPizzas(students, slicesPerStudent) * 8
8
8
-
return nearest8th - neededSlices
8
8
+
totalSlices = howManyPizzas(students, slicesPerStudent) * 8
9
9
+
return totalSlices - neededSlices
9
10
10
10
-
print('Testing howManyPizzas()...', end='')
11
11
-
assert(howManyPizzas(8, 1) == 1)
12
12
-
assert(howManyPizzas(9, 1) == 2)
13
13
-
assert(howManyPizzas(5, 4) == 3)
14
14
-
assert(howManyPizzas(10, 2) == 3)
15
15
-
assert(howManyPizzas(0, 0) == 0)
16
16
-
assert(howManyPizzas(0, 3) == 0)
17
17
-
assert(howManyPizzas(10, 0) == 0)
18
18
-
assert(howManyPizzas(3, 4) == 2)
19
19
-
print('Passed!')
20
11
21
21
-
print('Testing leftoverPizzaSlices()...', end='')
22
22
-
assert(leftoverPizzaSlices(8, 1) == 0)
23
23
-
assert(leftoverPizzaSlices(9, 1) == 7)
24
24
-
assert(leftoverPizzaSlices(5, 4) == 4)
25
25
-
assert(leftoverPizzaSlices(10, 2) == 4)
26
26
-
assert(leftoverPizzaSlices(0, 0) == 0)
27
27
-
assert(leftoverPizzaSlices(0, 3) == 0)
28
28
-
assert(leftoverPizzaSlices(10, 0) == 0)
29
29
-
assert(leftoverPizzaSlices(3, 4) == 4)
30
30
-
print('Passed!')
12
12
+
print("Testing howManyPizzas()...", end="")
13
13
+
assert howManyPizzas(8, 1) == 1
14
14
+
assert howManyPizzas(9, 1) == 2
15
15
+
assert howManyPizzas(5, 4) == 3
16
16
+
assert howManyPizzas(10, 2) == 3
17
17
+
assert howManyPizzas(0, 0) == 0
18
18
+
assert howManyPizzas(0, 3) == 0
19
19
+
assert howManyPizzas(10, 0) == 0
20
20
+
assert howManyPizzas(3, 4) == 2
21
21
+
print("Passed!")
22
22
+
23
23
+
print("Testing leftoverPizzaSlices()...", end="")
24
24
+
assert leftoverPizzaSlices(8, 1) == 0
25
25
+
assert leftoverPizzaSlices(9, 1) == 7
26
26
+
assert leftoverPizzaSlices(5, 4) == 4
27
27
+
assert leftoverPizzaSlices(10, 2) == 4
28
28
+
assert leftoverPizzaSlices(0, 0) == 0
29
29
+
assert leftoverPizzaSlices(0, 3) == 0
30
30
+
assert leftoverPizzaSlices(10, 0) == 0
31
31
+
assert leftoverPizzaSlices(3, 4) == 4
32
32
+
print("Passed!")
+16
-14
python/assignments/sep30/level1/pythagoreanTriple.py
···
1
1
-
def isPythagoreanTriple(a, b, c):
2
2
-
return (a < b and b < c) and ((a**2 + b**2) == c**2) and not (a < 0 or b < 0 or c < 0)
1
1
+
def isPythagoreanTriple(a: float, b: float, c: float) -> bool:
2
2
+
return (
3
3
+
(a < b and b < c) and ((a**2 + b**2) == c**2) and not (a < 0 or b < 0 or c < 0)
4
4
+
)
3
5
4
6
5
5
-
print('Testing isPythagoreanTriple()...', end='')
6
6
-
assert(isPythagoreanTriple(3, 4, 5) == True) # 3**2 + 4**2 == 5**2
7
7
-
assert(isPythagoreanTriple(5, 4, 3) == False) # wrong order
8
8
-
assert(isPythagoreanTriple(4, 5, 3) == False) # wrong order
7
7
+
print("Testing isPythagoreanTriple()...", end="")
8
8
+
assert isPythagoreanTriple(3, 4, 5) == True # 3**2 + 4**2 == 5**2
9
9
+
assert isPythagoreanTriple(5, 4, 3) == False # wrong order
10
10
+
assert isPythagoreanTriple(4, 5, 3) == False # wrong order
9
11
10
10
-
assert(isPythagoreanTriple(5, 12, 13) == True) # 5**2 + 12**2 == 13**2
11
11
-
assert(isPythagoreanTriple(13, 12, 5) == False) # wrong order
12
12
-
assert(isPythagoreanTriple(12, 13, 5) == False) # wrong order
12
12
+
assert isPythagoreanTriple(5, 12, 13) == True # 5**2 + 12**2 == 13**2
13
13
+
assert isPythagoreanTriple(13, 12, 5) == False # wrong order
14
14
+
assert isPythagoreanTriple(12, 13, 5) == False # wrong order
13
15
14
14
-
assert(isPythagoreanTriple(-3, 4, 5) == False) # no negatives
15
15
-
assert(isPythagoreanTriple( 0, 5, 5) == False) # no 0's
16
16
-
assert(isPythagoreanTriple( 1, 1, 1) == False) # 1**2 + 1**2 != 1**2
17
17
-
assert(isPythagoreanTriple( 3, 4, 6) == False) # 3**2 + 4**2 != 6**2
18
18
-
print('Passed!')
16
16
+
assert isPythagoreanTriple(-3, 4, 5) == False # no negatives
17
17
+
assert isPythagoreanTriple(0, 5, 5) == False # no 0's
18
18
+
assert isPythagoreanTriple(1, 1, 1) == False # 1**2 + 1**2 != 1**2
19
19
+
assert isPythagoreanTriple(3, 4, 6) == False # 3**2 + 4**2 != 6**2
20
20
+
print("Passed!")
+22
-17
python/assignments/sep30/level1/triangleArea.py
···
1
1
from math import sqrt
2
2
-
def almostEqual(x, y):
3
3
-
return (abs(x - y) < 10**-9)
4
2
5
5
-
def triangleArea(a, b, c):
6
6
-
semiperimeter = .5 * (a + b + c)
7
7
-
return sqrt(semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c))
8
3
4
4
+
def almostEqual(x: float, y: float) -> bool:
5
5
+
return abs(x - y) < 10**-9
9
6
10
10
-
print('Testing triangleArea()...', end='')
11
11
-
assert(almostEqual(triangleArea(3, 4, 5), 6))
12
12
-
assert(almostEqual(triangleArea(15, 9, 12), 54))
13
13
-
assert(almostEqual(triangleArea(7, 25, 24), 84))
14
14
-
assert(almostEqual(triangleArea(8, 15, 17), 60))
15
15
-
assert(almostEqual(triangleArea(0, 0, 0), 0))
16
16
-
assert(almostEqual(triangleArea(1, 1, 1), sqrt(3)/4))
17
17
-
assert(almostEqual(triangleArea(5, 5, 5), 25*sqrt(3)/4))
18
18
-
assert(almostEqual(triangleArea(12, 12, 12), 144*sqrt(3)/4))
19
19
-
assert(almostEqual(triangleArea(7, 12, 18), sqrt(11063)/4))
20
20
-
assert(almostEqual(triangleArea(9.1, 11.7, 3), 7*sqrt(3026)/50))
21
21
-
print('Passed!')
7
7
+
8
8
+
def triangleArea(a: float, b: float, c: float) -> float:
9
9
+
semiperimeter = 0.5 * (a + b + c)
10
10
+
return sqrt(
11
11
+
semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c)
12
12
+
)
13
13
+
14
14
+
15
15
+
print("Testing triangleArea()...", end="")
16
16
+
assert almostEqual(triangleArea(3, 4, 5), 6)
17
17
+
assert almostEqual(triangleArea(15, 9, 12), 54)
18
18
+
assert almostEqual(triangleArea(7, 25, 24), 84)
19
19
+
assert almostEqual(triangleArea(8, 15, 17), 60)
20
20
+
assert almostEqual(triangleArea(0, 0, 0), 0)
21
21
+
assert almostEqual(triangleArea(1, 1, 1), sqrt(3) / 4)
22
22
+
assert almostEqual(triangleArea(5, 5, 5), 25 * sqrt(3) / 4)
23
23
+
assert almostEqual(triangleArea(12, 12, 12), 144 * sqrt(3) / 4)
24
24
+
assert almostEqual(triangleArea(7, 12, 18), sqrt(11063) / 4)
25
25
+
assert almostEqual(triangleArea(9.1, 11.7, 3), 7 * sqrt(3026) / 50)
26
26
+
print("Passed!")
+19
-13
python/assignments/sep30/level2/dotsOverlap.py
···
1
1
from math import sqrt
2
2
-
def distance(a, b):
3
3
-
return sqrt((b[0]-a[0])**2 + (b[1]-a[1])**2)
4
2
5
5
-
def dotsOverlap(x1, y1, r1, x2, y2, r2):
3
3
+
4
4
+
def distance(a: tuple[float, float], b: tuple[float, float]) -> float:
5
5
+
return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
6
6
+
7
7
+
8
8
+
def dotsOverlap(
9
9
+
x1: float, y1: float, r1: float, x2: float, y2: float, r2: float
10
10
+
) -> bool:
6
11
d = distance((x1, y1), (x2, y2))
7
7
-
return d <= (r1+r2)
12
12
+
return d <= (r1 + r2)
8
13
9
9
-
print('Testing dotsOverlap()...')
10
10
-
assert(dotsOverlap(0, 0, 2, 3, 0, 2) == True)
11
11
-
assert(dotsOverlap(0, 0, 2, 5, 0, 2) == False)
12
12
-
assert(dotsOverlap(0, 0, 2, 4, 0, 2) == True)
13
13
-
assert(dotsOverlap(-4, 5, 2, -3, 5, 5) == True)
14
14
-
assert(dotsOverlap(3, 3, 3, 3, -3, 2.99) == False)
15
15
-
assert(dotsOverlap(3, 3, 3, 3, -3, 3) == True)
16
16
-
assert(dotsOverlap(5, 3, 0, 5, 3, 0) == True)
17
17
-
print('Passed!')
14
14
+
15
15
+
print("Testing dotsOverlap()...")
16
16
+
assert dotsOverlap(0, 0, 2, 3, 0, 2) == True
17
17
+
assert dotsOverlap(0, 0, 2, 5, 0, 2) == False
18
18
+
assert dotsOverlap(0, 0, 2, 4, 0, 2) == True
19
19
+
assert dotsOverlap(-4, 5, 2, -3, 5, 5) == True
20
20
+
assert dotsOverlap(3, 3, 3, 3, -3, 2.99) == False
21
21
+
assert dotsOverlap(3, 3, 3, 3, -3, 3) == True
22
22
+
assert dotsOverlap(5, 3, 0, 5, 3, 0) == True
23
23
+
print("Passed!")
+14
-15
python/assignments/sep30/level2/getGreen.py
···
1
1
-
def fullRGB(rgb):
2
2
-
rgb = str(rgb)
3
3
-
while len(rgb) < 9:
4
4
-
rgb = '0' + rgb
5
5
-
return rgb
1
1
+
def fullRGB(rgb: int) -> str:
2
2
+
return str(rgb).zfill(9)
3
3
+
4
4
+
5
5
+
def getGreen(rgb: int) -> int:
6
6
+
return int(fullRGB(rgb)[3:6])
6
7
7
7
-
def getGreen(rgb):
8
8
-
return int(fullRGB(rgb[3:6]))
9
8
10
10
-
print('Testing getGreen()...', end='')
11
11
-
assert(getGreen(218112214) == 112)
12
12
-
assert(getGreen(134134134) == 134)
13
13
-
assert(getGreen(111019213) == 19)
14
14
-
assert(getGreen(221000000) == 0)
15
15
-
assert(getGreen(32175) == 32)
16
16
-
assert(getGreen(0) == 0)
17
17
-
print('Passed!')
9
9
+
print("Testing getGreen()...", end="")
10
10
+
assert getGreen(218112214) == 112
11
11
+
assert getGreen(134134134) == 134
12
12
+
assert getGreen(111019213) == 19
13
13
+
assert getGreen(221000000) == 0
14
14
+
assert getGreen(32175) == 32
15
15
+
assert getGreen(0) == 0
16
16
+
print("Passed!")
+16
-17
python/assignments/sep30/level2/isGray.py
···
1
1
-
def fullRGB(rgb):
2
2
-
rgb = str(rgb)
3
3
-
while len(rgb) < 9:
4
4
-
rgb = '0' + rgb
5
5
-
return rgb
1
1
+
def fullRGB(rgb: int) -> str:
2
2
+
return str(rgb).zfill(9)
3
3
+
4
4
+
5
5
+
def isGray(rgb: int) -> bool:
6
6
+
rgbStr = fullRGB(rgb)
7
7
+
return (rgbStr[0:3] == rgbStr[3:6]) and (rgbStr[3:6] == rgbStr[6:9])
6
8
7
7
-
def isGray(rgb):
8
8
-
rgb = fullRGB(rgb)
9
9
-
return (rgb[0:3] == rgb[3:6]) and (rgb[3:6] == rgb[6:9])
10
9
11
11
-
print('Testing isGray()...', end='')
12
12
-
assert(isGray(112112112) == True)
13
13
-
assert(isGray(112112113) == False)
14
14
-
assert(isGray(123195060) == False)
15
15
-
assert(isGray(255255255) == True)
16
16
-
assert(isGray(0) == True)
17
17
-
assert(isGray(19019019) == True)
18
18
-
assert(isGray(175112) == False)
19
19
-
print('Passed!')
10
10
+
print("Testing isGray()...", end="")
11
11
+
assert isGray(112112112) == True
12
12
+
assert isGray(112112113) == False
13
13
+
assert isGray(123195060) == False
14
14
+
assert isGray(255255255) == True
15
15
+
assert isGray(0) == True
16
16
+
assert isGray(19019019) == True
17
17
+
assert isGray(175112) == False
18
18
+
print("Passed!")
+15
-12
python/assignments/sep30/level2/nthFibonacciNumber.py
···
1
1
from math import sqrt
2
2
3
3
-
def almostEqual(x, y):
4
4
-
return (abs(x - y) < 10**-9)
3
3
+
4
4
+
def almostEqual(x: float, y: float) -> bool:
5
5
+
return abs(x - y) < 10**-9
6
6
+
5
7
6
6
-
def nthFibonacciNumber(n):
8
8
+
def nthFibonacciNumber(n: int) -> float:
7
9
n += 1
8
8
-
return ((1+sqrt(5))**n - (1-sqrt(5))**n)/(sqrt(5)*2**n)
10
10
+
return ((1 + sqrt(5)) ** n - (1 - sqrt(5)) ** n) / (sqrt(5) * 2**n)
11
11
+
9
12
10
10
-
print('Testing nthFibonacciNumber()...', end='')
11
11
-
assert(almostEqual(nthFibonacciNumber(0), 1))
12
12
-
assert(almostEqual(nthFibonacciNumber(1), 1))
13
13
-
assert(almostEqual(nthFibonacciNumber(2), 2))
14
14
-
assert(almostEqual(nthFibonacciNumber(3), 3))
15
15
-
assert(almostEqual(nthFibonacciNumber(4), 5))
16
16
-
assert(almostEqual(nthFibonacciNumber(5), 8))
13
13
+
print("Testing nthFibonacciNumber()...", end="")
14
14
+
assert almostEqual(nthFibonacciNumber(0), 1)
15
15
+
assert almostEqual(nthFibonacciNumber(1), 1)
16
16
+
assert almostEqual(nthFibonacciNumber(2), 2)
17
17
+
assert almostEqual(nthFibonacciNumber(3), 3)
18
18
+
assert almostEqual(nthFibonacciNumber(4), 5)
19
19
+
assert almostEqual(nthFibonacciNumber(5), 8)
17
20
18
18
-
print('Passed!')
21
21
+
print("Passed!")
+18
-16
python/assignments/sep30/level3/numberOfSteps.py
···
1
1
-
from math import sqrt,ceil
1
1
+
from math import sqrt, ceil
2
2
3
3
-
def numberOfSteps(bricks: int):
4
4
-
return ceil((-1 + sqrt(1+8*bricks))/2)
5
3
6
6
-
print('Testing numberOfSteps()...', end='')
7
7
-
assert(numberOfSteps(0) == 0)
8
8
-
assert(numberOfSteps(1) == 1)
9
9
-
assert(numberOfSteps(2) == 2)
10
10
-
assert(numberOfSteps(3) == 2)
11
11
-
assert(numberOfSteps(4) == 3)
12
12
-
assert(numberOfSteps(6) == 3)
13
13
-
assert(numberOfSteps(7) == 4)
14
14
-
assert(numberOfSteps(10) == 4)
15
15
-
assert(numberOfSteps(11) == 5)
16
16
-
assert(numberOfSteps(55) == 10)
17
17
-
assert(numberOfSteps(56) == 11)
18
18
-
print('Passed!')
4
4
+
def numberOfSteps(bricks: int) -> int:
5
5
+
return ceil((-1 + sqrt(1 + 8 * bricks)) / 2)
6
6
+
7
7
+
8
8
+
print("Testing numberOfSteps()...", end="")
9
9
+
assert numberOfSteps(0) == 0
10
10
+
assert numberOfSteps(1) == 1
11
11
+
assert numberOfSteps(2) == 2
12
12
+
assert numberOfSteps(3) == 2
13
13
+
assert numberOfSteps(4) == 3
14
14
+
assert numberOfSteps(6) == 3
15
15
+
assert numberOfSteps(7) == 4
16
16
+
assert numberOfSteps(10) == 4
17
17
+
assert numberOfSteps(11) == 5
18
18
+
assert numberOfSteps(55) == 10
19
19
+
assert numberOfSteps(56) == 11
20
20
+
print("Passed!")
+14
-11
python/assignments/sep30/level3/rectanglesOverlap.py
···
1
1
-
def rectanglesOverlap(ax1:int, ay1:int, aw:float, ah:float, bx1:int, by1:int, bw:float, bh:float):
1
1
+
def rectanglesOverlap(
2
2
+
ax1: int, ay1: int, aw: float, ah: float, bx1: int, by1: int, bw: float, bh: float
3
3
+
) -> bool:
2
4
ax2 = ax1 + aw
3
5
ay2 = ay1 + ah
4
6
bx2 = bx1 + bw
5
7
by2 = by1 + bh
6
8
return (ax1 <= bx2 and ax2 >= bx1) and (ay1 <= by2 and ay2 >= by1)
7
9
8
8
-
print('Testing rectanglesOverlap()...', end='')
10
10
+
11
11
+
print("Testing rectanglesOverlap()...", end="")
9
12
# Intersect at right of rectangle 1
10
10
-
assert(rectanglesOverlap(1,1,5,1,6,1,2,2) == True)
13
13
+
assert rectanglesOverlap(1, 1, 5, 1, 6, 1, 2, 2) == True
11
14
# Intersect at top of rectangle 1
12
12
-
assert(rectanglesOverlap(1,4,5,3,1,5,8,3) == True)
15
15
+
assert rectanglesOverlap(1, 4, 5, 3, 1, 5, 8, 3) == True
13
16
# Intersect at left of rectangle 1
14
14
-
assert(rectanglesOverlap(1,5,6,6,-4,7,5,3) == True)
17
17
+
assert rectanglesOverlap(1, 5, 6, 6, -4, 7, 5, 3) == True
15
18
# Intersect at bottom of rectanagle 1
16
16
-
assert(rectanglesOverlap(10,10,3,3,9,7,3,3) == True)
19
19
+
assert rectanglesOverlap(10, 10, 3, 3, 9, 7, 3, 3) == True
17
20
# Partially overlapping rectangles
18
18
-
assert(rectanglesOverlap(1,7,3,6,3,4,2,5) == True)
21
21
+
assert rectanglesOverlap(1, 7, 3, 6, 3, 4, 2, 5) == True
19
22
# Don't intersect
20
20
-
assert(rectanglesOverlap(1,4,3,3,10,10,5,5) == False)
23
23
+
assert rectanglesOverlap(1, 4, 3, 3, 10, 10, 5, 5) == False
21
24
# Don't intersect, but x-coordinates overlap
22
22
-
assert(rectanglesOverlap(1,4,30,3,10,10,5,5) == False)
25
25
+
assert rectanglesOverlap(1, 4, 30, 3, 10, 10, 5, 5) == False
23
26
# Don't intersect, but y-coordinates overlap
24
24
-
assert(rectanglesOverlap(1,4,3,15,10,10,5,5) == False)
25
25
-
print('Passed!')
27
27
+
assert rectanglesOverlap(1, 4, 3, 15, 10, 10, 5, 5) == False
28
28
+
print("Passed!")
+24
-15
python/assignments/sep30/level3/triangeAreaByCoordinates.py
···
1
1
from math import sqrt
2
2
3
3
-
def almostEqual(x: float, y: float):
4
4
-
return (abs(x - y) < 10**-9)
5
3
6
6
-
def distance(a: tuple[int, int], b: tuple[int, int]):
7
7
-
return sqrt((b[0]-a[0])**2 + (b[1]-a[1])**2)
4
4
+
def almostEqual(x: float, y: float) -> bool:
5
5
+
return abs(x - y) < 10**-9
6
6
+
8
7
9
9
-
def triangleArea(a: float, b: float, c: float):
10
10
-
semiperimeter = .5 * (a + b + c)
11
11
-
return sqrt(semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c))
8
8
+
def distance(a: tuple[float, float], b: tuple[float, float]) -> float:
9
9
+
return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
10
10
+
11
11
+
12
12
+
def triangleArea(a: float, b: float, c: float) -> float:
13
13
+
semiperimeter = 0.5 * (a + b + c)
14
14
+
return sqrt(
15
15
+
semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c)
16
16
+
)
17
17
+
12
18
13
13
-
def triangleAreaByCoordinates(x1, y1, x2, y2, x3, y3):
19
19
+
def triangleAreaByCoordinates(
20
20
+
x1: float, y1: float, x2: float, y2: float, x3: float, y3: float
21
21
+
) -> float:
14
22
A = distance((x1, y1), (x2, y2))
15
23
B = distance((x2, y2), (x3, y3))
16
24
C = distance((x3, y3), (x1, y1))
17
25
return triangleArea(A, B, C)
18
26
19
19
-
print('Testing triangleAreaByCoordinates()...', end='')
20
20
-
assert(almostEqual(triangleAreaByCoordinates(10, 11, 14, 11, 12, 13), 4))
21
21
-
assert(isinstance(triangleAreaByCoordinates(10, 11, 14, 11, 12, 13), float))
22
22
-
assert(almostEqual(triangleAreaByCoordinates(2, 4.0, 2, 7, 6.0, 7), 6))
23
23
-
assert(almostEqual(triangleAreaByCoordinates(0, 0, 12.0, 0, 12, 5), 30))
24
24
-
assert(almostEqual(triangleAreaByCoordinates(0, 0, 0, 1, 1, 1), 0.5))
25
25
-
print('Passed!')
27
27
+
28
28
+
print("Testing triangleAreaByCoordinates()...", end="")
29
29
+
assert almostEqual(triangleAreaByCoordinates(10, 11, 14, 11, 12, 13), 4)
30
30
+
assert isinstance(triangleAreaByCoordinates(10, 11, 14, 11, 12, 13), float)
31
31
+
assert almostEqual(triangleAreaByCoordinates(2, 4.0, 2, 7, 6.0, 7), 6)
32
32
+
assert almostEqual(triangleAreaByCoordinates(0, 0, 12.0, 0, 12, 5), 30)
33
33
+
assert almostEqual(triangleAreaByCoordinates(0, 0, 0, 1, 1, 1), 0.5)
34
34
+
print("Passed!")
+15
-14
python/assignments/sep30/level4/blendColors.py
···
1
1
def halfway(a: int, b: int) -> int:
2
2
return 0
3
3
4
4
-
def blendColors(rgb1: int, rgb2: int):
5
5
-
(r1,g1,b1) = [int(str(rgb1).zfill(9)[i:i+3]) for i in range(0,9,3)]
6
6
-
(r2,g2,b2) = [int(str(rgb2).zfill(9)[i:i+3]) for i in range(0,9,3)]
7
7
-
(r3,g3,b3) = [round((r1+r2)/2), round((g1+g2)/2), round((b1+b2)/2)]
8
8
-
(final_r, final_g, final_b) = [str(x).zfill(3) for x in (r3,g3,b3)]
9
9
-
final_color = int(final_r + final_g + final_b)
10
10
-
return final_color
4
4
+
5
5
+
def blendColors(rgb1: int, rgb2: int) -> int:
6
6
+
(r1, g1, b1) = [int(str(rgb1).zfill(9)[i : i + 3]) for i in range(0, 9, 3)]
7
7
+
(r2, g2, b2) = [int(str(rgb2).zfill(9)[i : i + 3]) for i in range(0, 9, 3)]
8
8
+
(r3, g3, b3) = [round((r1 + r2) / 2), round((g1 + g2) / 2), round((b1 + b2) / 2)]
9
9
+
(final_r, final_g, final_b) = [str(x).zfill(3) for x in (r3, g3, b3)]
10
10
+
return int(final_r + final_g + final_b)
11
11
+
11
12
12
12
-
print('Testing blendColors()...', end='')
13
13
-
assert(blendColors(204153050, 104000152) == 154076101)
14
14
-
assert(blendColors(220153102, 151189051) == 186171076)
15
15
-
assert(blendColors(153051153, 51204) == 76051178)
16
16
-
assert(blendColors(123456789, 123456789) == 123456789)
17
17
-
assert(blendColors(0, 0) == 0)
18
18
-
print('Passed!')
13
13
+
print("Testing blendColors()...", end="")
14
14
+
assert blendColors(204153050, 104000152) == 154076101
15
15
+
assert blendColors(220153102, 151189051) == 186171076
16
16
+
assert blendColors(153051153, 51204) == 76051178
17
17
+
assert blendColors(123456789, 123456789) == 123456789
18
18
+
assert blendColors(0, 0) == 0
19
19
+
print("Passed!")