S
S
shmostertoster2016-12-08 00:39:30
Python
shmostertoster, 2016-12-08 00:39:30

How to distribute numbers randomly into a matrix?

Hello!
There are 45 tiles (4 in a row) with a geometric pattern (3 types, but how can you flip = 6)

# Вид 1
a = ("A") #7 (кол-во плиток)
b = ("B") #8
# Вид 2
c = ("C") #7
d = ("D") #8
# Вид 3
e = ("E") #7
f = ("F") #8

It is necessary to distribute the letters in a random non-repeating order into a matrix of 4 in a row (I know that it will not work evenly).
For example:
first row - A, C, E, B
second row - D, F, A, C
third .... So that "Views" are not one under the other or side by side (left-right)
The random number generator made
import random

a = ("A")
b = ("B")
c = ("C")
d = ("D")
e = ("E")
f = ("F")
print(random.sample((a, b, c, d, e, f),6))

but when I make a loop for 14 passes, some letters appear more times than necessary (for example, A appears 10 times, and its maximum is 7)
We need some kind of counter that would subtract values ​​after each pass, but I can’t figure out how crank...
Push to the right path.
Here is a practical application in repair)
Thank you)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-12-08
@shmostertoster

Well, the head-on solution is to use Counter.

from collections import Counter

cntr = Counter({"A": 7, "B": 8, "C": 7, "D": 8, "E": 7, "F": 8})

You now have a cntr counter.
You can check if there are any items left for a particular key:
x = "A"
if cntr[x]:
    # Условие выполнится, только если счётчик для "А" больше нуля

And when you have used a certain letter once, you can subtract one point from its counter:
x = "A"
cntr.subtract( {x: 1} )    # Счётчик для "A" уменьшится на единицу

You can easily find many more examples of working with Counter in Google. This is a very useful class. Once you get the hang of it, you'll want to use it quite often.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question