Results 1 to 3 of 3
  1. #1
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default How to generate alternate color of balls combination for 3 boxes?

    I am working on a project in which I have three box (as of now) and each box will have some color of balls

    So I am storing them in a Map of String and List of String as mention below.

    Map<String, List<String>> boxBallMap = new LinkedHashMap<String, List<String>>();

    Data in the above map is like this -

    Java Code:
    	{box1=[blue, red, orange]}
    	{box2=[blue, red]}
    	{box3=[blue, red, orange]}
    ProblemStatement:-

    Basis on the above input, I need to return a mapping which will be List<Map<String, String>>, let's say for above input, below mapping would be return as an output -

    Java Code:
    	[{box1=blue, box2=red, box3=orange}, 
    	{box1=red, box3=blue}, 
    	{box1=orange, box2=blue, box3=red}]
    Here if you see, each row has alternate color of balls for each box - meaning blue for box1, red for box2, orange for box3 in first row. I cannot have same color of balls in each row. So this combination is not possible as it has same color of balls for two boxes in one row.

    Java Code:
    {box1=blue, box2=blue, box3=orange}
    And also, in the second row, I won't use those balls which have been used in the first row for that box. In second row, box1 has red why? bcoz blue was already used in the first row for box1 and box3 has blue and no box2 in second row.

    The output combination is getting generated basis on the input being passed as shown above.

    I started with the below code -

    Java Code:
    	List<String> balls1 = Arrays.asList("red", "blue", "orange");
    	List<String> balls2 = Arrays.asList("red", "blue");
    	List<String> balls3 = Arrays.asList("red", "blue", "orange");
    
    	Map<String, List<String>> maps = new LinkedHashMap<String, List<String>>();
    	maps.put("box1", balls1);
    	maps.put("box2", balls2);
    	maps.put("box3", balls3);
    
    	List<Map<String, String>> mappingss = generateMappings(maps);
    	System.out.println(mappingss);

    Below is my method in which the crux of logic should be there

    Java Code:
        private static List<Map<String, String>> generateMappings(Map<String, List<String>> map) {
    	
    		return null;
        }
    Below algorithm might work but still not sure how should I fit this in the code -

    - sort the boxes by the number of balls they have in it (ascending, from the smallest to the largest box).
    - while there are colors left
    - loop over the sorted list of boxes
    - in each iteration pick a color from the box (if there is one left), that is not already picked in the current iteration (of the while loop)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: How to generate alternate color of balls combination for 3 boxes?

    Quote Originally Posted by raihan26 View Post
    Below algorithm might work but still not sure how should I fit this in the code -

    - sort the boxes by the number of balls they have in it (ascending, from the smallest to the largest box).
    - while there are colors left
    - loop over the sorted list of boxes
    - in each iteration pick a color from the box (if there is one left), that is not already picked in the current iteration (of the while loop)
    Which part of that is giving you trouble? What have you tried so far?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default Re: How to generate alternate color of balls combination for 3 boxes?

    This is what I have tried but this only works for when number of balls is same in each box-

    Java Code:
    public List<Map<String, String>> createMappings(List<String> boxes, List<String> balls) {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        for(int i = 0; i < balls.size(); i++) {
            Map<String, String> row = new HashMap<String,String>();
            for(int j = 0; j < boxes.size(); j++) {
                String box = boxes.get(j);
                int ballIndex = (j + i) % balls.size();
                String ball = balls.get(ballIndex);
                row.put(box, ball);
            }
            result.add(row);
        }
        return result;
    }

Similar Threads

  1. Bouncing Balls Program, List balls do not move
    By VettesRus in forum New To Java
    Replies: 10
    Last Post: 10-01-2013, 04:39 PM
  2. Are there alternate approaches?
    By ras_oscar in forum New To Java
    Replies: 3
    Last Post: 09-11-2013, 03:04 PM
  3. Java HangMan - Alternate between players
    By JohnyQ in forum Networking
    Replies: 1
    Last Post: 11-22-2011, 02:45 AM
  4. Alternate Algorithm? Can you think of one?
    By VinceGuad in forum New To Java
    Replies: 8
    Last Post: 03-18-2009, 04:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •