Skip to content
Merged
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
* [CountVowels](String/CountVowels.js)
* [CreatePermutations](String/CreatePermutations.js)
* [DiceCoefficient](String/DiceCoefficient.js)
* [FirstUniqueCharacter](String/FirstUniqueCharacter.js)
* [FormatPhoneNumber](String/FormatPhoneNumber.js)
* [GenerateGUID](String/GenerateGUID.js)
* [HammingDistance](String/HammingDistance.js)
Expand Down
45 changes: 0 additions & 45 deletions Data-Structures/Linked-List/RotateListRight.js

This file was deleted.

36 changes: 34 additions & 2 deletions Data-Structures/Linked-List/SinglyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* a singly linked list.
*/

// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean, rotateListRight

class Node {
constructor (data) {
Expand All @@ -16,9 +16,15 @@ class Node {
}

class LinkedList {
constructor () {
constructor (listOfValues) {
this.headNode = null
this.length = 0

if (listOfValues instanceof Array) {
for (const value of listOfValues) {
this.addLast(value)
}
}
}

// initiates the currentNode and currentIndex and return as an object
Expand Down Expand Up @@ -224,6 +230,32 @@ class LinkedList {
return list
}

// Method for Rotating a List to the right by k places
rotateListRight (k) {
let i = 0
let current = this.headNode
while (current) {
i++
current = current.next
}
k %= i
current = this.headNode
let prev = null
while (k--) {
if (!current || !current.next) {
return current
} else {
while (current.next) {
prev = current
current = current.next
}
prev.next = current.next
current.next = this.headNode
this.headNode = current
}
}
}

// Method to iterate over the LinkedList
iterator () {
let { currentNode } = this.initiateNodeAndIndex()
Expand Down
25 changes: 25 additions & 0 deletions Data-Structures/Linked-List/test/SinglyLinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,29 @@ describe('SinglyLinkedList', () => {
list.clean()
expect(list.isEmpty()).toBe(true)
})

it('should shift every node by k steps towards right, shifts tail nodes towards the start and change head of the list', () => {
// Case 0: When head of list is null
const tempNode = new LinkedList()
expect(tempNode.get()).toEqual([])

// Creating list
const headNode = new LinkedList([10, 20, 30, 40, 50])

// Case 1: when k = 0 => List should be unaffected
headNode.rotateListRight(0)
expect(headNode.get()).toEqual([10, 20, 30, 40, 50])

// Case 2: Rotate right by 2 steps
headNode.rotateListRight(2)
expect(headNode.get()).toEqual([40, 50, 10, 20, 30])

// Case 3: Rotate right by 12 steps
headNode.rotateListRight(12)
expect(headNode.get()).toEqual([20, 30, 40, 50, 10])

// Case 4: when k = length of the list = 5 => List should be unaffected
headNode.rotateListRight(5)
expect(headNode.get()).toEqual([20, 30, 40, 50, 10])
})
})
23 changes: 23 additions & 0 deletions Maths/IsSquareFree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
* Square free integer: https://en.wikipedia.org/wiki/Square-free_integer
* function to check if an integer has repeated prime factors.
* return false if the number as repeated prime factors.
* else true
*/

/**
* @function isSquareFree
* @description -> Checking if number is square free using prime factorization
* @param {number} number
* @returns {boolean} true if the number has unique prime factors, otherwise false
*/

import { PrimeFactors } from './PrimeFactors.js'
export const isSquareFree = (number) => {
const primeFactorsArray = PrimeFactors(number)
if (number <= 0) {
throw new Error('Number must be greater than zero.')
}
return primeFactorsArray.length === new Set(primeFactorsArray).size
}
20 changes: 20 additions & 0 deletions Maths/test/IsSquareFree.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions String/FirstUniqueCharacter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @function firstUniqChar
* @description Given a string str, find the first non-repeating character in it and return its index. If it does not exist, return -1.
* @param {String} str - The input string
* @return {Number} - The index of first unique character.
* @example firstUniqChar("javascript") => 0
* @example firstUniqChar("sesquipedalian") => 3
* @example firstUniqChar("aabb") => -1
*/

const firstUniqChar = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}
const count = new Map()

for (const char of str) {
if (!count[char]) {
count[char] = 1
} else {
count[char]++
}
}
for (let i = 0; i < str.length; i++) {
if (count[str[i]] === 1) return i
}
return -1
}

export { firstUniqChar }
9 changes: 9 additions & 0 deletions String/test/FirstUniqueCharacter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { firstUniqChar } from '../FirstUniqueCharacter'

describe('firstUniqChar', () => {
it('locates the index of first unique character in the string', () => {
expect(firstUniqChar('javascript')).toEqual(0)
expect(firstUniqChar('sesquipedalian')).toEqual(3)
expect(firstUniqChar('aabb')).toEqual(-1)
})
})
21 changes: 20 additions & 1 deletion Trees/BreadthFirstTreeTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,26 @@ class BinaryTree {
this.traversal = []
}

breadthFirst () {
breadthFirstIterative () {
this.traversal = []
if (this.root) {
this.traversal.push(this.root)
}
for (let i = 0; i < this.traversal.length; i++) {
const currentNode = this.traversal[i]
if (currentNode.left) {
this.traversal.push(currentNode.left)
}
if (currentNode.right) {
this.traversal.push(currentNode.right)
}
this.traversal[i] = currentNode.data
}
return this.traversal
}

breadthFirstRecursive () {
this.traversal = []
const h = this.getHeight(this.root)
for (let i = 0; i !== h; i++) {
this.traverseLevel(this.root, i)
Expand Down
9 changes: 7 additions & 2 deletions Trees/test/BreadthFirstTreeTraversal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ describe('Breadth First Tree Traversal', () => {
// / \ \
// 3 6 9

it('Binary tree - Level order traversal', () => {
it('Binary tree - Level order recursive traversal', () => {
expect(binaryTree.traversal).toStrictEqual([])
const traversal = binaryTree.breadthFirst()
const traversal = binaryTree.breadthFirstRecursive()
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
})

it('Binary tree - Level order iterative traversal', () => {
const traversal = binaryTree.breadthFirstIterative()
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
})
})