Language generated by a Context Free Grammar? - formal-languages

What is the language generated by this language? i would say its all words with exactly 2 or 3 b's but i'm not quite sure.

Any number of 'a's before, after and between either 2 or 3 'b's.
It is progressive... any number of S, followed by any number of X, followed by any number of Y, with, optionally, any number of Z. Each of these elements can be any number of the character 'a'. S, X, and Y all move on to the next element when a 'b' is encountered. Y can terminate before a 'b' is seen (thus, a 'b' from S and a 'b' from X are guaranteed, but not one from Y).

Related

Prolog - Display prime numbers

I am new to Prolog programming so still learning. I'm tryingto create a program that accepts a value X and returns a list of all prime numbers between 6 and X. I'll also be later adding an error output if X is entered as 6 or lower.
I have come up with the below code so far to have the value of x entered, y as the first prime number and the counter as 1. Even this is not working, as I try to increment the values of Y. If i can get this working I plan to then add conditions to only show Y if it is a prime, by checking that it divides by itself and 1.
If anyone has any advice it would be greatly appreciated.
prime (X, Y, Counter) :-
X >= Y,
writeln(Y),
Next is Y + Counter,
prime (X, Next, Counter).
prime(X, Y, _) :-
Y > X.
Remove the spaces in prime ( here and here:
Then it works to call ?- prime(10, 3, 1). and have it count.

Is there a way to create a type 3 grammar for this language?

I'm trying to find a grammar of the highest type possible for this language:
L={0^2n 1^(n-1)|n>=1}
I only managed to do this:
S->00X
X->00X1|λ
Which is not type 3. I can't seem to figure out how to get it to type 3 (if that's even possible).
You can't do it, because L is not a regular language.
Assume that L is regular. Let w = 0^(2p)1^(p-1) for some integer p>=1, so that |w| > p. Further, consider the strings x, y, and z such that w = xyz with |xy| <= p, which means both x and y are sequences of 0s (since p < 2p). By the pumping lemma, any string of the form xy^nz is also in L, but that means we can increase the number of 0s without increasing the number of 1s found in z. Thus, our assumption that L is regular must be false.

Prolog program about lists

I am a new programmer in Prolog and i tried to do a program that says: make the predicate penta(X), where X is a list and returns true when in X there are 5 consecutive elements where : the first element is the sum between the first and the second. Also the third element is the difference between the 5th and the 4th for example: X = [ ... 5, 7, 12, 18, 30, ... ].
So I did this:
penta(X) :-
\+length(X,0), //here i verify if the lists contains less than 5 elements so it gives false.
\+length(X,1),
\+length(X,2),
\+length(X,3),
\+length(X,4),
(A, B, C, D, E | X),
C is A + B,
C is D - E,
penta(X).
This actually does not compile so it doesn't work yet. Tell me what's wrong with it if you would like.
thank you very much.
In prolog, we write predicates not functions. A predicate defines a rule which will succeed or fail on sets of instantiated variables. So you want a predicate, penta(X) that succeeds if X is a list that contains 5 consecutive elements meeting your criteria.
Start from the top. Either the 5 consecutive elements that meet the criteria are at the head of your list, or they are later in the list.
% Succeed if the first 5 elements meets the criteria
penta([A, B, C, D, E |_]) :-
... % what goes here for this to succeed?
% Succeeds if the rest of the list succeeds, without the first element
penta([_|T]) :- penta(T).
I think these are the only two rules you need. Anything else you query that doesn't match these will fail by default, which is what you want. You don't need to check for the length for the 0 through 4 length cases. Those cases will fail the above predicates.
You'll notice that, depending upon how you implement the above, it might succeed several times. That is, it may find more than one solution. You need to decide if that's what you want, or if you want it to stop after one solution. I'll leave that as further exercise.

Surrounded region algorithm

The "Surrounded Region" problem states:
"Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region."
I'm confused as to what the task is for this problem. I'm not clear on what dictates when a region is 'surrounded' based on all the examples found online(which happen to all be the same example).
The example given.
input output
X X X X X X X X
X O O X X X X X
X X O X X X X X
X O X X X O X X
Both groups of O's look surrounded by X's to me. Is the rule that all four sides need to be surrounded by X's? and since the bottom O doesn't have a X below it it's not 'captured'?
what happens if this is the input? is nothing captured at all?
X X X X
X O O O
X X O X
X O X X
According to the definition, if 'O' cell is surrounded by 'X' cells, i.e. up/down/left/right cells are 'X'.
The first thought could be for each 'O' cell, add it to an array, check its up/down/left/right and if it is another 'O' cell, continue until, it hits all 'X' cells or it hits boundary. In the former case, cells in the array can all be flipped to'X'; while in the latter case, cells in the array cannot be flipped.
Yes, exactly.
This surrounding and capturing is in fact, like a game (of GO). The edges cannot be captured, that's it. If you put your dots to edges, they will be yours till the end of the game. Also, surrounding means, if O's are surrounded by X's, then X's will form a cycle around O's. And whenever such a cycle completes, all the inside O's will be flipped to X's and vice-versa.
definition of cycle:
A cycle of X's or O's is any connected region where you start from a cell and return to it, without repeating (revisiting) a cell, and each step you can take a chess piece king's move to complete the path.
So, in your input example, the path:
(1,0)->(0,1)->(0,2)->(1,3)->(2,3)->(3,2)->(2,1)->(1,0) forms a cycle.

