What is the difference between (0:2):4 and 0:(2:4)?
Both neglects the 2nd part of the bracket thus printing values similar to writing (0:4) and (0:2) respectively.
I could generalize from this that the bracket's first element is only working in this vector. But I would like to know the actual reason why is it happening.
the colon operator has lower priority than (), so, matlab first evaluates the vector inside the parenthesis, then, if one of the operands is a vector, colon only takes the first value. here are the evaluation steps:
(0:2):4 -> (0:2)=[0 1 2] -> 0:4 -> [0,1,2,3,4]
0:(2:4) -> (2:4)=[2 3 4] -> 0:2 -> [0,1,2]
In Datastage, I have an INTEGER field from a Seq File 0, in a transformer i wanted to write a constraint that if the Source data from the seq file is <> 0 or <> Numeric (numbers) then it should be written in seq file 2 and other Numeric data and 0 into Seq File 1 sequential file.
Please help me out.
Seq File 0 ->Transformer(Constraint) -> Seq File 1 (0 or Other Numerics) and
-> Seq File 2 (Blanks and Non-numeric)
Check out the num Function or alpha function to check wether it is a number or alphanumeric value. You may have to handle the empty case separately.
I have a String stored in a table with two logic gates which looks like 1*((CUBL>1)*((DIFL>1)*1)).
I have been able to replace the text with values from a different table i.e. 1*((1500>1)*((0>1)*1)). The issue i am having is being able to evaluate the equations between the brackets. The following is a list of steps that i believe are needed.
1*((CUBL>1)*((DIFL>1)*1)) -> 1*((1500>1)*((0>1)*1))
1*((1500>1)*((0>1)*1)) -> 1*(1*(0*1))
1*(1*(0*1)) -> 1*(1*0)
1*(1*0) -> 1*0 = 0
I'm trying to create a parser for program. For example,
I entered (what I want)
"(2+3)-4" it will become something like this "(minus, (plus, num 2, num 3),num 4)"
What I've done so far..
"(2+3)-4" I then split it and it becomes list Z = ["(","2","+","3",")","-","4"] then I compared if "-" is a member of Z, if true I append the element "-" into a new list ["-"]
I'm not sure if the way I'm doing is correct, I'm new to Er-lang and struggling quite a lot. If anyone is able to offer me some insight, thanks.
Consider the following, which returns a tuple-based representation of its input:
parse(Expr) ->
Elems = re:split(Expr, "([-+)(])", [{return,list}]),
parse(lists:filter(fun(E) -> E /= [] end, Elems), []).
parse([], [Result]) ->
Result;
parse([], [V2,{op,Op},V1|Tacc]) ->
parse([], [{Op,V1,V2}|Tacc]);
parse(["("|Tail], Acc) ->
parse(Tail, [open|Acc]);
parse([")"|Tail], [Op,open|TAcc]) ->
parse(Tail, [Op|TAcc]);
parse(["+"|Tail], Acc) ->
parse(Tail, [{op,plus}|Acc]);
parse(["-"|Tail], Acc) ->
parse(Tail, [{op,minus}|Acc]);
parse([V2|Tail], [{op,Op},V1|Tacc]) ->
parse(Tail, [{Op,V1,{num,list_to_integer(V2)}}|Tacc]);
parse([Val|Tail], Acc) ->
parse(Tail, [{num,list_to_integer(Val)}|Acc]).
The first function, parse/1, splits the expression along the + and - operators and parentheses, preserving these in the resulting list. It then filters that list to remove empty elements, and passes it with an empty accumulator to parse/2.
The parse/2 function has eight clauses, described below:
The first two handle the case when the parsed input list has been exhausted. The second of these handles the case where multiple elements in the accumulator need to be collapsed into a single tuple consisting of operator and operands.
The next two handle clauses parentheses. When we see an open parenthesis, we push an atom open into the accumulator. Upon seeing the matching close parenthesis, we expect to see an operation tuple and the atom open in the accumulator, and we replace them with just the tuple.
Clauses 5 and 6 handle + and - respectively. Each just pushes a {op,Operator} tuple into the accumulator, where Operator is either the atom plus or the atom minus.
The final two clauses handle values. The first one handles the case where the accumulator holds a value and an op tuple, which gets replaced with a full operation tuple consisting of the atom plus or minus followed by two num tuples each holding integer operands. The last clause just handles plain values.
Putting this in a module p, compiling it, and running it in an Erlang shell yields the following:
1> p:parse("2+3").
{plus,{num,2},{num,3}}
2> p:parse("(2+3)-4").
{minus,{plus,{num,2},{num,3}},{num,4}}
When I add a line to the middle of a file, all following lines have their number incremented.
Is there a utility that generates the list of equivalent line numbers between two files?
The output would be something like:
1 1
2 2
3 4 (line added)
4 5
One can probably create such utility by using dynamic programming in a way similar to the diff algorithm. Seems useful, hasn't already been done?
I found out it is pretty easy to do with python's difflib:
import difflib
def seq_equivs(s1, s2):
equiv = []
s = difflib.SequenceMatcher(a=s1, b=s2)
for m in s.get_matching_blocks():
if m[2] == 0:
break
for n in range(1, 1+m[2]):
equiv.append((m[0]+n, m[1]+n))
return equiv
Example usage:
f1 = open('file1.txt').read().split('\n')
f2 = open('file2.txt').read().split('\n')
for equivs in seq_equivs(f1, f2):
print('%d %d' % equivs)