-
Notifications
You must be signed in to change notification settings - Fork 0
Animal shelter #28
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
MuckT
wants to merge
3
commits into
main
Choose a base branch
from
animal-shelter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Animal shelter #28
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Challenge Summary | ||
|
||
## AnimalShelter | ||
- Create a class called AnimalShelter which holds only dogs and cats. | ||
- The shelter operates using a first-in, first-out approach. | ||
- | ||
|
||
### Required Methods | ||
#### `enqueue` | ||
|
||
- Arguments: animal | ||
- animal can be either a dog or a cat object | ||
|
||
#### `dequeue` | ||
|
||
- Arguments: pref | ||
- pref can be either "dog" or "cat" | ||
- Return: either a dog or a cat, based on preference. | ||
- If pref is not "dog" or "cat" then return null. | ||
|
||
## Approach & Efficiency | ||
|
||
Use an object for storage | ||
|
||
| Method | Time Complexity | Space Complexity | | ||
| ---- | ---- | ---- | | ||
| enqueue | O(1) | O(1)| | ||
| dequeue | O(n) | O(1) | | ||
|
||
|
||
```Bash | ||
# run all related tests | ||
npm test animal-shelter.test.js | ||
``` | ||
|
||
### Sources | ||
|
||
[ Implement a Queue in JavaScript ](https://initjs.org/implement-a-queue-in-javascript-de306a8821c) |
43 changes: 43 additions & 0 deletions
43
javascript/code-challenges/animal-shelter/animal-shelter.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
class AnimalShelter{ | ||
constructor(storage = {}, front = 0, back = 0) { | ||
this.storage = storage | ||
this.front = front | ||
this.back = back | ||
} | ||
|
||
// enqueue | ||
enqueue(animalObject) { | ||
this.back++ | ||
this.storage[this.back] = animalObject | ||
} | ||
|
||
// dequeue | ||
dequeue(pref = null) { | ||
if(this.back === 0){throw new Error('Shelter is empty')} | ||
let result | ||
if(pref === null) { | ||
result = this.storage[this.front] | ||
delete this.storage[this.front] | ||
this.front++ | ||
return result | ||
} | ||
let tempCounter = +this.front | ||
while(this.storage[`${tempCounter}`].type !== pref && tempCounter < this.back) { | ||
let gapCounter = 1 | ||
while(this.storage[`${tempCounter + gapCounter}`] === undefined && gapCounter < this.back) { | ||
gapCounter++ | ||
} | ||
tempCounter+= gapCounter | ||
} | ||
result = this.storage[`${tempCounter}`] | ||
delete this.storage[tempCounter] | ||
if(tempCounter === this.front) { | ||
this.front++ | ||
} | ||
return result | ||
} | ||
} | ||
|
||
module.exports = AnimalShelter |
72 changes: 72 additions & 0 deletions
72
javascript/code-challenges/animal-shelter/animal-shelter.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict' | ||
|
||
const AnimalShelter = require('./animal-shelter') | ||
|
||
let populatedAnimalShelter, emptyAnimalShelter | ||
|
||
beforeEach(() => { | ||
populatedAnimalShelter = new AnimalShelter({ | ||
2: { | ||
type: 'dog', | ||
name: 'Fluff' | ||
}, | ||
4: { | ||
type: 'cat', | ||
name: 'Mittens' | ||
}, | ||
5: { | ||
type: 'dog', | ||
name: 'Boscow' | ||
} | ||
}, 2, 5) | ||
emptyAnimalShelter = new AnimalShelter() | ||
}) | ||
|
||
describe('Given Animal Shelter', () => { | ||
describe('When enqueue', () => { | ||
let animalToAdd = { | ||
type: 'cat', | ||
name: 'Smittens' | ||
} | ||
it('Then should be able to add an animal populated shelter', () => { | ||
populatedAnimalShelter.enqueue(animalToAdd) | ||
expect(populatedAnimalShelter.storage).toStrictEqual({ | ||
'2': { type: 'dog', name: 'Fluff' }, | ||
'4': { type: 'cat', name: 'Mittens' }, | ||
'5': { type: 'dog', name: 'Boscow' }, | ||
'6': { type: 'cat', name: 'Smittens' } | ||
}) | ||
}) | ||
|
||
it('Then should be able to add animal to empty shelter', () => { | ||
emptyAnimalShelter.enqueue(animalToAdd) | ||
expect(emptyAnimalShelter.storage).toStrictEqual({ | ||
'1': { type: 'cat', name: 'Smittens' } | ||
}) | ||
}) | ||
}) | ||
|
||
describe('When dequeue', () => { | ||
it('Then should be able to dequeue animal based on preference', () => { | ||
populatedAnimalShelter.dequeue('cat') | ||
expect(populatedAnimalShelter.storage).toStrictEqual({ | ||
'2': { type: 'dog', name: 'Fluff' }, | ||
'5': { type: 'dog', name: 'Boscow' }, | ||
}) | ||
}) | ||
|
||
it('Then should dequeue animal there the longest if no preference provided', () =>{ | ||
populatedAnimalShelter.dequeue() | ||
expect(populatedAnimalShelter.storage).toStrictEqual( | ||
{ | ||
'4': { type: 'cat', name: 'Mittens' }, | ||
'5': { type: 'dog', name: 'Boscow' } | ||
} | ||
) | ||
}) | ||
|
||
it('Then should throw error on empty shelter', () => { | ||
expect(() => {emptyAnimalShelter.dequeue()}).toThrow('Shelter is empty') | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should return null if pref in null instead of next in queue.