Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 #This one is a challenge. There\'s a lot going on: splitting Console output wi

ID: 3707934 • Letter: 1

Question

1 #This one is a challenge. There's a lot going on: splitting Console output will be displayed here 2#up strings, removing unnecessary characters, converting to 3 #integers, and running a big conditional. Our solution to 4 #this is 34 lines -- you can do it! 5# 6#in web development, it is common to represent a color like 7 #this: 9# rgb(red val, green-val, blue-val) 10 # 11#where red val, green val and blue val would be substituted 12 #with values from 0-255 telling the computer how much to 13#light up that portion of the pixel. For example: red. 15#-rgb 255, 0, 0) would make a color 16# -rgb(255, 255, 0) would make yellow, because it is equal 17 # parts red and green 18 # -rgb(0, 0, 0) would make black, the absence of all color 19 # -rgb(255, 255, 255) would make white, the presence of all 29# colors equally 21 # 22 #Don't let the function-like syntax here confuse you: here, 23 #these are just strings. The string "rgb(0, 255, 0)" 24#represents the color green. 25 # 26 # rite a function called "find color" that accepts a single 27 #argument expected to be a stringas just.described. Your 28 #function should return a simplified version of the color 29#that is represented according to the following rules: 39# 31 # If there is more red than any other color, return "red" 32 # If there is more green than any other color, return "green". 33 # If there is more blue than any other color, return "blue" 34 # If there are equal parts red and green, return "yellow". 35# If there are equal parts red and blue, return "purple" 36# If there are equal parts green and blue, return "teal" 37 # If there are equal parts red, green, and blue, return "gray". 38# (even though this might be white or black) 39 40 41 # rite your function here! 42 43 45#Below are some lines of code that will 46#You can change the value of the variable(s) to test your test your function 48 #function with different inputs. | # 49#If your function works correctly, this will originally 50 #print: red, purple, gray, each on their own line 51 print(find_color( "rgb (125, 50, 75)") 52 print (find_color("rgb(125, 17, 125) 53 print(find_color("rgb(217, 217, 217)")) 54

Explanation / Answer

def find_color(color): color = str(color).replace('rgb(', '').replace(')', '') r, g, b = int(color.split(', ')[0]), int(color.split(', ')[1]), int(color.split(', ')[2]) if r == g == b: return "gray" elif r > g and r > b: return "red" elif b > g and b > r: return "blue" elif g > r and g > b: return "green" elif r == g: return "yellow" elif g == b: return "teal" elif r == b: return "purple"