Hi all
I'm having trouble finding the regular expression for a problem.
The problem is:
Consider a game in which you have a bag with blue and red balls in equal numbers and a two-pan balance. Drawing, randomly from the bag one ball at a time, you must place the ball on the if it's blue, or on the right if it's red. If,at any given time, there are three more balls of one colour than another, the balance unbalances and all the balls fall to the ground, losing the game. The game ends successfully when the bag is empty, if the scales don't tip until the last ball is placed.
Considering the sequence of balls drawn, construct a regular that verifies whether or not the player has won the game:
I tried: (B{0,2}R{0,2}|R{0,2}B{0,2})|(R{0,1}B?R{0,1}|B{0,1}R?B{0,1})|(R{0,2}B?R?)|(B{0,2}R?) but without sucess.
Anyone can help me?

4 Kommentare

Torsten
Torsten am 2 Dez. 2023
Bearbeitet: Torsten am 2 Dez. 2023
Here is a start:
n = 10;
game = ones(2*n,1);
r = randsample(2*n,n)
game(r) = -1;
If "1" stands for "blue" and "-1" stands for "red" in a game with n blue and n red balls, it's now easy to decide whether the game ended successfully or not, isn't it ?
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes am 2 Dez. 2023
Verschoben: Torsten am 2 Dez. 2023
Hi But I need a regular expression
Torsten
Torsten am 2 Dez. 2023
Bearbeitet: Torsten am 2 Dez. 2023
Think about what the results mean if you compute
game(1)
game(1) + game(2)
game(1) + game(2) + game(3)
...
This is almost the solution - so more hints cannot be given.
Alexander
Alexander am 2 Dez. 2023
I can only confirm @Torsten: Why don't you write a program in steps, which is presenting the single results, instead of pushing all in one line which is hardly to follow up.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ishu
Ishu am 2 Jan. 2024

0 Stimmen

Hi Pedro Alexandre Fernandes,
I understand that you want to check whether a given sequence of balls would result in a win or a loss in the described game.
In this case you can keep the count of numbers of blue balls picked and number of red balls picked and then can check the difference between them.
function hasWon = check(seq)
% Initialize counters
blue = 0;
red = 0;
% Iterate through the sequence of balls
for i = 1:length(seq)
if seq(i) == 'B' % If the ball is blue
blue = blue + 1;
elseif seq(i) == 'R' % If the ball is red
red = red + 1;
else
error('Invalid ball color. Use "B" for blue and "R" for red.');
end
% Check the balance
if abs(blue - red) > 2
hasWon = false;
return; % The game is lost
end
end
% game won
hasWon = true;
end
Hope it helps!

Kategorien

Mehr zu Number games finden Sie in Hilfe-Center und File Exchange

Beantwortet:

am 2 Jan. 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by