How to resolve the algorithm Symmetric difference step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Symmetric difference step by step in the AWK programming language

Table of Contents

Problem Statement

Given two sets A and B, compute

( A ∖ B ) ∪ ( B ∖ A ) .

{\displaystyle (A\setminus B)\cup (B\setminus A).}

That is, enumerate the items that are in A or B but not both. This set is called the symmetric difference of A and B. In other words:

( A ∪ B ) ∖ ( A ∩ B )

{\displaystyle (A\cup B)\setminus (A\cap B)}

(the set of items that are in at least one of A or B minus the set of items that are in both A and B). Optionally, give the individual differences (

A ∖ B

{\displaystyle A\setminus B}

and

B ∖ A

{\displaystyle B\setminus A}

) as well.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Symmetric difference step by step in the AWK programming language

Source code in the awk programming language

# syntax: GAWK -f SYMMETRIC_DIFFERENCE.AWK
BEGIN {
    load("John,Bob,Mary,Serena",A)
    load("Jim,Mary,John,Bob",B)
    show("A \\ B",A,B)
    show("B \\ A",B,A)
    printf("symmetric difference: ")
    for (i in C) {
      if (!(i in A && i in B)) {
        printf("%s ",i)
      }
    }
    printf("\n")
    exit(0)
}
function load(str,arr,  i,n,temp) {
    n = split(str,temp,",")
    for (i=1; i<=n; i++) {
      arr[temp[i]]
      C[temp[i]]
    }
}
function show(str,a,b,  i) {
    printf("%s: ",str)
    for (i in a) {
      if (!(i in b)) {
        printf("%s ",i)
      }
    }
    printf("\n")
}


  

You may also check:How to resolve the algorithm Department numbers step by step in the Fortran programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the bc programming language
You may also check:How to resolve the algorithm Here document step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Word wheel step by step in the Wren programming language