Collection of coding kata, one beginning, more to follow...
mvn test
Find longest peak chain in array of integers and give count of elements. Peak chain stops, if following is equal or has a valley (goes down and up).
Working example would be: 0, 1, 2, 3, 3, 4, 0, 6, 10, 8, 5, 2, 3, 4
10 | *
|
8 | *
|
6 | *
| *
4 | * *
| * * *
2 | * *
| *
*--+---+---+---+---+----*---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12 13
TV series was boring, so I painted it for you to see it clearly. Chain starts at x=0 but stops at x=3, because follower is equal (y=3, y=3). So it restarts and ends on x=6. Newly restarts x=6, goes up top peak at x=8 (enjoys the view), slides all the way down to x=11. Then it stops, because follower goes up again. x=11 is a valley - inverted peak.
So we climb from x=6 to peak and slide down x=11, that counts 6 elements, would be longest peak. Code of working example should result count 6.
💡 Link to code example using index
💡 Link to code example using spaceship strategy
💊 Link to test Both are working...
Generate fibonacci list of integers on given maximum of mathematical operations.
List begins mathematically correct with 0, e.g.
0, 1, 1, 2, 3, 5, 8, 13, 21
appears on 8 operations.
💡 Link to code example using a loop
💡 Link to code example using stream
Compress nucleotides (A, C, G, T), being able to decompress this again.
The compress method transforms the input nucleotide string "ACGT" into an array of Byte values [0, 1, 2, 3], where each byte represents a nucleotide according to the Nucleotide enum mapping.
-
Input String:
nucleoideString = "ACGT";
-
Convert to IntStream of Characters:
nucleoideString.chars();
This converts the string
"ACGT"into anIntStreamwhere each integer represents the Unicode code point of the characters."ACGT" -> [65, 67, 71, 84]🔸Stream at this stage:
[65, 67, 71, 84] // Unicode code points for 'A', 'C', 'G', 'T' -
Map IntStream to Byte Stream using Nucleotide Enum:
.mapToObj(nucleotideChar -> Nucleotide.tryFromChar((char) nucleotideChar).value)
This converts each Unicode code point to its corresponding
Nucleotideenum value and then to the byte value. Object Wrapping: Since primitive streams (IntStream, LongStream, DoubleStream) cannot directly handle object types, .mapToObj is necessary to transform the primitive int values into Byte objects.- Converting the integer character code to a
char. - Using the
Nucleotide.tryFromCharmethod to get the correspondingNucleotideenum value. - Calling
getValue()on theNucleotideenum to get the corresponding byte value. - Wrapping the primitive byte value in a
Byteobject.
That is what happens for each character:
'A'(65) ->Nucleotide.A->0b00(0)'C'(67) ->Nucleotide.C->0b01(1)'G'(71) ->Nucleotide.G->0b10(2)'T'(84) ->Nucleotide.T->0b11(3)
🔸Stream at this stage:
[(byte) 0b00, (byte) 0b01, (byte) 0b10, (byte) 0b11] // Corresponding byte values of the nucleotides - Converting the integer character code to a
-
Collect into an Array of Bytes:
.toArray(Byte[]::new);
This collects the stream of bytes into an array of
Byteobjects.Final output:
new Byte[] { (byte) 0b00, (byte) 0b01, (byte) 0b10, (byte) 0b11 } // Byte array
The inflate method transforms the input Byte array
new Byte[] { (byte) 0b00, (byte) 0b01, (byte) 0b10, (byte) 0b11 } back into the nucleotide string "ACGT",
where each byte is converted to its corresponding nucleotide character.
-
Input Byte Array:
Byte[] compressedBytes = new Byte[] { (byte) 0b00, (byte) 0b01, (byte) 0b10, (byte) 0b11 };
-
Convert to Stream of Bytes:
Arrays.stream(compressedBytes);
This converts the array
{ (byte) 0b00, (byte) 0b01, (byte) 0b10, (byte) 0b11 }into aStream<Byte>.🔸Stream at this stage:
[(byte) 0b00, (byte) 0b01, (byte) 0b10, (byte) 0b11] -
Map Byte Stream to Nucleotide Characters:
.map(valueByte -> Nucleotide.tryFromValue(valueByte).toString())
This converts each byte value back to its corresponding
Nucleotideenum value and then to the string representation of the nucleotide, theString enum:name.That is what happens for each byte:
(byte) 0b00->Nucleotide.A->"A"(byte) 0b01->Nucleotide.C->"C"(byte) 0b10->Nucleotide.G->"G"(byte) 0b11->Nucleotide.T->"T"
🔸Stream at this stage:
["A", "C", "G", "T"] -
Collect to a Single String:
.collect(Collectors.joining());
This joins all the strings in the stream into a single string.
Final output:
"ACGT"