-
Notifications
You must be signed in to change notification settings - Fork 12
Callenge Solved With Deque #2
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: master
Are you sure you want to change the base?
Changes from 1 commit
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,10 +1,29 @@ | ||
| const { Deque } = require ('./deque.js'); | ||
|
|
||
| function palindromeChecker(text) { | ||
| if (!text) { | ||
| return false | ||
| } | ||
|
|
||
| // your code | ||
| const re = /[^A-Za-z0-9]/g; | ||
| text = text.toLowerCase().replace(re, '') | ||
|
|
||
| return; | ||
| let Deq = new Deque() | ||
| for (var i = 0; i < text.length; i++) | ||
| { | ||
| Deq.addFront(text.charAt(i)) | ||
|
||
| } | ||
|
|
||
| for(var i = 1; i <= Deq.size(); i++) { | ||
| if (Deq.peekBack() !== Deq.peekFront()) { | ||
| return false | ||
| } else { | ||
| Deq.removeBack() | ||
| Deq.removeFront() | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| module.exports = { palindromeChecker }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| class Deque { | ||
|
|
||
| constructor() { | ||
| this.count = 0; | ||
| this.topCount = 0; | ||
| this.items = {}; | ||
| } | ||
|
|
||
| addFront(element) { | ||
| if (this.isEmpty()) { | ||
| this.addBack(element); | ||
| } else if (this.topCount > 0) { | ||
| this.topCount--; | ||
| this.items[this.topCount] = element; | ||
| } else { | ||
| for (let i = this.count; i > 0; i--) { | ||
| this.items[i] = this.items[i - 1]; | ||
| } | ||
| this.count++; | ||
| this.topCount = 0; | ||
| this.items[0] = element; | ||
| } | ||
| } | ||
|
|
||
| addBack(element) { | ||
| this.items[this.count] = element; | ||
| this.count++; | ||
| } | ||
|
|
||
| removeFront() { | ||
| if (this.isEmpty()) { | ||
| return undefined; | ||
| } | ||
| const rta = this.items[this.topCount]; | ||
| delete this.items[this.topCount]; | ||
| this.topCount++; | ||
| return rta; | ||
| } | ||
|
|
||
| removeBack() { | ||
| if (this.isEmpty()) { | ||
| return undefined; | ||
| } | ||
| this.count--; | ||
| const result = this.items[this.count]; | ||
| delete this.items[this.count]; | ||
| return result; | ||
| } | ||
|
|
||
| size() { | ||
| return this.count - this.topCount; | ||
| } | ||
|
|
||
| isEmpty() { | ||
| return this.size() === 0; | ||
| } | ||
|
|
||
| getItems() { | ||
| return this.items; | ||
| } | ||
|
|
||
| peekFront() { | ||
| if (this.isEmpty()) { | ||
| return undefined; | ||
| } | ||
| return this.items[this.topCount]; | ||
| } | ||
|
|
||
| peekBack() { | ||
| if (this.isEmpty()) { | ||
| return undefined; | ||
| } | ||
| return this.items[this.count - 1]; | ||
| } | ||
|
|
||
| toString() { | ||
| if (this.isEmpty()) { | ||
| return ''; | ||
| } | ||
| let result = ''; | ||
| for (let index = this.topCount; index < this.count; index++) { | ||
| result+= `${this.items[index]},`; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| clear() { | ||
| this.count = 0; | ||
| this.topCount = 0; | ||
| this.items = {}; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| module.exports = { Deque }; |
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.
usa nombres de variables más descriptivas, recuerda que js usa el patrón camel case
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.
usa
const