diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ba70b3b..7435d37 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,9 +5,9 @@ - - - + + + - + - + + - @@ -253,6 +253,11 @@ 10 diff --git a/src/main/java/datastructures/Event.java b/src/main/java/datastructures/Event.java new file mode 100644 index 0000000..bec08ea --- /dev/null +++ b/src/main/java/datastructures/Event.java @@ -0,0 +1,38 @@ +package datastructures; + +public class Event { + + int timestamp; + String category; + String displayTest; + + public Event(int timestamp, String category, String displayTest) { + this.timestamp = timestamp; + this.category = category; + this.displayTest = displayTest; + } + + public int getTimestamp() { + return timestamp; + } + + public void setTimestamp(int timestamp) { + this.timestamp = timestamp; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getDisplayTest() { + return displayTest; + } + + public void setDisplayTest(String displayTest) { + this.displayTest = displayTest; + } +} diff --git a/src/main/java/googleOA/socialMedia/SocialMedia.java b/src/main/java/googleOA/socialMedia/SocialMedia.java new file mode 100644 index 0000000..51bee18 --- /dev/null +++ b/src/main/java/googleOA/socialMedia/SocialMedia.java @@ -0,0 +1,42 @@ +package googleOA.socialMedia; + +import datastructures.Event; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class SocialMedia { + + Map categoryLimit = + Map.of("USER_FOLLOW", 1, "TRENDING_TOPIC", 2, "DIRECT_MESSAGE", 5); + public List collapseEvents(List events) { + List res = new ArrayList<>(); + String previousCategory = null; + int previousCategoryCount = 0; + + for (Event event : events) { + if (event.getCategory().equals(previousCategory)) + previousCategoryCount++; + else { + if (previousCategory != null) { + if (categoryLimit.containsKey(previousCategory)) { + if (previousCategoryCount > categoryLimit.get(previousCategory)) { + String val = previousCategory + "(+" + String.valueOf(previousCategoryCount - 1) + " more)"; + res.add(val); + } else { + for (int i = 0; i < previousCategoryCount; i++) { + res.add(previousCategory); + } + } + } + } + previousCategoryCount = 1; + } + previousCategory = event.getCategory(); + if (previousCategoryCount == 0) previousCategoryCount++; + } + + return res; + } +} diff --git a/src/test/java/googleOA/socialMedia/SocialMediaTest.java b/src/test/java/googleOA/socialMedia/SocialMediaTest.java new file mode 100644 index 0000000..7d939eb --- /dev/null +++ b/src/test/java/googleOA/socialMedia/SocialMediaTest.java @@ -0,0 +1,57 @@ +package googleOA.socialMedia; + +import datastructures.Event; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class SocialMediaTest { + + private SocialMedia socialMedia; + + private List testcase = new ArrayList<>(); + + @BeforeEach + void setUp() { + socialMedia = new SocialMedia(); + Event first = new Event(1, "USER_FOLLOW", ""); + Event sec = new Event(1, "USER_FOLLOW", ""); + Event third = new Event(1, "TRENDING_TOPIC", ""); + Event fourth = new Event(1, "TRENDING_TOPIC", ""); + Event fifth = new Event(1, "TRENDING_TOPIC", ""); + Event sixth = new Event(1, "TRENDING_TOPIC", ""); + Event seventh = new Event(1, "TRENDING_TOPIC", ""); + Event eighth = new Event(1, "TRENDING_TOPIC", ""); + Event nineth = new Event(1, "TRENDING_TOPIC", ""); + Event tenth = new Event(1, "DIRECT_MESSAGE", ""); + Event eleventh = new Event(1, "DIRECT_MESSAGE", ""); + Event twelveth = new Event(1, "DIRECT_MESSAGE", ""); + Event thirteenth = new Event(1, "DIRECT_MESSAGE", ""); + Event fourteenth = new Event(1, "DIRECT_MESSAGE", ""); + Event fifteenth = new Event(1, "DIRECT_MESSAGE", ""); + Event sixteenth = new Event(1, "TRENDING_TOPIC", ""); + + testcase = List.of(first, sec, third, fourth, fifth, sixth, seventh, eighth, + nineth, tenth, eleventh, twelveth, thirteenth, fourteenth, fifteenth, sixteenth); + } + + @Test + void collapseEvents() { + List expectedResult = List.of( + "USER_FOLLOW", + "USER_FOLLOW", + "TRENDING_TOPIC (+6 more)", + "DIRECT_MESSAGE (+5 more)", + "TRENDING_TOPIC" + ); + + List actualResult = socialMedia.collapseEvents(testcase); + for (int i = 0; i < actualResult.size(); i++) { + assertEquals(expectedResult.get(i), actualResult.get(i)); + } + } +} \ No newline at end of file