Jump to content

Compare a list of strings: Difference between revisions

Added Mathcad example.
(Added Mathcad example.)
Line 1,728:
<pre>true
false</pre>
 
=={{header|Mathcad}}==
Mathcad is a non-text-based programming environment. The expressions below are an approximations of the way that they are entered (and) displayed on a Mathcad worksheet. The worksheet is available at xxx_tbd_xxx
 
This particular version of "Compare a list of strings" was created in Mathcad Prime Express 7.0, a free version of Mathcad Prime 7.0 with restrictions (such as no programming or symbolics). All Mathcad numbers are complex doubles. There is a recursion depth limit of about 4,500. Strings are a distinct data and are not conceptually a list of integers.
 
<lang Mathcad>-- define list of list of strings (nested vector of vectors of strings)
-- Mathcad vectors are single column arrays.
-- The following notation is for convenience in writing arrays in text form.
-- Mathcad array input is normally via Mathcad's array operator or via one of the
-- array-builder functions, such as stack or augment.
-- "," between vector elements indicates a new row.
-- " " between vector elements indicates a new column.
 
list:=["AA","AA","AA"],["AA","BB","CC"],["AA","CC","BB"],["CC","BB","AA"],["AA","ACB","BB","CC"],["AA"]]
 
-- define functions head and rest that return the first value in a list (vector)
-- and the list excluding the first element, respectively.
 
head(v):=if(IsArray(v),v[0,v)
rest(v):=if(rows(v)>1,submatrix(v,1,rows(v)-1,0,0),0)
 
-- define a function oprel that iterates through a list (vector) applying a comparison operator op to each pair of elements at the top of the list.
-- Returns immediately with false (0) if a comparison fails.
 
oprel(op,lst,val):=if(op(val,head(lst)),if(rows(lst)>1,oprel(op,rest(lst),head(lst)),1),0)
 
oprel(op,lst):=if(rows(lst)>1,oprel(op,rest(lst),head(lst)),1)
 
-- define a set of boolean comparison functions
-- transpose represents Mathcad's transpose operator
-- vectorize represents Mathcad's vectorize operator
 
eq(a,b):=a=b (transpose(vectorize(oprel,list))) = [1 0 0 0 0 1] -- equal
lt(a,b):=a<b (transpose(vectorize(oprel,list))) = [0 1 0 0 1 1] -- ascending
 
-- oprel, eq and lt also work with numeric values
 
list:=[11,11,11],[11,22,33],[11,33,22],[33,22,11],[11,132,22,33],[11]]
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.