prolog sublists between # - list

I have this function that extracts sublists between # like this:
?- f([1,2,3,#,4,5,6,7,#,8,9,10],L).
L=[1,2,3];
L=[4,5,6,7];
L=[8,9,10];
false.
However when I call it with variables the variables unify with # and so I get a bunch of sublists like this
Fila = [#, _, _, _, _, _, #, _, #, _, _, _],
| espaco_fila(Fila, Espaco).
Fila = [#, _2456, _2462, _2468, #, _2480, #, _2492, #|...],
Espaco = [_2456, _2462, _2468],
dif(_2456, #),
dif(_2462, #),
dif(_2468, #) ;
Fila = [#, _2604, _2610, _2616, _2622, #, #, _2640, #|...],
Espaco = [_2604, _2610, _2616, _2622],
dif(_2604, #),
dif(_2610, #),
dif(_2616, #),
dif(_2622, #) ;
Fila = [#, _2758, _2764, _2770, _2776, _2782, #, _2794, #|...],
Espaco = [_2758, _2764, _2770, _2776, _2782],
dif(_2758, #),
dif(_2764, #),
dif(_2770, #),
dif(_2776, #),
dif(_2782, #) ;
Fila = [#, #, _2456, _2462, _2468, #, #, _2486, #|...],
Espaco = [_2456, _2462, _2468],
dif(_2456, #),
dif(_2462, #),
dif(_2468, #) ;
Fila = [#, #, _2610, _2616, _2622, _2628, #, _2640, #|...],
Espaco = [_2610, _2616, _2622, _2628],
dif(_2610, #),
dif(_2616, #),
dif(_2622, #),
dif(_2628, #) ;
Fila = [#, _2456, #, _2468, _2474, _2480, #, _2492, #|...],
Espaco = [_2468, _2474, _2480],
dif(_2468, #),
dif(_2474, #),
dif(_2480, #) ;
Fila = [#, _2456, _2462, _2468, _2474, _2480, #, _2492, #|...],
Espaco = [_2504, _2510, _2516],
dif(_2504, #),
dif(_2510, #),
dif(_2516, #) ;
false.
I'm taking the sublists using these
append([Esp, [#|_]], Fila)
append([_,[#],Esp, [#|_]], Fila)
append([_,[#],Esp], Fila)
Is there any way to make them ignore the variables and look only at the constant #?

Related

If statement with multiple "and" and "or" conditions

I am trying to check multiple conditions. If they are true I would like to do something. The problem is, that I have quite a challenging combination of "and" and "or". Parentheses doesn't seem to work. How should I restructure this to make it work?
rocnt = Worksheets(1).Range("C" & Rows.Count).End(xlUp).Row
For i = 1 To rocnt
If _
Range("K" & i).Value = "Zeesan" _
And Range("G" & i).Value = "UNDEFINED" _
And Range("H" & i).Value = "UNDEFINED" _
And Range("I" & i).Value = "UNDEFINED" _
And (Range("F" & i).Value = "NO DATA" Or Range("F" & i).Value = "YES") _
And (Range("L" & i).Value = "Novogene" Or Range("L" & i).Value = "180" Or Range("L" & i).Value = "130" Or Range("L" & i).Value = "120" Or Range("L" & i).Value = "38") _
Then
'do something'
end If

Why this syntax error? No given reason from the shell

I've just finished writing this piece of pattern matching (to be attached below). No matter how I tried fixing it up, there is still "Error: Syntax error" when I tried to compile it.
The piece of code is here:
let rec stmt (s:Ast.stmt) : X86.inst list =
let get_label e =
let inst = stmt e in
let pattern = (List.nth (List.rev inst) 0) in
let label = match pattern with
|Memory (Store (Addr (Label l), Reg r)) -> l
|_ -> "Error"
in
match s with
|Exp e -> (match e with
|Int i -> [movei RAX i; store new_temp() RAX]
|Var v -> [load RBX v; store new_temp() RBX]
|Binop (e1, op, e2) -> let (label1, label2) = (get_label e1, get_label e2)
in let operation =
match op with
|Plus -> [Arith (Add (RAX, Reg RBX))]
|Minus -> [Arith (Sub (RAX, Reg RBX))]
|Times -> [Arith (Mul RBX)]
|Div -> [Arith (Div RBX)]
|Eq -> [Arith (Cmp (RAX, RBX))]
|Neq -> [Arith (Cmp (RAX, Reg RBX)); set NE RAX]
|Lt -> [Arith (Cmp (RAX, Reg RBX)); set L RAX]
|Lte -> [Arith (Cmp (RAX, Reg RBX)); set LE RAX]
|Gt -> [Arith (Cmp (RAX, Reg RBX)); set G RAX]
|Gte -> [Arith (Cmp (RAX, Reg RBX)); set GE RAX]
in [load RAX label1; load RBX label2] # operation # [store (new_temp()) RAX]
|Not e -> let label = get_label e in
[load RBX label; Arith (Neg RBX); store (new_temp()) RAX]
|And (e1, e2) -> let (label1, label2) = (get_label e1, get_label e2) in
[load RAX label1; load RBX label2; Bitop (And (RAX, Reg RBX)); store (new_temp()) RAX]
|Or (e1, e2) -> let (label1, label2) = (get_label e1, get_label e2) in
[load RAX label1; load RBX label2; Bitop (Or (RAX, Reg RBX)); store (new_temp()) RAX]
|Assign (e1, e2) -> let label = get_label e2 in
[movei RAX label; store e1 RAX; store (new_temp()) RAX]
|_ -> [])
|Block e -> []
|If (l, m, r) -> []
|While (l, r) -> []
|For (l, m1, m2, r) -> []
|Return e -> []
|_ -> []
let compile (p : Ast.program) : result =
let _ = reset() in
let _ = collect_vars(p) in
let insts = List.concat_map stmt p in
{ code = insts; data = VarSet.elements !variables }
Error is at let compile (p: Ast.program) : result =
which is this (from the shell):
110 | let compile (p : Ast.program) : result =
^^^
Error: Syntax error
The error follows from not having properly completed the get_label function:
let get_label e =
let inst = stmt e in
let pattern = (List.nth (List.rev inst) 0) in
let label = match pattern with
|Memory (Store (Addr (Label l), Reg r)) -> l
|_ -> "Error"
Hence the in following it is associated with let label = ..., not let get_label e = ..., despite your indentation.
I suspect what you want is to just skip the label binding:
let get_label e =
let inst = stmt e in
let pattern = (List.nth (List.rev inst) 0) in
match pattern with
| Memory (Store (Addr (Label l), Reg r)) -> l
| _ -> "Error"
It might be a good idea to use a tool like ocp-indent, which will indent your code according to how the compiler will interpret the code, not how you want the compiler to interpret it. That usually helps to avoid mistakes like this :)

Awk split pattern error: empty line splits in the middle of a line

I'm trying to split a biggish text file (10Gb+) by a fixed # of empty lines with a one liner suggested here:
awk 'BEGIN {nParMax = 100000; npar = 0 ;nFile =0}
/^$/{npar++;if(npar==nParMax){nFile++;npar=0;next}}
{print $0 > "split_"nFile".out"}' fname
It gets the job done 99.99% of the time, meaning splitting the file by nParMax number of empty lines.
However, once in a time I'm getting the very last paragraph split in the middle (2-3-5 lines, instead of full say 10-15 lines), in the middle of a line.
I would really appreciate an advice on why this happen (wrong regex pattern?) and how to avoid this happening.
Thanks in advance!
Edit
The misbehaving paragraph:
# sent_id = 170247_3
# text = В то же время видеокадры с места событий свидетельствуют о том, что после звука, похожего на выстрел, находившихся на площади людей охватила паника.
1 В _ ADP _ _ 4 case _ O
2 то _ DET _ Animacy=Inan|Case=Acc|Gender=Neut|Number=Sing 4 det _ O
3 же _ PART _ _ 2 advmod _ O
4 время _ NOUN _ Animacy=Inan|Case=Acc|Gender=Neut|Number=Sing 9 obl _ O
5 видеокадры _ NOUN _ Animacy=Inan|Case=Nom|Gender=Masc|Number=Plur 9 nsubj _ O
6 с _ ADP _ _ 7 case _ O
7 места _ NOUN _ Animacy=Inan|Case=Gen|Gender=Neut|Number=Sing 5 nmod _ O
8 событий _ NOUN _ Animacy=Inan|Case=Gen|Gender=Neut|Number=Plur 7 nmod _ O
9 свидетельствуют _ VERB _ Aspect=Imp|Mood=Ind|Number=Plur|Person=3|Tense=Pres|VerbForm=Fin|Voice=Act 0 root _ O
10 о _ ADP _ _ 11 case _ O
11 том _ PRON _ Animacy=Inan|Case=Loc|Gender=Neut|Number=Sing 9 obl _ O
12 , _ PUNCT _ _ 25 punct _ O
13 что _ SCONJ _ _ 25 mark _ O
14 после _ ADP _ _ 15 case _ O
15 звука _ NOUN _ Animacy=Inan|Case=Gen|Gender=Masc|Number=Sing 25 obl _ O
16 , _ PUNCT _ _ 17 punct _ O
17 похожего _ ADJ _ Case=Gen|Degree=Pos|Gender=Masc|Number=Sing 15 amod _ O
18 на _ ADP _ _ 19 case _ O
19 выстрел _ NOUN _ Animacy=Inan|Case=Acc|Gender=Masc|Number=Sing 17 obl _ O
20 , _ PUNCT _ _ 15 punct _ O
21 находившихся _ VERB _ Animacy=Anim|Aspect=Imp|Case=Acc|Number=Plur|Tense=Past|VerbForm=Part|Voice=Act 24 acl _ O
22 на _ ADP _ _ 23 case _ O
23 площади _ NOUN _ Animacy=Inan|Case=Loc|Gender=Fem|Number=Sing 21 obl _ O
24 людей _ NOUN _ Animacy=Anim|Case=Acc|Gender=Masc|Number=Plur 25 obj _ O
25 охватила _ VERB _ Aspect=Perf|Gender=Fem|Mood=Ind|Number=Sing|Tense=Past|VerbForm=Fin|Voice=Act 11 acl _ O
26 паника _ NOUN _ Animacy=Inan|Case=Nom|Gender=Fem|Number=Sing 25 nsubj _ O
27 . _ PUNCT _ _ 9 punct _ O
The resulting split:
# sent_id = 170247_3
# text = В то же время видеокадры с места событий свидетельствуют о том, что после звука, похожего на выстрел, находившихся на площади людей охватила паника.
1 В _ ADP _ _ 4 case _ O
2 то _ DET _ Animacy=Inan|Case=Acc|Gender=Neut|Number=Sing 4 det _ O
3
The next splitted file starts cleanly, as it supposed to. Half paragraph is lost somewhere.
Edit 2
The same para (# sent_id = 170247_3$) in VIM with special chars on (:set list). The split happens on line 3 (see above):
# sent_id = 170247_3$
# text = В то же время видеокадры с места событий свидетельствуют о том, что после звука, похожего на выстрел, находившихся на площади людей охватила паника.$
1^IВ^I_^IADP^I_^I_^I4^Icase^I_^IO$
2^Iто^I_^IDET^I_^IAnimacy=Inan|Case=Acc|Gender=Neut|Number=Sing^I4^Idet^I_^IO$
3^Iже^I_^IPART^I_^I_^I2^Iadvmod^I_^IO$
4^Iвремя^I_^INOUN^I_^IAnimacy=Inan|Case=Acc|Gender=Neut|Number=Sing^I9^Iobl^I_^IO$
5^Iвидеокадры^I_^INOUN^I_^IAnimacy=Inan|Case=Nom|Gender=Masc|Number=Plur^I9^Insubj^I_^IO$
6^Iс^I_^IADP^I_^I_^I7^Icase^I_^IO$
7^Iместа^I_^INOUN^I_^IAnimacy=Inan|Case=Gen|Gender=Neut|Number=Sing^I5^Inmod^I_^IO$
8^Iсобытий^I_^INOUN^I_^IAnimacy=Inan|Case=Gen|Gender=Neut|Number=Plur^I7^Inmod^I_^IO$
9^Iсвидетельствуют^I_^IVERB^I_^IAspect=Imp|Mood=Ind|Number=Plur|Person=3|Tense=Pres|VerbForm=Fin|Voice=Act^I0^Iroot^I_^IO$
10^Iо^I_^IADP^I_^I_^I11^Icase^I_^IO$
11^Iтом^I_^IPRON^I_^IAnimacy=Inan|Case=Loc|Gender=Neut|Number=Sing^I9^Iobl^I_^IO$
12^I,^I_^IPUNCT^I_^I_^I25^Ipunct^I_^IO$
13^Iчто^I_^ISCONJ^I_^I_^I25^Imark^I_^IO$
14^Iпосле^I_^IADP^I_^I_^I15^Icase^I_^IO$
15^Iзвука^I_^INOUN^I_^IAnimacy=Inan|Case=Gen|Gender=Masc|Number=Sing^I25^Iobl^I_^IO$
16^I,^I_^IPUNCT^I_^I_^I17^Ipunct^I_^IO$
17^Iпохожего^I_^IADJ^I_^ICase=Gen|Degree=Pos|Gender=Masc|Number=Sing^I15^Iamod^I_^IO$
18^Iна^I_^IADP^I_^I_^I19^Icase^I_^IO$
19^Iвыстрел^I_^INOUN^I_^IAnimacy=Inan|Case=Acc|Gender=Masc|Number=Sing^I17^Iobl^I_^IO$
20^I,^I_^IPUNCT^I_^I_^I15^Ipunct^I_^IO$
21^Iнаходившихся^I_^IVERB^I_^IAnimacy=Anim|Aspect=Imp|Case=Acc|Number=Plur|Tense=Past|VerbForm=Part|Voice=Act^I24^Iacl^I_^IO$
22^Iна^I_^IADP^I_^I_^I23^Icase^I_^IO$
I'm not sure if this is what you're trying to do or not but to split a file of X paragraphs into n (10 below) files where X is some number greater than or equal to n as I think you're trying to do would be:
awk -v RS= -v ORS='\n\n' -n 10 '
NR==FNR { totParas=NR; parasPerFile=2; next }
(FNR % parasPerFile) == 1 {
close(out)
out = FILENAME "_out" (++c)
parasLeft = totParas - (FNR - 1)
parasPerFile = int(parasLeft/n) + (parasLeft%n ? 1 : 0)
}
{ print > out }
' file file

Replace cleverly values in a cell with macros using regex?

I've got the following macro :
Sub MacroClear()
Dim wbD As Workbook, _
wbC As Workbook, _
wsD As Worksheet, _
wsC As Worksheet, _
DicC() As Variant, _
Dic() As String, _
ValToReplace As String, _
IsInDic As Boolean
Set wbD = Workbooks.Open("D:\Users\me\Documents\macro\Dictionnary", ReadOnly:=True)
Set wbC = Workbooks("FileToTreat.xlsm")
Set wsD = wbD.Worksheets("Feuil1")
Set wsC = wbC.Worksheets("draft")
ReDim DicC(1, 0)
For i = 1 To wsD.Range("C" & wsD.Rows.Count).End(xlUp).Row
Dic = Split(wsD.Cells(i, 3), ";")
ValToReplace = Trim(wsD.Cells(i, 2))
For k = LBound(Dic) To UBound(Dic)
IsInDic = False
For l = LBound(DicC, 2) To UBound(DicC, 2)
If LCase(DicC(1, l)) <> Trim(LCase(Dic(k))) Then
'No match
Else
'Match
IsInDic = True
Exit For
End If
Next l
If IsInDic Then
'Don't add to DicC
Else
DicC(0, UBound(DicC, 2)) = Trim(Dic(k))
DicC(1, UBound(DicC, 2)) = ValToReplace
ReDim Preserve DicC(UBound(DicC, 1), UBound(DicC, 2) + 1)
End If
Next k
Next i
ReDim Preserve DicC(UBound(DicC, 1), UBound(DicC, 2) - 1)
wbD.Close
Erase Dic
For l = LBound(DicC, 2) To UBound(DicC, 2)
Cells.Replace What:="*" & Trim(DicC(0, l)) & "*", _
Replacement:=Trim(DicC(1, l)), _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next l
Erase DicC
Set wbD = Nothing
Set wbC = Nothing
Set wsD = Nothing
Set wsC = Nothing
End Sub
I'll try to explain : It takes from the dictionnary my "words to replace" (column C), all separated by a ";", and my "primary words" (column B).
image http://img11.hostingpics.net/pics/403257dictionnary.png
Then it searches in all the cells of my "file to treat" (via Cells.Replace), if it finds something in column C of my dictionnary, it replaces it with what's in column B.
But now that I've got "SCE" in my column C (For Sony Computer Entertainment, to be replaced by Sony in column B), even when SCE is in a word (for example : ascend), it replaces the word with Sony. I don't want to replace it if it's inside a word...
In Java, I'd have done it easily with p = Pattern.compile("[^a-zA-Z]"+keyword+"[^a-zA-Z]", Pattern.CASE_INSENSITIVE);
but I have no idea how to solve this problem in VBA. I tried some things but it didn't work, had errors etc. so I came back to the start.
So I changed few parameters in the replace method and proposed a loop for all your cells, you'll just have to set the right column (in second proposition : here B=2).
Parameters :
LookAt:=xlWhole 'To search for whole expression
SearchOrder:=xlByColumns 'Search in column
MatchCase:=True 'Will look for the expression with the same casing (not sure about this word...)
Try one of these :
For l = LBound(DicC, 2) To UBound(DicC, 2)
Cells.Replace What:="*" & Trim(DicC(0, l)) & "*", _
Replacement:=Trim(DicC(1, l)), _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
MatchCase:=True, _
SearchFormat:=False, _
ReplaceFormat:=False
Next l
Or with the loop on each cell :
For l = LBound(DicC, 2) To UBound(DicC, 2)
For k = 1 To wsC.Rows(wsC.Rows.Count).End(xlUp).Row
wsC.Cells(i, 2).Replace What:="*" & Trim(DicC(0, l)) & "*", _
Replacement:=Trim(DicC(1, l)), _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
MatchCase:=True, _
SearchFormat:=False, _
ReplaceFormat:=False
Next k
Next l

Backtracking PROLOG:display all sub-sets with aspect of "mountain"

Can you help me with this problem of backtracking in Prolog?
The string a1, ..., an is given and it consists of distinct integers.
It is required to display all sub-sets with aspect of "mountain"
(a set has a "mountain" aspect if the elements increase to a certain
point and then decrease).
Eg. 10 16 27 18 14 7.
I know how to verify if a set has mountain aspect...
but now I need to display all solutions :(
% munte(list,integer,integer)
% rezolvare(list)
munte([],2,_).
munte([H|[]],2,_).
munte([H|[H1|T]],Pas,Nr):-
Pas = 1,
H<H1,!,
L=[H1|T],
Nr1=Nr+1,
munte(L,Pas,Nr1).
munte([H|[H1|T]],Pas,Nr):-
Pas = 1,
H>H1,
Nr>1,!,
L=[H1|T],
Pas1=Pas + 1,
Nr1=Nr+1,
munte(L,Pas1,Nr1).
munte([H|[H1|T]],Pas,Nr):-
Pas = 2,
H>H1,!,
L=[H1|T],
Nr1=Nr+1,
munte(L,Pas,Nr1).
rezolvare(L1):-munte(L1,1,1).
Note that this answer deals with subsequences of lists, not subsets of sets.
In the following we use clpfd:
:- use_module(library(clpfd)).
We define hillSubseq_in/2 based on tfilter/3, (*)/2 and hill/1:
hillSubseq_in(Zs,Xs) :-
tfilter(*,Xs,Zs),
hill(Zs).
Sample query:
?- hillSubseq_in(Zs,[2,9,3,1,5,9,2]).
Zs = [2,9,3,1]
; Zs = [2,9,3,2]
; Zs = [2,9,3]
; Zs = [2,9,1]
; Zs = [2,9,5,2]
; Zs = [2,9,5]
; Zs = [2,9,2]
; Zs = [2,3,1]
; Zs = [2,3,5,9,2]
; Zs = [2,3,5,2]
; Zs = [2,3,9,2]
; Zs = [2,3,2]
; Zs = [2,5,9,2]
; Zs = [2,5,2]
; Zs = [2,9,2]
; Zs = [3,5,9,2]
; Zs = [3,5,2]
; Zs = [3,9,2]
; Zs = [1,5,9,2]
; Zs = [1,5,2]
; Zs = [1,9,2]
; Zs = [5,9,2]
; false.