Write a function named place_random_bricks(m, n, colours)
that randomly places bricks row-wise (completes the current row placement before moving to the next row) on the baseplate.
It must have three parameters:
m
- the number of rows on the base-plate,n
- the number of columns on the base-plate, andcolours
- a random string constructed over “G, R, B, Y, C” (G-Green, R-Red, Blue-B, Y-Yellow, C-Cyan).
This function should return a string of length m * n
, where a character at any position i
represents the colour of a brick placed on the baseplate
(0 ≤ i < (m x n), i ∈ “colours′′);
a value “G” (Green colour) represents no brick was placed.
All colours have an equal probability of being selected.
import random
import colorama
import string
def place_random_bricks(m, n, colours):
v_string=['G', 'R', 'B', 'Y', 'C']
for i in range(m * n):
colours = string.ascii_uppercase
a = print(''.join(random.choices(colours)))
I am expecting a random number of RYBCG.
CodePudding user response:
I would recommend using numpy to generate random 2d arrays since. and the using a lookup string to convert those numbers to the list of random colours you want it to be converted to.
import numpy as np
def place_random_bricks(m,n, colors):
ar = np.random.randint(0,len(colors),(m,n))
return '\n'.join([''.join([colors[j] for j in i]) for i in ar])
print(place_random_bricks(4,5,['G','R','B','Y','C']))
CodePudding user response:
It seems odd that you wouldn't have to return an m*n matrix, but it clearly says it wants one string.
import random
def place_random_bricks(m, n, colours):
row = [random.choice(colours) for _ in range(m*n)]
return ''.join(row)
print(place_random_bricks(5,5,'GRBYC'))
Output:
GGYGYBBCBGYCCGYGCRRRCBGCB