java - Create all combinations of mappings of characters to truth values -
i have set of characters {'p', 'q', 'r', ...}
, want call run()
(not important does) on permutations of mappings characters boolean values. example, if set of characters {'p','q','r'}
, naively went through every permutation manually, this:
private static void runmaps(){ hashmap<character,boolean> m = new hashmap<>(); m.put('p', true); m.put('q', true); m.put('r', true); run(m); m = new hashmap<>(); m.put('p', true); m.put('q', true); m.put('r', false); run(m); // ... // 8 permutations }
it's bugging me because know can (and should able to) use recursion here i'm struggling.
edit: after while managed working. there's added stuff in code below general idea shown.
private static boolean runmaps(syntaxtree formula, arraylist<string> chars, hashmap<string, boolean> map, int index) { if (index == chars.size()) { return checkformula(formula, map).getdata().equals("true"); } else { hashmap<string, boolean> newmap1 = (hashmap<string, boolean>) map.clone(); newmap1.put(chars.get(index), true); hashmap<string, boolean> newmap2 = (hashmap<string, boolean>) map.clone(); newmap2.put(chars.get(index), false); return runmaps(formula, chars, newmap1, index + 1) && runmaps(formula, chars, newmap2, index + 1); }
}
the basic concept go through map in order, , divide up, changing 1 character @ time.
something in pseudo code:
character[] keys = map.getkeys() // or similar sounding int length = keys.length int position = 0 function(keys, position, length, map) { if position >= length return map.put(keys[position], false) // run permutations 0 in front function(keys, position+1, length, map) run(map) map.put(keys[position], right) // run permutations 1 in front function(keys, position+1, length, map) run(map) }
Comments
Post a Comment