he code provided to me when I asked this question is my own code from a preious question I asked on how to make it faster. My code is too slow and times out. I am looking for guidance on how to make it faster or a suggestion for a different approach. The question and my code are below. **************   Question Colour trio def colour_trio(colours): This problem was inspired by the fun little Mathologer video “Secret of Row 10” whose fractal animation once again remind us about how moving from two to three often opens the barn door for the chaos horse to emerge and wildly gallop away. To start, look at three values imaginatively named “red”, “yellow” and “blue”. These names serve as colourful (heh) mnemonics that could just as well have been “foo”, “bar” and “baz”, so no connection to actual physical colours is intended or implied. Next, de0ine a simple mixing rule between these colours with a rule that says that whenever any colour is mixed with itself, the result is that same colour, whereas mixing two different colours always gives the third. For example, mixing blue to blue gives that same blue, whereas mixing blue to yellow gives red. Given the 0irst row of colours as a string of lowercase letters to denote these colours, this function should construct the rows below the 0irst row one at the time according to the following discipline. Each row always contains one fewer element than the previous row above it. The i:th element of each row is calculated by mixing the colours of the previous row in positions i and i + 1. The singleton element of the bottom row is returned as the final answer. For example, starting from the 0irst row 'rybyr' leads to 'brrb', which leads to 'yry', which leads to 'bb', which leads to 'b' as the 0inal answer, Regis. When the Python virtual machine goes 'brrrrr', that in turn leads to 'yrrrr', 'brrr' , 'yrr', 'br' for the 0inal answer 'y' for “Yes, please!” colours Expected result 'y' 'y' 'bb' 'b' 'rybyry' 'r' 'brybbr' 'r' 'rbyryrrbyrbb' 'y' 'yrbbbbryyrybb' 'b' (Today's five-dollar power word to astonish your friends and co-workers is "quasigroup".) check_circle Expert Answer thumb_up   thumb_down Step 1 According to the Information given:- We have to create and define Color trio here we mix the colors and find the singleton element  in a row and rest follows the given instruction. Pyhton Code:- def colour_trio(colours): ##Recursive. #Base case    if len(colours) == 1:     return colours   else:     new_string = ""     for letter in range(0,len(colours)-1):       if colours[letter] == 'b':         if colours [letter+1] == 'b':           new_string+="b"           elif colours [letter+1] == 'r':           new_string+="y"             elif colours [letter+1] == 'y':           new_string+="r"           elif colours[letter] == 'y':         if colours [letter+1] == 'y':           new_string+="y"           elif colours [letter+1] == 'r':           new_string+="b"         elif colours [letter+1] == 'b':           new_string+="r"         elif colours[letter] == 'r':         if colours [letter+1] == 'r':           new_string+="r"           elif colours [letter+1] == 'y':           new_string+="b"          elif colours [letter+1] == 'b':           new_string+="y"         return colour_trio(new_string)   if __name__ == '__main__':     colours="yrbbbbryyrybb"     print(colour_trio(colours))

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter2: Elements Of High-quality Programs
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question
**************
The code provided to me when I asked this question is my own code from a preious question I asked on how to make it faster. My code is too slow and times out. I am looking for guidance on how to make it faster or a suggestion for a different approach. The question and my code are below.
**************
 
Question

Colour trio

def colour_trio(colours):

This problem was inspired by the fun little Mathologer video “Secret of Row 10” whose fractal animation once again remind us about how moving from two to three often opens the barn door for the chaos horse to emerge and wildly gallop away. To start, look at three values imaginatively named “red”, “yellow” and “blue”. These names serve as colourful (heh) mnemonics that could just as well have been “foo”, “bar” and “baz”, so no connection to actual physical colours is intended or implied. Next, de0ine a simple mixing rule between these colours with a rule that says that whenever any colour is mixed with itself, the result is that same colour, whereas mixing two different colours always gives the third. For example, mixing blue to blue gives that same blue, whereas mixing blue to yellow gives red.

Given the 0irst row of colours as a string of lowercase letters to denote these colours, this function should construct the rows below the 0irst row one at the time according to the following discipline. Each row always contains one fewer element than the previous row above it. The i:th element of each row is calculated by mixing the colours of the previous row in positions i and i + 1. The singleton element of the bottom row is returned as the final answer. For example, starting from the 0irst row 'rybyr' leads to 'brrb', which leads to 'yry', which leads to 'bb', which leads to 'b' as the 0inal answer, Regis. When the Python virtual machine goes 'brrrrr', that in turn leads to 'yrrrr', 'brrr' , 'yrr', 'br' for the 0inal answer 'y' for “Yes, please!”

colours

Expected result

'y'

'y'

'bb'

'b'

'rybyry'

'r'

'brybbr'

'r'

'rbyryrrbyrbb'

'y'

'yrbbbbryyrybb'

'b'

(Today's five-dollar power word to astonish your friends and co-workers is "quasigroup".)

check_circle

Expert Answer

thumb_up
 
thumb_down
Step 1

According to the Information given:-

We have to create and define Color trio here we mix the colors and find the singleton element  in a row and rest follows the given instruction.

Pyhton Code:-

def colour_trio(colours):
##Recursive.
#Base case 
  if len(colours) == 1:
    return colours
  else:
    new_string = ""
    for letter in range(0,len(colours)-1):
      if colours[letter] == 'b':
        if colours [letter+1] == 'b':
          new_string+="b"  
        elif colours [letter+1] == 'r':
          new_string+="y"    
        elif colours [letter+1] == 'y':
          new_string+="r"    

      elif colours[letter] == 'y':
        if colours [letter+1] == 'y':
          new_string+="y"  
        elif colours [letter+1] == 'r':
          new_string+="b"
        elif colours [letter+1] == 'b':
          new_string+="r"  

      elif colours[letter] == 'r':
        if colours [letter+1] == 'r':
          new_string+="r"  
        elif colours [letter+1] == 'y':
          new_string+="b" 
        elif colours [letter+1] == 'b':
          new_string+="y"    
    return colour_trio(new_string)  

if __name__ == '__main__':
    colours="yrbbbbryyrybb"
    print(colour_trio(colours))

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage