Print Combinations of Strings from List of List of String




Comment hidden because of low score. Click to expand.
1
of 1 vote

/* Credits to Stefan Wagner for the base code */

import java.util.*;
import java.lang.*;
import java.io.*;

class CartesianIterator <T> implements Iterator <List <T>> {

    private final List <List <T>> lilio;    
    private int current = 0;
    private final long last;

    public CartesianIterator (final List <List <T>> llo) {
        lilio = llo;
        long product = 1L;
        for (List <T> lio: lilio)
            product *= lio.size ();
        last = product;
    } 

    public boolean hasNext () {
        return current != last;
    }

    public List <T> next () {
        ++current;
        return get (current - 1, lilio);
    }

    public void remove () {
        ++current;
    }

    private List<T> get (final int n, final List <List <T>> lili) {
        switch (lili.size ())
        {
            case 0: return new ArrayList <T> (); // no break past return;
            default: {
                List <T> inner = lili.get (0);
                List <T> lo = new ArrayList <T> ();
                lo.add (inner.get (n % inner.size ()));
                lo.addAll (get (n / inner.size (), lili.subList (1, lili.size ())));
                return lo;
            }
        }
    }
}

class CartesianIterable <T> implements Iterable <List <T>> {

    private List <List <T>> lilio;  

    public CartesianIterable (List <List <T>> llo) {
        lilio = llo;
    }

    public Iterator <List <T>> iterator () {
        return new CartesianIterator <T> (lilio);
    }
}
class Ideaone {

    public static void main (String[] args) {
        List <String> la = Arrays.asList (new String [] {"quick ", "slow "});
        List <String> lb = Arrays.asList (new String [] {"brown ", "red "});      
        List <String> lc = Arrays.asList (new String [] {"fox", "dog"});
        List <List <String>> llc = new ArrayList <List <String>> ();
        llc.add (la);
        llc.add (lb);
        llc.add (lc);

        CartesianIterable <String> ci = new CartesianIterable <String> (llc);
        for (List<String> lo: ci)
            show (lo);
    }

    public static void show (List <String> lo) {
        for (Object o: lo)
            System.out.print (o);
            System.out.print ("\n");
    }
}

- Satya Sampathirao October 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since every List has two choices, it's essentially a binary decision. You can think of it as a 3 bit binary number.
e.g.
000
001
010
.....
101
110
111

If you can get the binary representation and convert it to each choice, you can come up with the answer.

For example,
if the first bit is zero then choose 'quick'
if the second bit is one then choose 'red'
if the third bit is zero then choose 'fox'

private static String binary(int numOfBits, int value) {
		StringBuilder builder = new StringBuilder();
		while (numOfBits>0) {
			int a = value % 2;
			if (a>0) builder.append("1");
			else builder.append("0");
			value /= 2;
			numOfBits--;
		}
		return builder.toString();
	}


	private static List<String> solve(List<String[]> input) {
		List<String> result = new LinkedList<String>();
		int size = input.size();
		int max = (int) ((Math.pow(2, size)) - 1);
		List<String> binaryValues = new LinkedList<String>();
		for (int i = 0; i<=max; i++) {
			String binary = binary(size, i);
			binaryValues.add(binary);
		}
		for (String s : binaryValues) {
			StringBuilder builder = new StringBuilder();
			for (int i=0; i<s.length(); i++) {
				if (s.charAt(i)=='0')
					builder.append(input.get(i)[0]);
				else
					builder.append(input.get(i)[1]);
				builder.append(" ");
			}
			result.add(builder.toString());
		}
		return result;
	}

- phishman3579 February 15, 2015 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More