Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ are not the only possible solutions to these cases.
8. [Wolf 🐺 & Sheep 🐑](src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java)
9. [Player Ranking System 🏆](src/test/java/pl/mperor/interview/tasks/ranking/PlayerRankingSystemTest.java)
10. [Simple Linked List 🔗](src/test/java/pl/mperor/interview/tasks/list/SimpleLinkedListTest.java)
11. [Wega Signals 📡](src/test/java/pl/mperor/interview/tasks/signal/WegaSignalTest.java)

## Sources 🔗

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/pl/mperor/interview/tasks/signal/SignalAnalyzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pl.mperor.interview.tasks.signal;

import java.util.Comparator;
import java.util.List;
import java.util.stream.IntStream;

public class SignalAnalyzer {

private final List<String> signals;

public SignalAnalyzer(List<String> signals) {
this.signals = List.copyOf(signals);
}

public String readSecretMessage() {
return IntStream.iterate(39, index -> index < signals.size(), index -> index + 40)
.mapToObj(signals::get)
.map(signal -> String.valueOf(signal.charAt(9)))
.reduce("", String::concat);
}

public String findSignalWithMostDistinctLetters() {
return signals.stream()
.max(Comparator.comparingLong(SignalAnalyzer::countDistinctLetters))
.orElseThrow();
}

public static long countDistinctLetters(String signal) {
return signal.chars().distinct().count();
}

public List<String> findSignalsWithLettersDistanceAtMost(int maxDistance) {
return signals.stream()
.filter(signal -> lettersDistance(signal) <= maxDistance)
.toList();
}

private static int lettersDistance(String signal) {
var statistics = signal.chars().summaryStatistics();
return statistics.getMax() - statistics.getMin();
}
}
84 changes: 84 additions & 0 deletions src/test/java/pl/mperor/interview/tasks/signal/WegaSignalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package pl.mperor.interview.tasks.signal;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Wega Signals 📡
*
* <p>Scientists saved signals coming from space to a file. There are 1000 lines in the file.
* Each line contains one non-empty word consisting of uppercase English letters.
* The length of one word does not exceed 100 characters.
*
* <p>Write a program which answers the following questions:
* <ol>
* <li>After joining the tenth letters of every fortieth word (starting with the fortieth word),
* we will receive a secret message. Write this message.</li>
* <li>Find the word that has the largest number of different letters.
* Write out the word and the number of its different letters.
* If there is more than one such word, write the first of them.</li>
* <li>Consider the distance of letters in the alphabet, e.g. the distance between A and B is equal to 1.
* Write all words where the distance between each two letters is at most 10.</li>
* </ol>
*/
public class WegaSignalTest {

private SignalAnalyzer analyzer;

@BeforeEach
public void setUp() throws URISyntaxException, IOException {
Path examples = Path.of(getClass().getClassLoader().getResource("wega/examples.txt").toURI());
analyzer = new SignalAnalyzer(Files.readAllLines(examples));
}

@Test
public void testReadSecretMessage() {
assertEquals("NIECHCIMATURAPROSTABEDZIE", analyzer.readSecretMessage());
}

@Test
public void testFindSignalWithMostDistinctLetters() {
String signal = analyzer.findSignalWithMostDistinctLetters();

assertEquals("AKLMNOPRSTWZA", signal);
assertEquals(12, SignalAnalyzer.countDistinctLetters(signal));
}

@Test
public void testFirstSignalWinsWhenDistinctLettersAreEqual() {
var tiedAnalyzer = new SignalAnalyzer(List.of("AB", "ABC", "CBA", "AAB"));

assertEquals("ABC", tiedAnalyzer.findSignalWithMostDistinctLetters());
}

@Test
public void testFindSignalsWithLettersDistanceAtMost10() {
List<String> expected = List.of(
"AAAAAAAAAI",
"AAAAAAAAAE",
"AAAAAAAAAC",
"AAAAAAAAAH",
"AAAAAAAAAC",
"AAAAAAAAAI",
"AAAAAAAAAA",
"BB",
"AAAAAAAAAA",
"AAAAAAAAAA",
"AAAAAAAAAB",
"AAAAAAAAAE",
"AAAAAAAAAD",
"AAAAAAAAAI",
"AAAAAAAAAE"
);

assertEquals(expected, analyzer.findSignalsWithLettersDistanceAtMost(10));
}
}
Loading
Loading