My code that is attached at the bottom is too slow. I would appreciate any help you can offeto make it faster. My assignment tester times out at 15seconds. 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, define 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 first row of colours as a string of lowercase letters to denote these colours, this function should construct the rows below the first 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 first row 'rybyr' leads to 'brrb', which leads to 'yry', which leads to 'bb', which leads to 'b' as the final answer, Regis. When the Python virtual machine goes 'brrrrr', that in turn leads to 'yrrrr', 'brrr' , 'yrr', 'br' for the final answer 'y' for “Yes, please!” colours Expected result 'y' 'y' 'bb' 'b' 'rybyry' 'r' 'brybbr' 'r' 'rbyryrrbyrbb' 'y' 'yrbbbbryyrybb' 'b'   Is there anyway to make the following code faster? #Colour Trio 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)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

My code that is attached at the bottom is too slow. I would appreciate any help you can offeto make it faster. My assignment tester times out at 15seconds.

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, define 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 first row of colours as a string of lowercase letters to denote these colours, this function should construct the rows below the first 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 first row 'rybyr' leads to 'brrb', which leads to 'yry', which leads to 'bb', which leads to 'b' as the final answer, Regis. When the Python virtual machine goes 'brrrrr', that in turn leads to 'yrrrr', 'brrr' , 'yrr', 'br' for the final answer 'y' for “Yes, please!”

colours

Expected result

'y'

'y'

'bb'

'b'

'rybyry'

'r'

'brybbr'

'r'

'rbyryrrbyrbb'

'y'

'yrbbbbryyrybb'

'b'

 

Is there anyway to make the following code faster?

#Colour Trio
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)  

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY