CMU Coding Bootcamp

fix: formatting, pydocs, comments for sep30

thecoded.prof d2de7452 a2b72f8e

verified
+3 -1
nilla.nix
··· 16 16 src = pins.nixpkgs; 17 17 }; 18 18 }; 19 - shells.default = { 19 + shells.default = config.shells.python; 20 + shells.python = { 20 21 # Declare what systems the shell can be used on. 21 22 systems = [ "x86_64-linux" ]; 22 23 ··· 30 31 mkShell { 31 32 packages = [ 32 33 pkgs.python314 34 + pkgs.black 33 35 ]; 34 36 }; 35 37 };
+1
python/assignments/sep30/level1/numberOfBricks.py
··· 2 2 3 3 4 4 def numberOfBricks(steps: int) -> int: 5 + """Calculate the number of bricks in some steps using arithmetic series: S(n) = n(n+1)/2.""" 5 6 return ceil((steps * (steps + 1)) / 2) 6 7 7 8
+2
python/assignments/sep30/level1/pizzas.py
··· 1 1 def howManyPizzas(students: int, slicesPerStudent: int) -> int: 2 + """Check how many pizzas are needed given that each pizza has 8 slices.""" 2 3 neededSlices = students * slicesPerStudent 3 4 return (neededSlices + 7) // 8 4 5 5 6 6 7 def leftoverPizzaSlices(students: int, slicesPerStudent: int) -> int: 8 + """Check how many slices of pizza will be leftover given that each pizza has 8 slices.""" 7 9 neededSlices = students * slicesPerStudent 8 10 totalSlices = howManyPizzas(students, slicesPerStudent) * 8 9 11 return totalSlices - neededSlices
+6
python/assignments/sep30/level1/pythagoreanTriple.py
··· 1 1 def isPythagoreanTriple(a: float, b: float, c: float) -> bool: 2 + """ 3 + Return true if all the following are True: 4 + - check that numbers are ascending 5 + - check that a^2 + b^2 = c^2 6 + - check that a, b, and c are positive 7 + """ 2 8 return ( 3 9 (a < b and b < c) and ((a**2 + b**2) == c**2) and not (a < 0 or b < 0 or c < 0) 4 10 )
+1
python/assignments/sep30/level1/triangleArea.py
··· 6 6 7 7 8 8 def triangleArea(a: float, b: float, c: float) -> float: 9 + """Use herons formula: `sqrt(sp(sp-a)(sp-b)(sp-c))` to calculate the area of a triangle using it's side lengths.""" 9 10 semiperimeter = 0.5 * (a + b + c) 10 11 return sqrt( 11 12 semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c)
+2
python/assignments/sep30/level2/dotsOverlap.py
··· 2 2 3 3 4 4 def distance(a: tuple[float, float], b: tuple[float, float]) -> float: 5 + """Calculate the distance between two points using the Euclidean distance formula.""" 5 6 return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) 6 7 7 8 8 9 def dotsOverlap( 9 10 x1: float, y1: float, r1: float, x2: float, y2: float, r2: float 10 11 ) -> bool: 12 + """Check if two circles overlap.""" 11 13 d = distance((x1, y1), (x2, y2)) 12 14 return d <= (r1 + r2) 13 15
+2
python/assignments/sep30/level2/getGreen.py
··· 1 1 def fullRGB(rgb: int) -> str: 2 + """Left pad 0's to make the string of length 9.""" 2 3 return str(rgb).zfill(9) 3 4 4 5 5 6 def getGreen(rgb: int) -> int: 7 + """Return the green portion of an RGB value.""" 6 8 return int(fullRGB(rgb)[3:6]) 7 9 8 10
+2
python/assignments/sep30/level2/isGray.py
··· 1 1 def fullRGB(rgb: int) -> str: 2 + """Left pad 0's to make the string of length 9.""" 2 3 return str(rgb).zfill(9) 3 4 4 5 5 6 def isGray(rgb: int) -> bool: 7 + """Return True if the RGB value passed is considered 'gray'.""" 6 8 rgbStr = fullRGB(rgb) 7 9 return (rgbStr[0:3] == rgbStr[3:6]) and (rgbStr[3:6] == rgbStr[6:9]) 8 10
+1
python/assignments/sep30/level2/nthFibonacciNumber.py
··· 6 6 7 7 8 8 def nthFibonacciNumber(n: int) -> float: 9 + """Return the nth Fibonacci number.""" 9 10 n += 1 10 11 return ((1 + sqrt(5)) ** n - (1 - sqrt(5)) ** n) / (sqrt(5) * 2**n) 11 12
+6
python/assignments/sep30/level3/numberOfSteps.py
··· 2 2 3 3 4 4 def numberOfSteps(bricks: int) -> int: 5 + """ 6 + Given a number of bricks return how many steps would be required to use them all given that: 7 + S(0) = 0 8 + S(n) = S(n-1) + n 9 + Formula: `n=(-1+sqrt(1+8*bricks))/2` 10 + """ 5 11 return ceil((-1 + sqrt(1 + 8 * bricks)) / 2) 6 12 7 13
+1
python/assignments/sep30/level3/rectanglesOverlap.py
··· 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: 4 + """Check if two rectangles overlap.""" 4 5 ax2 = ax1 + aw 5 6 ay2 = ay1 + ah 6 7 bx2 = bx1 + bw
+3
python/assignments/sep30/level3/triangeAreaByCoordinates.py
··· 6 6 7 7 8 8 def distance(a: tuple[float, float], b: tuple[float, float]) -> float: 9 + """Calculate the distance between two points using the Euclidean distance formula.""" 9 10 return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) 10 11 11 12 12 13 def triangleArea(a: float, b: float, c: float) -> float: 14 + """Calculate the area of a triangle using Heron's formula.""" 13 15 semiperimeter = 0.5 * (a + b + c) 14 16 return sqrt( 15 17 semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c) ··· 19 21 def triangleAreaByCoordinates( 20 22 x1: float, y1: float, x2: float, y2: float, x3: float, y3: float 21 23 ) -> float: 24 + """Calculate the area of a triangle given its coordinates.""" 22 25 A = distance((x1, y1), (x2, y2)) 23 26 B = distance((x2, y2), (x3, y3)) 24 27 C = distance((x3, y3), (x1, y1))
+2 -4
python/assignments/sep30/level4/blendColors.py
··· 1 - def halfway(a: int, b: int) -> int: 2 - return 0 3 - 4 - 5 1 def blendColors(rgb1: int, rgb2: int) -> int: 2 + """Blend two colors represented as RGB values.""" 6 3 (r1, g1, b1) = [int(str(rgb1).zfill(9)[i : i + 3]) for i in range(0, 9, 3)] 7 4 (r2, g2, b2) = [int(str(rgb2).zfill(9)[i : i + 3]) for i in range(0, 9, 3)] 5 + # Calculate the average of each color component 8 6 (r3, g3, b3) = [round((r1 + r2) / 2), round((g1 + g2) / 2), round((b1 + b2) / 2)] 9 7 (final_r, final_g, final_b) = [str(x).zfill(3) for x in (r3, g3, b3)] 10 8 return int(final_r + final_g + final_b)