From d1152144aa5d777f22666c91b44c1588cc7a0d01 Mon Sep 17 00:00:00 2001 From: Akshay Dubey <38462415+itsakshaydubey@users.noreply.github.com> Date: 2022年9月15日 12:20:58 +0530 Subject: [PATCH 1/2] algorithm: Liouville function (#1100) --- Maths/LiouvilleFunction.js | 25 +++++++++++++++++++++++++ Maths/test/LiouvilleFunction.test.js | 19 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 Maths/LiouvilleFunction.js create mode 100644 Maths/test/LiouvilleFunction.test.js diff --git a/Maths/LiouvilleFunction.js b/Maths/LiouvilleFunction.js new file mode 100644 index 0000000000..f13916d55d --- /dev/null +++ b/Maths/LiouvilleFunction.js @@ -0,0 +1,25 @@ +/* + * Author: Akshay Dubey (https://github.com/itsAkshayDubey) + * Liouville Function: https://en.wikipedia.org/wiki/Liouville_function + * For any positive integer n, define λ(n) as the sum of the primitive nth roots of unity. + * It has values in {−1, 1} depending on the factorization of n into prime factors: + * λ(n) = +1 if n positive integer with an even number of prime factors. + * λ(n) = −1 if n positive integer with an odd number of prime factors. + */ + +/** + * @function liouvilleFunction + * @description -> This method returns λ(n) of given number n + * returns 1 when number has even number of prime factors + * returns -1 when number has odd number of prime factors + * @param {Integer} number + * @returns {Integer} 1|-1 + */ + +import { PrimeFactors } from './PrimeFactors.js' +export const liouvilleFunction = (number) => { + if (number <= 0) { + throw new Error('Number must be greater than zero.') + } + return PrimeFactors(number).length % 2 === 0 ? 1 : -1 +} diff --git a/Maths/test/LiouvilleFunction.test.js b/Maths/test/LiouvilleFunction.test.js new file mode 100644 index 0000000000..58a79c9cc2 --- /dev/null +++ b/Maths/test/LiouvilleFunction.test.js @@ -0,0 +1,19 @@ +import { liouvilleFunction } from '../LiouvilleFunction' + +const expectedValuesArray = [1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1] + +describe('Testing liouville function', () => { + for (let i = 1; i <= 100; i++) { + it('Testing for number = ' + i + ', should return ' + expectedValuesArray[i], () => { + expect(liouvilleFunction(i)).toBe(expectedValuesArray[i - 1]) + }) + } + + it('should throw error when supplied negative numbers', () => { + expect(() => { liouvilleFunction(-1) }).toThrow(Error) + }) + + it('should throw error when supplied zero', () => { + expect(() => { liouvilleFunction(0) }).toThrow(Error) + }) +}) From cf0593f4307749ae2c790f500f13f30aa91f23bc Mon Sep 17 00:00:00 2001 From: Kartik Kapgate <99239411+10kartik@users.noreply.github.com> Date: 2022年9月15日 12:22:44 +0530 Subject: [PATCH 2/2] Refactor Cycledetection.js and added it's test. (#1099) --- Data-Structures/Linked-List/CycleDetection.js | 24 +++++--------- .../Linked-List/test/CycleDetection.test.js | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 Data-Structures/Linked-List/test/CycleDetection.test.js diff --git a/Data-Structures/Linked-List/CycleDetection.js b/Data-Structures/Linked-List/CycleDetection.js index ba33cd0c4a..63b730eca1 100644 --- a/Data-Structures/Linked-List/CycleDetection.js +++ b/Data-Structures/Linked-List/CycleDetection.js @@ -1,32 +1,24 @@ /** - * A LinkedList based solution for Detect a Cycle in a list + * A LinkedList based solution for Detecting a Cycle in a list. * https://en.wikipedia.org/wiki/Cycle_detection */ -function main () { +function detectCycle (head) { /* Problem Statement: Given head, the head of a linked list, determine if the linked list has a cycle in it. - - Note: - * While Solving the problem in given link below, don't use main() function. - * Just use only the code inside main() function. - * The purpose of using main() function here is to avoid global variables. - Link for the Problem: https://leetcode.com/problems/linked-list-cycle/ */ - const head = '' // Reference to head is given in the problem. So please ignore this line - let fast = head - let slow = head + if (!head) { return false } - while (fast != null && fast.next != null && slow != null) { + let slow = head + let fast = head.next + while (fast && fast.next) { + if (fast === slow) { return true } fast = fast.next.next slow = slow.next - if (fast === slow) { - return true - } } return false } -main() +export { detectCycle } diff --git a/Data-Structures/Linked-List/test/CycleDetection.test.js b/Data-Structures/Linked-List/test/CycleDetection.test.js new file mode 100644 index 0000000000..0041dece59 --- /dev/null +++ b/Data-Structures/Linked-List/test/CycleDetection.test.js @@ -0,0 +1,31 @@ +import { detectCycle } from '../CycleDetection' +import { Node } from '../SinglyLinkedList' + +describe('Detect Cycle', () => { + it('should detect loop and return true', () => { + // Creating list and making a loop + const headNode = new Node(10) + headNode.next = new Node(20) + headNode.next.next = new Node(30) + headNode.next.next.next = new Node(40) + headNode.next.next.next.next = headNode + expect(detectCycle(headNode)).toEqual(true) + }) + + it('should not detect a loop and return false', () => { + // Case 0: When head is null, there is no loop. + expect(detectCycle(null)).toEqual(false) + const headNode = new Node(10) + + // Case 1: List with single node doesn't have any loop + expect(detectCycle(headNode)).toEqual(false) + + headNode.next = new Node(20) + headNode.next.next = new Node(30) + headNode.next.next.next = new Node(40) + headNode.next.next.next.next = new Node(50) + + // Case 2: List not having any loops + expect(detectCycle(headNode)).toEqual(false) + }) +})

AltStyle によって変換されたページ (->オリジナル) /