Monday, 27 June 2016

Challenge #272 [Easy] What's in the bag?

Another day another challenge, I need to program a lot more as at the moment all I have been doing is studying for the OCP exam, it is harder than I had anticipated with all the functional programming. As I had thought it would be similar to linq, perhaps the understanding was not there at the time. Either way I am not practicing enough and caught up in the what if I fail stuff.

User wins $10,000 due to botched forced Windows 10 upgrade.

Properly want an LED peggy board to make a flashing mooninite sign, it would change between Ignignokt and Err. 

YouTube 500 error that I actually experienced. 

500 error something went wrong with what appears to be a base 64 encoded string

And on to the challenge, I look it against the other examples on daily programmer, so I have a long way to go.

package com.company;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Map<Character, Integer> fullBag = new HashMap<Character, Integer>() {{
   put('A', 9); put('B', 2); put('C', 2); put('D', 4); put('E', 12);
   put('F', 2); put('G', 3); put('H', 2); put('I', 9); put('J', 1);
   put('K', 1); put('L', 4); put('M', 2); put('N', 6); put('O', 8);
   put('P', 2); put('Q', 1); put('R', 6); put('S', 4); put('T', 6);
   put('U', 4); put('V', 2); put('W', 2); put('X', 1); put('Y', 2);
   put('Z', 1); put('_', 2);
        }};

        //String tilesToRemove = "PQAREIOURSTHGWIOAE_";
        String tilesToRemove = "LQTOONOEFFJZT";
        //String tilesToRemove = "AXHDRUIOR_XHJZUQEE";
        for (Character tile : tilesToRemove.toCharArray()) {
            if (fullBag.keySet().contains(tile)) {
                fullBag.put(tile, fullBag.get(tile) -1);
            }
        }

        boolean hasNegativeTiles = fullBag.values().stream().filter( x -> x < 0 ).limit(1).count() > 0;
        if (hasNegativeTiles) {
            List<Character> negativeTiles = new ArrayList<>();
            for (Character tile : fullBag.keySet()) {
                if (fullBag.get(tile) < 0) { negativeTiles.add(tile);}
            }
            System.out.println("Invalid input. More "
                    +  negativeTiles.toString().substring(1,  negativeTiles.toString().length()-1)
                    + " have been taken from the bag than possible.");
        } else {
            List<Character> charsNeeded;
            Map<Integer, List<Character>> filteredMap = 

              new HashMap<>();
            for (int i = 12; i >= 0; i-- ) {
                charsNeeded = new ArrayList<>();

                for (Character c : fullBag.keySet()) {
                    if (fullBag.get(c) == i) {
                        charsNeeded.add(c);
                    }
                }

                if (!charsNeeded.isEmpty()) {
                    filteredMap.put(i, charsNeeded);
                }
            }

            List<Integer> reversedList = new ArrayList<>(filteredMap.keySet());
            Collections.reverse(reversedList);
            for (Integer key : reversedList) {
                String collectionToPrint = filteredMap.get(key).toString();
                System.out.println(key + ":\t" + collectionToPrint.substring(1, collectionToPrint.length() -1));
            }
        }
    }
}

No comments:

Post a Comment