Related
With respect to the following tree:
What is the correct inorder traversal?
U S T X C P Y R B A I G J F N H V T E D L
U S T X C P Y R B A D E I G J F N H V T L
What is the correct postorder traversal?
U T S X P R Y C B D I J G N V T H F E L A
U T S X P R Y C B I J G N V T H F E D L A
I evaluated both pairs. But some are saying 1-1 and 2-1 are correct, while others say 1-2 and 2-2 are correct. I'm confused. Which ones are actually correct?
inorder:
B U S T X C P Y R A D E I G J F N H V T L
postorder (2.2 is correct):
U T S X P R Y C B I J G N V T H F E D L A
Hi i having a problem with sort list like:
sort([p v q v r, p, p v q], R).
expected result is
R = [p, p v q, p v q v r]
Is any built in sort in swi-prolog which allow me that or i have to write it myself.
I am starting with an array of letters:
letters = %w[c s t p b l f g d m
y o u i h t r a e l
o t l a e m r s n i
m a y l p x s e k d]
Passing them, finding all combinations that return an array like this ["cstp", "cstb", "cstl"], this is a shortened example.
def combinations(letters)
combos = letters.combination(4)
combos.collect do |letter_set|
letter_set.join(",").gsub("," ,"")
end
end
I am trying to figure out how to pass the return value of combinations into start_wtih_letter_c. Do I have to pass a block like &block? I tried various things that keep saying wrong number of arguments.
def start_with_letter_c(pass the return value)
combinations.select {|word| word.match(/^ca/) }
end
Here you go, no errors:
letters = %w[c s t p b l f g d m
y o u i h t r a e l
o t l a e m r s n i
m a y l p x s e k d]
def combinations(letters)
combos = letters.combination(4)
combos.collect do |letter_set|
letter_set.join(",").gsub("," ,"")
end
end
def start_with_letter_c(combinations)
combinations.select {|word| word.match(/^ca/) }
end
start_with_letter_c(combinations(letters))
# => ["cael", "caeo", "caet", "cael", "ca ...and so on
I would write something like this:
letters = %w[c s t p b l f g d m
y o u i h t r a e l
o t l a e m r s n i
m a y l p x s e k d]
def combinations(letters)
letters.combination(4).map(&:join)
end
def start_with_letter_c(combinations)
combinations.select { |word| word.start_with?('ca') }
end
start_with_letter_c(combinations(letters))
I'm very new to Prolog, and trying to find an element to search in binary tree, it finds it successfully but the problem is if it doesn't it still gives yes, I want it to say no or to say not found. My code is:
child(1,2,3).
child(2,4,5).
child(3,6,7).
node(1,a).
node(2,b).
node(3,c).
node(4,d).
node(5,f).
node(6,f).
node(7,g).
show(X):-
write('element is found in node: '),write(X),nl.
inc(X,Y,Z):-
Y is X+X,
Z is X+X+1.
find(A):-
traverse3(1,A).
traverse3(X,A):-
check(X,A),
inc(X,Y,Z),
child(X,Y,Z),
traverse3(Y,A),
traverse3(Z,A).
check(X,A):- not(node(X,A)).
check(X,A):-
node(X,A),
show(X).
traverse3(X,A):- not(child(X,Y,Z)).
This is an unusual binary tree. But anyway, since you have already "normalized" it to a database representation, all you have to do to find an element is to ask for it.
In other words, if your program tree.pl consists only of the child/3 and node/2 facts:
child(1,2,3).
child(2,4,5).
child(3,6,7).
node(1,a).
node(2,b).
node(3,c).
node(4,d).
node(5,f).
node(6,f).
node(7,g).
You can simply query for the element you need:
?- [tree].
true.
?- node(N, a).
N = 1.
?- node(N, f).
N = 5 ;
N = 6.
?- node(4, E).
E = d.
?- node(N, E).
N = 1, E = a ;
N = 2, E = b ;
N = 3, E = c ;
N = 4, E = d ;
N = 5, E = f ;
N = 6, E = f ;
N = 7, E = g.
Or is there something I am missing?
Is called Dyck Path.
It is a plane of x and y axis,
where each step will be only (x+1,y+1) or (x+1,y-1)
and will always stay above x-axis
K should means the peak of the Dyck path.
When K is 2 it should means that the peak is 2 and 3.
to form a legal sequence list of matching the parentheses a = '(', and b = ')' and has length 2N
Eg. [a,a,b,b] and [a,b,a,b] are the legal list for N = 2
[a,b,b,a] and [b,a,b,a] do not satisfies for N = 2
need to define the predicate
listFind(L,K,N) satisfies when L has list of order of 2N, for some k >= K
For example
|?- listFind(L,1,3).
L = [a,b,a,b,a,b] ? ;
L = [a,b,a,a,b,b] ? ;
L = [a,a,b,b,a,b] ? ;
L = [a,a,b,a,b,b] ? ;
L = [a,a,a,b,b,b] ? ;
no
|?- listFind(L,2,3).
L = [a,a,b,b,a,b] ? ;
L = [a,a,b,a,b,b] ? ;
L = [a,a,a,b,b,b] ? ;
no
Thanks in advance.
the role of K is unclear to me. Anyway, here is a snippet satisying your test case:
listFind(L, K, N) :-
N2 is N*2,
length(L, N2),
phrase(dyck, L),
% satisfy condition on K
run_length_encoded(L, RLE),
[X-a|_] = RLE, X >= K.
% DCG for Dyck' language over alphabet `a,b`
dyck --> [] ; [a], dyck, [b], dyck.
run_length_encoded([X|S], C) :-
run_length_encoded(S, X, 1, C).
run_length_encoded([Y|S], X, N, E) :-
( X == Y
-> M is N + 1,
run_length_encoded(S, X, M, E)
; E = [N-X|T],
run_length_encoded(S, Y, 1, T)
).
run_length_encoded([], X, C, [C-X]).
As you can see, the interpretation of K is
the sequence must start with at least K consecutives a