Skip to content

Commit

Permalink
#7 - matrix reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Jul 22, 2024
1 parent 96bca4f commit a30f6b0
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions metrichash.map/src/main/java/com/metrichash/map/ScratchPad.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public void matrixReductionForAdding() {

//Stream.of(m1).flatMap(Stream::of).toArray(Long[]::new);
System.out.println("stream using forEach - not flatmMap()");
Stream.of(m1).forEach(y -> LongStream.of(y).forEach(x -> System.out.println(x)));

//Stream.of(m1).forEach(y -> LongStream.of(y).forEach(x -> System.out.println(x)));

}

Expand All @@ -29,6 +30,7 @@ public void optional() {
Integer target = null;

//Optional<Integer> srcOptional = Optional.of(source);
// in case the source is null
Optional<Integer> srcOptional = Optional.ofNullable(source);

System.out.println("Optional");
Expand All @@ -37,10 +39,42 @@ public void optional() {
}


public boolean isPalindrome(String input) {
boolean criteria = true;

/*
* abba
* Algorithm:
* convert string to IntStream
* Brute force: iterate from both sides - once a match is not found - stop:false
* Optimized: compare a reversed string - if not equal - not a palindrome (abaa != aaba)
* Simplest: StringBuilder.reverse
*/
if(input.length() > 1) {
String reversed = "";
StringBuilder builder = new StringBuilder(input);
//input.chars().forEach(x -> reversed + = x + reversed);
// reverse

// compare
criteria = input.compareTo(builder.reverse().toString()) == 0;
}

String result = criteria ? "true" : "false";
System.out.println("Result: " + result);
return criteria;

}




public static void main(String[] args) {
ScratchPad pad = new ScratchPad();
pad.optional();
pad.matrixReductionForAdding();
pad.isPalindrome("abcddcba");

//pad.optional();
//pad.matrixReductionForAdding();
}

}

0 comments on commit a30f6b0

Please sign in to comment.