Prolog inserting multiple elements into list

I want to implement a predicate (vecLine2BitLine) which does the following:
get two lists and a number the first list is the length of blocks (the elements of the blocks are '$') and the second list contains the indexes that these blocks should be placed at meaning:
vecLine2BitLine([1,2,1],[2,5,9],12,BitLine).
BitLine=[' ','$',' ',' ','$','$',' ',' ','$',' ',' ',' '].
explanation:a block of length 1 is at index 2
and a block of length 2 is at index 5 and so on..
insert_at_mul : inserts an element N times (it works perfectly,dupli and my_flatten were implemented previously so i used them)
Ive been trying to activate insert_at_mul N times when N is the length of the list X and Y
in the predicate vecLine2BitLine.
dupli(L1,N,L2) :- dupli(L1,N,L2,N).
dupli([],_,[],_).
dupli([_|Xs],N,Ys,0) :- dupli(Xs,N,Ys,N).
dupli([X|Xs],N,[X|Ys],K) :- K > 0, K1 is K - 1, dupli([X|Xs],N,Ys,K1).
my_flatten(X,[X]) :- \+ is_list(X).
my_flatten([],[]).
my_flatten([X|Xs],Zs) :- my_flatten(X,Y), my_flatten(Xs,Ys), append(Y,Ys,Zs).
insert_at_mul(L,X,K,R,N):-dupli([X],N,XX) , insert_at(L,XX,K,L1) , my_flatten(L1,R).
get_num_spaces(L,N,X):-sum(L,S), X is N-S.
generate_spaces(N,L,X):- insert_at_mul(L,'',1,X,N).
vecLine2BitLineAux([],[],_,_,_).
vecLine2BitLineAux([X|Tail1],[Y|Tail2],N,L,Lnew):- insert_at_mul(L,'*',Y,Lnew,X) ,vecLine2BitLineAux(Tail1,Tail2,N,Lnew,R). // problem here!!!
vecLine2BitLine(X,Y,N,L):- get_num_spaces(X,N,Z) , generate_spaces(Z,[],ZZ) , vecLine2BitLineAux(X,Y,N,ZZ,L).
now the problem is that in the function vecLine2BitLine i cant activate insert_at_mul N times(thats what i tried to do in this code, but failed).
how can I fix vecLine2BitLine for it to work properly as in returning the correct output by actually activating the predicate insert_at_mul N times??
THANKS!
added :
vecLine2BitLine : input parameters : (L1,L2,N,Result)
N: after activating the predicate Result will be N in length.
L1: L1 is a list of numbers each number indicates the length of a block, a block is comprised of a Sequence of '$'.
L2: L2 is a list of numbers the numbers are indices for where the blocks in L1 should be placed.
example:
vecLine2BitLine([3,2],[1,5],9,BitLine).
we can look at the input better as tuples :
vecLine2BitLine[(3,1),(2,5)],9,BitLine).
(3,1) : there is a sequence of '' 3 times at index 1
(2,5) : there is a sequence of '' 2 times at index 5
in our example 9 is the length of BitLine at the end and we have to insert into the
list BitLine 3+2 of the "special chars" '*' but we have 9-(3+2) places left in the list
so we add '' in those places and then we get:
BitLine=['$','$','$','','$','$','','','',''].
This is kind of a nice problem because you can use the arguments as loop counters. The K argument gets you to the proper index. Let's just traverse the list and find a particular index as an example. Notice the base case is that you're at the right element, and the inductive case is prior to the right element.
traverse(1, [X|_], X).
traverse(N, [_|Xs], X) :- N > 0, N0 is N-1, traverse(N0, Xs, X).
We're going to apply that pattern to insert_at/4 to get to the right location in the list. Now let's write a repeat/3 predicate that repeats X N times in a new list L. This time the base case is when we've added all the repetitions we care to, and the inductive case is that we'll add another instance.
repeat(1, X, [X]).
repeat(N, X, [X|Xs]) :- N > 0, N0 is N-1, repeat(N0, X, Xs).
You can see the similarity of structure between these two. Try to combine them into a single predicate. Since this is homework, I'll stop here. You're inches from the goal.

Resources