Symmetric difference: Difference between revisions

Line 1,041:
<pre>
[Serena Jim]
</pre>
 
=={{header|REXX}}==
<lang rexx>
/*REXX program to find the symmetric difference (between two strings). */
 
a='["John", "Serena", "Bob", "Mary", "Serena"]'
b='["Jim", "Mary", "John", "Jim", "Bob"]'
 
say 'list A=' a
say 'list B=' b
a.=0 /*falisify all the a.k._ booleans. */
a.1=a /*store listA as a stemmed array (1).*/
a.2=b /*store listA as a stemmed array (2).*/
 
 
do k=1 for 2 /*process both lists (stemmed array).*/
if left(a.k,1)=='[' then a.k=substr(a.k,2)
if right(a.k,1)==']' then a.k=substr(a.k,1,length(a.k)-1)
 
do j=1 /*parse names in list, they may have blanks.*/
if left(a.k,1)==',' then a.k=substr(a.k,2) /*strip comma (if any)*/
if a.k='' then leave /*Null? We're done.*/
parse var a.k '"' _ '"' a.k /*get the list's name.*/
a.k.j=_ /*store the list name.*/
a.k._=1 /*make a boolean val. */
end
 
a.k.0=j-1 /*number of list names*/
end
 
SD='' /*symmetric diff list.*/
SD.=0 /*falsify all SD bools*/
 
do k=1 for 2 /*process both lists. */
ko=word(2 1,k) /*point to other list.*/
 
do j=1 for a.k.0 /*process list names. */
_=a.k.j /*a name in the list. */
if \a.ko._ & \SD._ then do /*if not in both... */
SD=SD '"'_'",' /*add to sym diff list*/
SD._=1 /*trueify a boolean. */
end
end
end
 
SD="["strip(space(SD),'T',",")']' /*clean up and bracket*/
say
say 'symmetric difference='SD /*show and tell time. */
 
SA='' /*symmetric AND list. */
SA.=0 /*falsify all SA bools*/
 
do j=1 for a.1.0 /*process A list names*/
_=a.1.j /*a name in the A list*/
if a.1._ & a.2._ & \SA._ then do /*if common to both...*/
SA=SA '"'_'",' /*add to sym AND list.*/
SA._=1 /*trueify a boolean. */
end
end
 
SA="["strip(space(SA),'T',",")']' /*clean up and bracket*/
say
say ' symmetric AND='SA /*show and tell time. */
</lang>
Output:
<pre style="height:30ex;overflow:scroll">
list A= ["John", "Serena", "Bob", "Mary", "Serena"]
list B= ["Jim", "Mary", "John", "Jim", "Bob"]
 
symmetric difference=["Serena", "Jim"]
 
symmetric AND=["John", "Bob", "Mary"]
</pre>