Skip to content

EventStream Puzzler #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: sarah-eventstream-puzzler
Choose a base branch
from
Open
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
35 changes: 30 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
//write tests here
import { eventStream } from './test_data';
import { scoreEventStream } from '.';

describe('Tests will go here!', () => {
it('should pass', () => {
expect(true).toBeTruthy()
describe('scoreEventStream', () => {
it('should return an object with an events property and a score property', () => {
const actual = scoreEventStream(eventStream);

expect(actual).toHaveProperty("events");
expect(actual).toHaveProperty("score");
})

it('length of events array in return object should be no greater than 5', () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the function now accepts the regionLength argument, then we need additional tests to verify expected array lengths. Default vs > vs <

const actual = scoreEventStream(eventStream);

expect(actual.events.length).toBeLessThanOrEqual(5);
})

it('should return an object whos events property is the expected test region', () => {
const actual = scoreEventStream(eventStream.slice(0,4));
const expected = eventStream.slice(0,4)

expect(actual.events).toEqual(expected);
})

it('should return an object with the cumulative score of the region as the score property', () => {
const expected = 7;

const actual = scoreEventStream(eventStream.slice(0,5));

expect(actual.score).toEqual(expected);
})
})
})
73 changes: 72 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
//Define class/functions here
import { event, eventWithScore, scored_region } from './models'

function addScoresToEvents(eventStream: ReadonlyArray<event>) {
const eventsWithScores = eventStream.map(event => {
if (event.eventType === "newMessage") {
return {
...event,
score: 1
}
} else if (event.eventType === "view") {
return {
...event,
score: 2
}
} else {
return {
...event,
score: 3
}
}
})
return eventsWithScores;
}

function findHighestSCore(scores: ReadonlyArray<number>) {
let highest_score = 0;
for (let i = 1; i <= scores.length; i++) {
if (scores[i] > scores[i - 1]) {
highest_score = scores[i]
} else {

}
}
return highest_score;
}

function separateSubRegions(eventStreamWithScores: ReadonlyArray<eventWithScore>, subRegionLength: number) {
const container: scored_region[] = [];
for (let i = 0; i <= eventStreamWithScores.length - subRegionLength; i++) {
const events_arr = eventStreamWithScores.slice(i, i + subRegionLength);
const event = {
events: events_arr,
score: Object.values(events_arr).map(e => { return e.score }).reduce((a, b) => { return a + b })
}
container.push(event);
}
return container;
}

function createScoresArray(eventStreamWithScores: ReadonlyArray<scored_region> | ReadonlyArray<eventWithScore>) {
return eventStreamWithScores.map(e => { return e.score })
}

export function scoreEventStream(eventStream: ReadonlyArray<event>, subRegionLength: number = 5) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const DEFAULT_SUBREGION_LENGTH = 5

if (eventStream.length <= subRegionLength) {
const eventsWithScores = addScoresToEvents(eventStream);
console.log(eventsWithScores);
const score = Object.values(eventsWithScores).map(e => { return e.score }).reduce((a, b) => { return a + b })
return {
events: eventStream,
score: score
}
} else {
const eventsWithScores = addScoresToEvents(eventStream);
const container = separateSubRegions(eventsWithScores, subRegionLength);
const scores = createScoresArray(container);
const highest_score = findHighestSCore(scores);
const subRegionWithHighestScore = container.filter(e => e.score === highest_score)[0];
return subRegionWithHighestScore;
}
}

13 changes: 13 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface event {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export interface event {
export interface Event {

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interfaces and types should be capialized.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type EventStream = ReadonlyArray<Event>;

timestamp: number,
eventType: string
}

export interface eventWithScore extends event {
score: number
}

export interface scored_region {
events: ReadonlyArray<event>,
score: number
}
39 changes: 39 additions & 0 deletions src/test_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const eventStream =
[
{
timestamp: 123123123,
eventType: "newMessage"
},
{
timestamp: 123123124,
eventType: "newMessage"
},
{
timestamp: 123123125,
eventType: "newMessage"
},
{
timestamp: 123123125,
eventType: "view",
},
{
timestamp: 123123125,
eventType: "view",
},
{
timestamp: 123123125,
eventType: "screenshot",
},
{
timestamp: 123123125,
eventType: "screenshot",
},
{
timestamp: 123123125,
eventType: "newMessage",
},
{
timestamp: 123123125,
eventType: "newMessage",
}
]