0

PROBLEM STATEMENT: Print the items that differ between the following two left and right sorted data sets:

A       A
B       C
C       D
D       E
G       F
H       EOF
EOF

The proposed solution should print:

B E F G H

Here is my data-driven implementation of this algorithm in pseudo-code. It appears to produce the desired results so my questions are:

  • Are there any flaws in terms of logic, efficiency or anything else using this approach?
  • Would this approach be called a data-driven design or would it be categorized under a different name?

Here is the pseudo-code:

METHOD PRINT_DIFFERENCES
    LEFT_DATA_NOT_EOF = CALL READ_LEFT_RECORD()
    RIGHT_DATA_NOT_EOF = CALL READ_RIGHT_RECORD()

    WHILE LEFT_DATA_NOT_EOF AND RIGHT_DATA_NOT_EOF
        WHILE LEFT_DATA_NOT_EOF AND RIGHT_DATA_NOT_EOF AND LEFT_KEY IS LESS THAN RIGHT_KEY
            PRINT LEFT_KEY
            LEFT_DATA_NOT_EOF = CALL READ_LEFT_RECORD()
        LOOP

        WHILE LEFT_DATA_NOT_EOF AND RIGHT_DATA_NOT_EOF AND RIGHT_KEY IS LESS THAN LEFT_KEY
            PRINT RIGHT_KEY
            RIGHT_DATA_NOT_EOF = CALL READ_RIGHT_RECORD()
        LOOP

        WHILE LEFT_DATA_NOT_EOF AND RIGHT_DATA_NOT_EOF AND LEFT_KEY IS EQUAL TO RIGHT_KEY
            LEFT_DATA_NOT_EOF = CALL READ_LEFT_RECORD()
            RIGHT_DATA_NOT_EOF = CALL READ_RIGHT_RECORD()
        LOOP
    LOOP

    WHILE LEFT_DATA_NOT_EOF
        PRINT LEFT_KEY
        LEFT_DATA_NOT_EOF = CALL READ_LEFT_RECORD()
    LOOP

    WHILE RIGHT_DATA_NOT_EOF
        PRINT RIGHT_KEY
        RIGHT_DATA_NOT_EOF = CALL READ_RIGHT_RECORD()
    LOOP
METHOD END

METHOD READ_LEFT_RECORD
    RESULT = MOVE TO NEXT LEFT RECORD

    IF RESULT IS EQUAL TO TRUE THEN
        SET LEFT_KEY FROM CURRENT LEFT CURSOR    
    END IF

    RETURN RESULT
METHOD END

METHOD READ_RIGHT_RECORD
    RESULT = MOVE TO NEXT RIGHT RECORD

    IF RESULT IS EQUAL TO TRUE THEN
        SET RIGHT_KEY FROM CURRENT RIGHT CURSOR    
    END IF

    RETURN RESULT
METHOD END
  • that "pseudo code" really hurt the eyes – dagnelies Oct 27 '17 at 14:20
  • A pseudo code is supposed to be easier to read than actual code .. right ? – Walfrat Oct 27 '17 at 14:27
3

I would simplify

WHILE LEFT_DATA_NOT_EOF AND RIGHT_DATA_NOT_EOF
    IF LEFT_KEY IS LESS THAN RIGHT_KEY
        PRINT LEFT_KEY
        LEFT_DATA_NOT_EOF = CALL READ_LEFT_RECORD()

    ELSE IF RIGHT_KEY IS LESS THAN LEFT_KEY
        PRINT RIGHT_KEY
        RIGHT_DATA_NOT_EOF = CALL READ_RIGHT_RECORD()

    ELSE 
        LEFT_DATA_NOT_EOF = CALL READ_LEFT_RECORD()
        RIGHT_DATA_NOT_EOF = CALL READ_RIGHT_RECORD()
LOOP
  • your while loop is AND'ed ... would'nt this fail for mismatches at the tail end? – Constantin Nov 1 '17 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.