-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: sarah-eventstream-puzzler
Are you sure you want to change the base?
Changes from all commits
47f54f5
11f5582
6539f33
a1293a4
b42fe60
6be0000
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', () => { | ||
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); | ||
}) | ||
}) | ||
}) |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||
export interface event { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interfaces and types should be capialized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
timestamp: number, | ||||||
eventType: string | ||||||
} | ||||||
|
||||||
export interface eventWithScore extends event { | ||||||
score: number | ||||||
} | ||||||
|
||||||
export interface scored_region { | ||||||
events: ReadonlyArray<event>, | ||||||
score: number | ||||||
} |
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", | ||
} | ||
] |
There was a problem hiding this comment.
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 <