Challenge: 


  Print set intersections.


Specifications: 


  Your program should accept as its first argument a path to a filename.
  Each line in the file is a test case.
  Each test case contain two semicolon delimited sorted lists of numbers in ascending order, whose numbers are separated by a comma.
  
  Print out the ascending order sorted intersection of the lists, one per line.
  Print empty new line in case the lists have no intersection


Solution:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;

public class SetIntersection {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File(args[0]));
        String[] parts;

        while (input.hasNextLine()) {
            parts = input.nextLine().split(";");

            System.out.println(
                findIntersection(
                    toIntArray(parts[0].split(",")),
                    toIntArray(parts[1].split(","))
                )
            );
        }
    }

    private static String findIntersection(int[] foo, int[] bar) {
        Set&lt;Integer&gt; intersection = new LinkedHashSet&lt;&gt;();

        for (int i = 0, j = 0; i &lt; foo.length; i++) {
            while (foo[i] &gt; bar[j] &amp;&amp; j &lt; bar.length) {
                j++;
            }
            if (foo[i] &lt; bar[j]) {
                continue;
            } else {
                intersection.add(bar[j++]);
            }
        }

        if (intersection.isEmpty()) {
            return ""; // none found
        }

        String result = intersection.toString();
        return result.substring(1, result.length() - 1);
    }

    private static int[] toIntArray(String[] array) {
        int[] nums = new int[array.length];

        for (int i = 0; i &lt; array.length; i++) {
            nums[i] = Integer.parseInt(array[i]);
        }

        return nums;
    }
}


Sample Input: 


  9,10,11;33,34,35
  3,7,8,22;11,22
  11,12,13,14;14,15,16
  20,21,22;45,46,47
  77,78,79;78,79,80,81,82
  33,35;3,18,26,35


Sample Output:


  
  22
  14  
  
  78, 79
  35


I wanted to take advantage of the fact the lists were sorted, but I fear my efforts only served to render my code convoluted and inefficient. What do you think?


Question: can a ring homomorphism map an integral domain to a non integral domain?
Answer: The quotient map $f: \mathbb{Z} \to \mathbb{Z}/(4)$ maps an integral domain $\mathbb{Z}$ to a ring with a (nilpotent) zerodivisor $\mathbb{Z}/(4)$. Now, the quotient map $g: \mathbb{Z}/(4) \to (\mathbb{Z}/(4))/(2) = \mathbb{Z}/(2)$ maps this ring to the field $\mathbb{Z}/(2)$.