Skip to content

Latest commit

 

History

History
80 lines (55 loc) · 2.02 KB

Coloured_Triangles.md

File metadata and controls

80 lines (55 loc) · 2.02 KB

CodeWars Python Solutions


Coloured Triangles

Task

A coloured triangle is created from a row of colours, each of which is red, green or blue. Successive rows, each containing one fewer colour than the last, are generated by considering the two touching colours in the previous row. If these colours are identical, the same colour is used in the new row. If they are different, the missing colour is used in the new row. This is continued until the final row, with only a single colour, is generated.

The different possibilities are:

Colour here:        G G        B G        R G        B R
Becomes colour:      G          R          B          G

With a bigger example:

R R G B R G B B
 R B R G B R B
  G G B R G G
   G R G B G
    B B R R
     B G R
      R B
       G

You will be given the first row of the triangle as a string and its your job to return the final colour which would appear in the bottom row as a string. In the case of the example above, you would the given RRGBRGBB you should return G.

  • The input string will only contain the uppercase letters R, G, B and there will be at least one letter so you do not have to test for invalid input.
  • If you are only given one colour as the input, return that colour.

Adapted from the 2017 British Informatics Olympiad



Given Code

def triangle(row):
    pass

Solution

def triangle(row):
    if len(row) == 1:
        return row
    else:
        temp = ""
        for i in range(len(row)-1):
            if i == len(row):
                return row
            elif row[i] == row[i+1]:
                temp = temp + row[i]
            elif row[i] in "BG" and row[i+1] in "BG":
                temp = temp + "R"
            elif row[i] in "RG" and row[i+1] in "RG":
                temp = temp + "B"
            elif row[i] in "BR" and row[i+1] in "BR":
                temp = temp + "G"
        return triangle(temp)

See on CodeWars.com