Grouping Colorful LEDs - Accenture

Grouping Colorful LEDs - Accenture

Problem:

The program must accept the names of the colors in an LED serial set as the input. The program must find the number of groups of LEDs having the same color at the beginning and the end in the given LED serial set. Also consider each LED in the given LED serial set as a group. Finally, the program must print the number of groups of LEDs as the output.

Input 1:

Red Blue Green Blue

Output 1:

5

Input 2:

Yellow Red Yellow Green Blue Yellow Green

Output 2:

11

Program:

color = input().split() 

count = 0

for i in range(len(color)):

    if color[i] in color[i+1:]: 

        count  +=  color[i+1:].count(color[i])

print(count + len(color))    

Explanation: 

1. Accept a list of color names. 

2. Iterate through the list and find the count of each color from the position next to the current position. Since to cover all the possible sublists that start and end with the same color.

3. To the final result, also add the count of the list. Since each color is considered as the list starting with the same color and ending with the same color. 

4. Print the final count.

 


Comments

Popular posts from this blog

Pronic Integers in N - InfyTQ question

Count Strong Points - Accenture

Letters at position of multiples of a number