Skip to content

Commit

Permalink
#2 - partial lambda stack
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Feb 11, 2024
1 parent 76e03b7 commit 2209f26
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.obrienlabs.compsci.collections;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

public class Stack {

boolean isBalanced(String s) {
boolean balanced = true;
// index is important
String pushKeys = "[{(";
String pullKeys = "]})";
Map<String, Integer> dictionaryIndex = new HashMap<String, Integer>();
Map<String, Integer> dictionaryCost = new HashMap<String, Integer>();

// initialize lookup map
pushKeys.chars().forEach(x -> dictionaryCost.put(String.valueOf((char)x), Integer.valueOf(1)));
pullKeys.chars().forEach(x -> dictionaryCost.put(String.valueOf((char)x), Integer.valueOf(-1)));

// initialize stacks
Map<Integer, Integer> stack = new HashMap<Integer, Integer>();
for(int i=0; i<pushKeys.length(); i++) {
stack.put(i, 0);
dictionaryIndex.put(pushKeys.substring(i, i+1), i);
dictionaryIndex.put(pullKeys.substring(i, i+1), i);
}

Stream.of(s).forEach(x -> System.out.println(x));
// lookup dictionary value and add to stack at index - flatmap
// parse both key strings
// use index in dictionary to add to stack
//Stream.of(pushKeys).forEach(x -> {

//});
//////Integer index = 0;
//Stream.of(s).forEach(x -> {
for(int index=0; index<s.length(); index++) {
// get lookup position
int position = dictionaryIndex.get(s.substring(index, index+1));
// get value to add to
int current = stack.get(position);
int adjust = dictionaryCost.get(s.substring(index, index+1));
stack.put(position, current + adjust);
}

// check numbers in stack - all must be zero
for(Integer value : stack.values()) {
if(value != 0) {
balanced = false;
}
}
//if(stack.entrySet().stream().filter(x -> !x.getValue().equals(0)).count() > 0)
// balanced = true;
//});

/*List<String> list = Stream.of(s)
.filter(x -> x.equals(dictionary.get(x)))
.collect(Collectors.toList());
list.forEach(x -> System.out.println(x));*/

return balanced;
}

public static void main(String[] args) {
Stack stack = new Stack();
String aString = "{}([()][])";
System.out.println(stack.isBalanced(aString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void testContructor() {
StringBuffer buffer = new StringBuffer();
leftList.stream().forEach(x -> buffer.append(x.getData()).append(","));
System.out.println("left nodes: " + tree.printOn(leftList));
System.out.println("left count: " + Integer.parseInt(Long.toString(leftList.stream().count())));

}
}

0 comments on commit 2209f26

Please sign in to comment.