2.We all know that the additive primaries are red, green, and blue. Now, write a Python program that will take a color sequence as a
string from the user where R represents Red, G represents Green and B represents Blue. The program should print the choice of
colors that is actually a tuple containing the sub-tuples as (color_name, color_frequency) only if the color_frequency for that color is
at least one in the given color sequence. [You are not allowed to use count() function here.]
===========================================================
Sample Input1:
"RGBRRGBBR"
Sample Output1:
(('Red', 4), ('Green', 2), ('Blue', 3))
Explanation2:
In the given color sequence, “R”, “B”, and “G” have color_frequency at least one (minimum1). So, “Red”, “Green” and “Blue” are
present in the output.
def color_sequence(line):
red = 0
green = 0
blue = 0
for ch in line:
if ch == 'R':
red += 1
elif ch == 'G':
green += 1
elif ch == 'B':
blue += 1
return ('Red', red), ('Green', green), ('Blue', blue)
def main():
line = input()
print(color_sequence(line))
if __name__ == '__main__':
main()
Comments
Leave a comment