Skip to content
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

Solve Event puzzler with tests #42

Open
wants to merge 7 commits into
base: andy-event-main
Choose a base branch
from
Open

Conversation

Plymatea
Copy link

Ticket:

  • None

What this Does:

  • Solves the event puzzler
  • Creates tests for solution
  • Adds new file for mock testing data

Media:
image

Testing

  • None

@Plymatea Plymatea requested a review from DuHerb June 14, 2022 17:52
@Plymatea Plymatea marked this pull request as ready for review June 14, 2022 17:52
timestamp: 123123125,
eventType: 'view'
}
]
Copy link

Choose a reason for hiding this comment

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

new line.

Also, indentation is all over the place in this file, and between all files. Tab should be the same in all files. In general, I would go with 2.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not totally sure why the indentation is all over the place. My standard is 2 spaces for a tab. I did find three spot on my local branch that I need to tab in. But my local branch is not nearly as screwy as this.... I'll pay more attention after I push it up this next time.

src/index.ts Outdated
if(stream.length <= 5 ) { return stream }

const newStream: Array<IEvent> = [...stream]
Copy link

Choose a reason for hiding this comment

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

shouldn't need a newStream or spread, as you are only going to iterate over the input stream.

Copy link
Author

Choose a reason for hiding this comment

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

done

src/index.ts Outdated
const newStream: Array<IEvent> = [...stream]
const newStreamValues: Array<number>= []
stream.forEach( e => newStreamValues.push(assignPointValue(e.eventType)));
Copy link

Choose a reason for hiding this comment

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

can you refactor this to use map or reduce

Copy link
Author

Choose a reason for hiding this comment

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

done

src/index.ts Outdated
let index = 4 //Start with the 5th event in the stream becuase the for loop looks backward and ends at the end of the array
let highestValue = 0
for(let i=4; i<newStreamValues.length; i++) { // Same here, Want to start at the 5th event in the array
Copy link

Choose a reason for hiding this comment

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

i'm not sure I understand why we need to look backwards over results.

In general, I'm finding the algorithm difficult to understand. One way to mitigate readability/maintainability, is to further encapsulate logic into smaller functions.

You may find it useful to build a helper function that takes an array of events and just provides a score. Or similar such function.

Copy link
Author

Choose a reason for hiding this comment

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

I encapsulated finding the index into a helper function.

I managed to get rid of the nested for loop, but not really happy with the hard coding in finding the sum of the values. Is there a better way to sum the values of a sub array?

@DuHerb
Copy link

DuHerb commented Jun 16, 2022

In general, I like that you've encapsulated different logic in to smaller, more maintainable code.

however, the fact that we are still looking backwards into arrays smells fishy to me.

can you think of a solution where your function can identify/record the index of the first event in the high scoring region?

src/index.ts Outdated
value?: number
}
export type eventTypeState = 'newMessage' | 'screenShot' | 'view'
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 type eventTypeState = 'newMessage' | 'screenShot' | 'view'
export type EventType = 'newMessage' | 'screenShot' | 'view'

types/interfaces should be capitalized

Copy link
Author

Choose a reason for hiding this comment

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

Done

src/index.ts Outdated
for (let i = 4; i < stream.length; i++) {
const currentSum =
stream[i].value! +
Copy link

Choose a reason for hiding this comment

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

adding the value prop and then looking it up feels like an unnecessary step. Can just get the value directly here via assignPointValue(stream[i].eventType)

src/index.ts Outdated
readonly timestamp: number,
readonly eventType: eventTypeState,
value?: number
Copy link

Choose a reason for hiding this comment

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

extra step. see comment in findLastIndex....

src/index.ts Outdated
let maxCumulativeSum = 0
for (let i = 4; i < stream.length; i++) {
const currentSum =
Copy link

Choose a reason for hiding this comment

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

getting the scores for an array of events can be delegated to a helper function.

The current solution, while working for this exact use case, is not very maintainable. For instance, what if the requirement for the function changed so that the length of the scored array was changed to 7. Now you have to add two more lines here, and adjust hardcoded values all over the place.

In another scenario, what if the the requirement was that the region length was completely variable? In that case, this solution won't work at all.

Encapsulation helps handle those types of solutions.

@Plymatea
Copy link
Author

In general, I like that you've encapsulated different logic in to smaller, more maintainable code.

however, the fact that we are still looking backwards into arrays smells fishy to me.

can you think of a solution where your function can identify/record the index of the first event in the high scoring region?

Instead of assigning the value to each object, I decided to assign the region value to each object. Completed that and realized it was just easier to create an array of the region values. So I refactored it again to it's current state. Create an array of values corresponding to the regions. Then find the max value, and index, then return the region from the original stream.

This seems a lot nicer than my first approach. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants