Answer to Question #275892 in Python for Happpy

Question #275892

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.


1
Expert's answer
2021-12-06T16:43:49-0500
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS