collapsing runs of the same color isn't quite corect.
It fails a test case.
import re def blend(c1, c2): if c1 == c2: return c1 colors = c1 + c2 if colors == "RB" or colors == "BR": return "G" if colors == "RG" or colors == "GR": return "B" if colors == "GB" or colors == "BG": return "R" def preprocess(row): orig = row row,_ = re.subn('RR+', 'R', row) row,_ = re.subn('BB+', 'B', row) row,_ = re.subn('GG+', 'G', row) print(orig, row) return row def triangle(row): if len(row) == 1: return row row = preprocess(row) row = list(row) for j in range(len(row)-1,0,-1): for i in range(j): row[i] = blend(row[i], row[i+1]) return row[0]
- import re
- def blend(c1, c2):
- if c1 == c2: return c1
- colors = c1 + c2
- if colors == "RB" or colors == "BR": return "G"
- if colors == "RG" or colors == "GR": return "B"
- if colors == "GB" or colors == "BG": return "R"
- def preprocess(row):
- orig = row
- row,_ = re.subn('RR+', 'R', row)
- row,_ = re.subn('BB+', 'B', row)
- row,_ = re.subn('GG+', 'G', row)
#print(orig, row)- print(orig, row)
- return row
- def triangle(row):
- if len(row) == 1: return row
- row = preprocess(row)
- row = list(row)
- for j in range(len(row)-1,0,-1):
- for i in range(j):
- row[i] = blend(row[i], row[i+1])
- return row[0]
import random test.describe('Basic tests') test.it('Sample cases') test.assert_equals(triangle('RBRGBRBGGRRRBGBBBGG'), 'G') test.assert_equals(triangle('RGRGRRGBRBBGBGBRGGRRGGRGGGGRG'), 'B') test.assert_equals(triangle('GB'), 'R') test.assert_equals(triangle('RRR'), 'R') test.assert_equals(triangle('RGBG'), 'B') test.assert_equals(triangle('RBRGBRB'), 'G') test.assert_equals(triangle('B'), 'B') test.it('Random tests') def do_rand_tests(): CHARS = 'RGB' def gen_t68wvn6g78w8v8g(): s = ''.join([random.choice(CHARS) for i in range(random.randint(1, 30))]) return s def do_ans_vnw6g7g8vn7v(row): MATCHES = {('B', 'G'): 'R', ('G', 'R'): 'B', ('B', 'R'): 'G'} def triangle(row): if len(row) == 1: return row[0] n_row = [calc_next(row[i], row[i + 1]) for i in range(len(row) - 1)] return triangle(n_row) def calc_next(p1, p2): return p1 if p1 == p2 else MATCHES[tuple(sorted([p1, p2]))] ans = triangle(row) return ans for i in range(random.randint(100, 120)): line = gen_t68wvn6g78w8v8g() tri = triangle(line) ans = do_ans_vnw6g7g8vn7v(line) if (tri != ans): print("failed: ", line) test.assert_equals(tri, ans) do_rand_tests()
def _test(cases):for _in, _out in cases:test.assert_equals(triangle(_in), _out)test.describe('Insane Coloured Triangles')basic_cases = [['B', 'B'],['GB', 'R'],['RRR', 'R'],['RGBG', 'B'],['RBRGBRB', 'G'],['RBRGBRBGGRRRBGBBBGG', 'G']]test.it('Basic Tests')_test(basic_cases)- import random
- test.describe('Basic tests')
- test.it('Sample cases')
- test.assert_equals(triangle('RBRGBRBGGRRRBGBBBGG'), 'G')
- test.assert_equals(triangle('RGRGRRGBRBBGBGBRGGRRGGRGGGGRG'), 'B')
- test.assert_equals(triangle('GB'), 'R')
- test.assert_equals(triangle('RRR'), 'R')
- test.assert_equals(triangle('RGBG'), 'B')
- test.assert_equals(triangle('RBRGBRB'), 'G')
- test.assert_equals(triangle('B'), 'B')
- test.it('Random tests')
- def do_rand_tests():
- CHARS = 'RGB'
- def gen_t68wvn6g78w8v8g():
- s = ''.join([random.choice(CHARS) for i in range(random.randint(1, 30))])
- return s
- def do_ans_vnw6g7g8vn7v(row):
- MATCHES = {('B', 'G'): 'R',
- ('G', 'R'): 'B',
- ('B', 'R'): 'G'}
- def triangle(row):
- if len(row) == 1:
- return row[0]
- n_row = [calc_next(row[i], row[i + 1]) for i in range(len(row) - 1)]
- return triangle(n_row)
- def calc_next(p1, p2):
- return p1 if p1 == p2 else MATCHES[tuple(sorted([p1, p2]))]
- ans = triangle(row)
- return ans
- for i in range(random.randint(100, 120)):
- line = gen_t68wvn6g78w8v8g()
- tri = triangle(line)
- ans = do_ans_vnw6g7g8vn7v(line)
- if (tri != ans):
- print("failed: ", line)
- test.assert_equals(tri, ans)
- do_rand_tests()
import re def blend(c1, c2): if c1 == c2: return c1 colors = c1 + c2 if colors == "RB" or colors == "BR": return "G" if colors == "RG" or colors == "GR": return "B" if colors == "GB" or colors == "BG": return "R" def preprocess(row): orig = row row,_ = re.subn('RR+', 'R', row) row,_ = re.subn('BB+', 'B', row) row,_ = re.subn('GG+', 'G', row) #print(orig, row) return row def triangle(row): if len(row) == 1: return row row = preprocess(row) row = list(row) for j in range(len(row)-1,0,-1): for i in range(j): row[i] = blend(row[i], row[i+1]) return row[0]
- import re
- def blend(c1, c2):
- if c1 == c2: return c1
- colors = c1 + c2
- if colors == "RB" or colors == "BR": return "G"
- if colors == "RG" or colors == "GR": return "B"
- if colors == "GB" or colors == "BG": return "R"
- def preprocess(row):
- orig = row
- row,_ = re.subn('RR+', 'R', row)
- row,_ = re.subn('BB+', 'B', row)
- row,_ = re.subn('GG+', 'G', row)
- #print(orig, row)
- return row
- def triangle(row):
- if len(row) == 1: return row
- row = preprocess(row)
- row = list(row)
- for j in range(len(row)-1,0,-1):
- for i in range(j):
- row[i] = blend(row[i], row[i+1])
- return row[0]
def blend(c1, c2):
if c1 == c2: return c1
colors = c1 + c2
if colors == "RB" or colors == "BR": return "G"
if colors == "RG" or colors == "GR": return "B"
if colors == "GB" or colors == "BG": return "R"
def triangle(row):
if len(row) == 1: return row
row = list(row)
for j in range(len(row)-1,0,-1):
for i in range(j):
row[i] = blend(row[i], row[i+1])
return row[0]
def _test(cases):
for _in, _out in cases:
test.assert_equals(triangle(_in), _out)
test.describe('Insane Coloured Triangles')
basic_cases = [
['B', 'B'],
['GB', 'R'],
['RRR', 'R'],
['RGBG', 'B'],
['RBRGBRB', 'G'],
['RBRGBRBGGRRRBGBBBGG', 'G']
]
test.it('Basic Tests')
_test(basic_cases)