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)
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)
Step by step
Solved in 4 steps with 1 images