From b7309f0018722801232f9de6219666e6ef69fd32 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 20:36:47 +0530 Subject: [PATCH 01/13] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Added=20solution=20?= =?UTF-8?q?for=20ProjectEuler-007?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem007.js | 27 +++++++++++++++++++++++++++ Project-Euler/test/Problem007.test.js | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 Project-Euler/Problem007.js create mode 100644 Project-Euler/test/Problem007.test.js diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js new file mode 100644 index 0000000000..48f27d56c1 --- /dev/null +++ b/Project-Euler/Problem007.js @@ -0,0 +1,27 @@ +import { PrimeCheck } from '../Maths/PrimeCheck.js' + +/** + * Find nth Prime Number + * + * P.S.(Project Euler - 007): + * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. + * What is the 10001st prime number? + * + * @param {Number} n + * @returns {Number} returns the nth prime number + */ +export const nthPrime = (n) => { + if (n < 1) { + return 'Invalid Input' + } + + let count = 0 + let inc = 2 + while (count < n) { + if (PrimeCheck(inc)) { + count++ + } + inc++ + } + return inc - 1 +} diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js new file mode 100644 index 0000000000..e979e45728 --- /dev/null +++ b/Project-Euler/test/Problem007.test.js @@ -0,0 +1,17 @@ +import { nthPrime } from '../Problem007.js' + +describe('checking nth prime number', () => { + it('should be invalid input if number is negative', () => { + expect(nthPrime(-3)).toBe('Invalid Input') + }) + it('should be invalid input if number is 0', () => { + expect(nthPrime(0)).toBe('Invalid Input') + }) + test('if the number is greather than 0', () => { + expect(nthPrime(10)).toBe(29) + }) + // Project Euler Condition Check + test('if the number is 10001', () => { + expect(nthPrime(10001)).toBe(104743) + }) +}) From d3a3b331c7d964262f524c37afb767c87994b6c3 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 20:45:06 +0530 Subject: [PATCH 02/13] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Spelling=20mistake?= =?UTF-8?q?=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem007.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js index e979e45728..6c48f1e786 100644 --- a/Project-Euler/test/Problem007.test.js +++ b/Project-Euler/test/Problem007.test.js @@ -7,7 +7,7 @@ describe('checking nth prime number', () => { it('should be invalid input if number is 0', () => { expect(nthPrime(0)).toBe('Invalid Input') }) - test('if the number is greather than 0', () => { + test('if the number is greater than 0', () => { expect(nthPrime(10)).toBe(29) }) // Project Euler Condition Check From e9b5a6a92174ff87a4d9b27cee8e596ad8fa76a3 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 23:02:44 +0530 Subject: [PATCH 03/13] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20changed=20varia?= =?UTF-8?q?ble=20name=20from=20`inc`=20to=20`candidateValue`=20and=20throw?= =?UTF-8?q?n=20error=20in=20case=20of=20invalid=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem007.js | 10 +++++----- Project-Euler/test/Problem007.test.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js index 48f27d56c1..95e3f10562 100644 --- a/Project-Euler/Problem007.js +++ b/Project-Euler/Problem007.js @@ -12,16 +12,16 @@ import { PrimeCheck } from '../Maths/PrimeCheck.js' */ export const nthPrime = (n) => { if (n < 1) { - return 'Invalid Input' + throw new Error('Invalid Input') } let count = 0 - let inc = 2 + let candidateValue = 2 while (count < n) { - if (PrimeCheck(inc)) { + if (PrimeCheck(candidateValue)) { count++ } - inc++ + candidateValue++ } - return inc - 1 + return candidateValue - 1 } diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js index 6c48f1e786..191d1e06af 100644 --- a/Project-Euler/test/Problem007.test.js +++ b/Project-Euler/test/Problem007.test.js @@ -2,10 +2,10 @@ import { nthPrime } from '../Problem007.js' describe('checking nth prime number', () => { it('should be invalid input if number is negative', () => { - expect(nthPrime(-3)).toBe('Invalid Input') + expect(() => nthPrime(-3)).toThrowError('Invalid Input') }) it('should be invalid input if number is 0', () => { - expect(nthPrime(0)).toBe('Invalid Input') + expect(() => nthPrime(0)).toThrowError('Invalid Input') }) test('if the number is greater than 0', () => { expect(nthPrime(10)).toBe(29) From e99c7225579aa6da265352daa5140c037281c311 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 23:23:09 +0530 Subject: [PATCH 04/13] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Modified=20the?= =?UTF-8?q?=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem007.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js index 95e3f10562..009c1a896e 100644 --- a/Project-Euler/Problem007.js +++ b/Project-Euler/Problem007.js @@ -16,12 +16,12 @@ export const nthPrime = (n) => { } let count = 0 - let candidateValue = 2 + let candidateValue = 1 while (count < n) { + candidateValue++ if (PrimeCheck(candidateValue)) { count++ } - candidateValue++ } - return candidateValue - 1 + return candidateValue } From 0f9f1bab7decbea8a2fbfc06b4b8531a39d0d6e1 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 10 Oct 2022 19:50:16 +0530 Subject: [PATCH 05/13] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20test=20?= =?UTF-8?q?case=20for=20ProjectEuler=20Problem001?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem001.js | 4 +++- Project-Euler/test/Problem001.test.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Project-Euler/test/Problem001.test.js diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index 65a35e32a1..c47c10bd1c 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -5,9 +5,11 @@ Find the sum of all the multiples of 3 or 5 below the provided parameter value n */ const multiplesThreeAndFive = (num) => { + if (num < 1) throw new Error('No natural numbers exist below 1') + let total = 0 // total for calculating the sum - for (let i = 0; i < num; i++) { + for (let i = 1; i < num; i++) { if (i % 3 === 0 || i % 5 === 0) { total += i } diff --git a/Project-Euler/test/Problem001.test.js b/Project-Euler/test/Problem001.test.js new file mode 100644 index 0000000000..e6b5549a57 --- /dev/null +++ b/Project-Euler/test/Problem001.test.js @@ -0,0 +1,17 @@ +import { multiplesThreeAndFive } from '../Problem001.js' + +describe('Sum of multiples of 3 or 5', () => { + it('should throw error when number is negative number', () => { + expect(() => multiplesThreeAndFive(-24)).toThrowError('No natural numbers exist below 1') + }) + it('should throw error when number is 0', () => { + expect(() => multiplesThreeAndFive(0)).toThrowError('No natural numbers exist below 1') + }) + test('if the number is greater than 0', () => { + expect(multiplesThreeAndFive(10)).toBe(23) + }) + // Project Euler Condition Check + test('if the number is 1000', () => { + expect(multiplesThreeAndFive(1000)).toBe(233168) + }) +}) From 53e3938b0a29f0ee101b06c559d4f358cd7a0cd6 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 23 Oct 2023 00:32:56 +0530 Subject: [PATCH 06/13] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20test=20?= =?UTF-8?q?cases=20for=20Project=20Euler=20Problem=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem004.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Project-Euler/test/Problem004.test.js diff --git a/Project-Euler/test/Problem004.test.js b/Project-Euler/test/Problem004.test.js new file mode 100644 index 0000000000..cd6a55ac98 --- /dev/null +++ b/Project-Euler/test/Problem004.test.js @@ -0,0 +1,11 @@ +import { largestPalindromic } from '../Problem004.js' + +describe('Largest Palindromic Number', () => { + test('if digit is 2', () => { + expect(largestPalindromic(2)).toBe(9009) + }) + // Project Euler Condition Check + test('if digit is 3', () => { + expect(largestPalindromic(3)).toBe(906609) + }) +}) From 96224e7950ab7bde4799d8103523b779e523e58c Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 23 Oct 2023 00:34:58 +0530 Subject: [PATCH 07/13] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20auto=20prettier?= =?UTF-8?q?=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bit-Manipulation/BinaryCountSetBits.js | 2 +- Maths/AutomorphicNumber.js | 2 +- Maths/test/AutomorphicNumber.test.js | 16 ++++----- Project-Euler/Problem006.js | 6 ++-- Recursive/test/BinaryEquivalent.test.js | 48 ++++++++++++------------- Search/InterpolationSearch.js | 2 +- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index b879f3bd67..b959caf062 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { let count = 0 while (a) { - a &= (a - 1) + a &= a - 1 count++ } diff --git a/Maths/AutomorphicNumber.js b/Maths/AutomorphicNumber.js index d1b6608316..ba008271fc 100644 --- a/Maths/AutomorphicNumber.js +++ b/Maths/AutomorphicNumber.js @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { n = Math.floor(n / 10) n_sq = Math.floor(n_sq / 10) } - + return true } diff --git a/Maths/test/AutomorphicNumber.test.js b/Maths/test/AutomorphicNumber.test.js index 19b963388c..57f40d27ee 100644 --- a/Maths/test/AutomorphicNumber.test.js +++ b/Maths/test/AutomorphicNumber.test.js @@ -9,19 +9,19 @@ describe('AutomorphicNumber', () => { }) test.each([ - { n: -3 , expected: false }, - { n: -25 , expected: false }, + { n: -3, expected: false }, + { n: -25, expected: false } ])('should return false when n is negetive', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(false) }) test.each([ - { n: 7 , expected: false }, - { n: 83 , expected: false }, - { n: 0 , expected: true }, - { n: 1 , expected: true }, - { n: 376 , expected: true }, - { n: 90625 , expected: true }, + { n: 7, expected: false }, + { n: 83, expected: false }, + { n: 0, expected: true }, + { n: 1, expected: true }, + { n: 376, expected: true }, + { n: 90625, expected: true } ])('should return $expected when n is $n', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(expected) }) diff --git a/Project-Euler/Problem006.js b/Project-Euler/Problem006.js index 474de2ae96..804b165558 100644 --- a/Project-Euler/Problem006.js +++ b/Project-Euler/Problem006.js @@ -1,8 +1,8 @@ // https://projecteuler.net/problem=6 export const squareDifference = (num = 100) => { - let sumOfSquares = (num)*(num+1)*(2*num+1)/6 - let sums = (num*(num+1))/2 - + let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6 + let sums = (num * (num + 1)) / 2 + return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares } diff --git a/Recursive/test/BinaryEquivalent.test.js b/Recursive/test/BinaryEquivalent.test.js index b79a455eed..ddabb7d477 100644 --- a/Recursive/test/BinaryEquivalent.test.js +++ b/Recursive/test/BinaryEquivalent.test.js @@ -1,29 +1,29 @@ -import { binaryEquivalent } from "../BinaryEquivalent"; +import { binaryEquivalent } from '../BinaryEquivalent' const tests = [ - { - test: 2, - expectedValue: "10" - }, - { - test: 0, - expectedValue: "0" - }, - { - test: 543, - expectedValue: "1000011111" - }, - { - test: 4697621023, - expectedValue: "100011000000000000000001000011111" - } + { + test: 2, + expectedValue: '10' + }, + { + test: 0, + expectedValue: '0' + }, + { + test: 543, + expectedValue: '1000011111' + }, + { + test: 4697621023, + expectedValue: '100011000000000000000001000011111' + } ] -describe("Binary Equivalent", () => { - test.each(tests)( - "of $test should be $expectedValue", - ({test, expectedValue}) => { - expect(binaryEquivalent(test)).toBe(expectedValue); - } - ) +describe('Binary Equivalent', () => { + test.each(tests)( + 'of $test should be $expectedValue', + ({ test, expectedValue }) => { + expect(binaryEquivalent(test)).toBe(expectedValue) + } + ) }) diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index e6deae496f..93f3b78b0e 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { } return -1 -} \ No newline at end of file +} From 31aca33f0f7a9cc443176a0a6db8f84fa6978f0b Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Wed, 9 Oct 2024 20:02:45 +0530 Subject: [PATCH 08/13] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Project=20Euler=202?= =?UTF-8?q?2=20|=20Added=20solution=20and=20test=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem022/Names.txt | 38 +++++++++++++++++++++ Project-Euler/Problem022/Problem022.js | 35 +++++++++++++++++++ Project-Euler/Problem022/Problem022.test.js | 33 ++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 Project-Euler/Problem022/Names.txt create mode 100644 Project-Euler/Problem022/Problem022.js create mode 100644 Project-Euler/Problem022/Problem022.test.js diff --git a/Project-Euler/Problem022/Names.txt b/Project-Euler/Problem022/Names.txt new file mode 100644 index 0000000000..9e4f8df387 --- /dev/null +++ b/Project-Euler/Problem022/Names.txt @@ -0,0 +1,38 @@ +"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY", +"LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE", +"LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA", +"AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE", +"CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE", +"JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE", +"JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY", +"THERESA","BEVERLY","DENISE","TAMMY","IRENE","JANE","LORI","RACHEL","MARILYN","ANDREA", +"KATHRYN","LOUISE","SARA","ANNE","JACQUELINE","WANDA","BONNIE","JULIA","RUBY","LOIS", +"TINA","PHYLLIS","NORMA","PAULA","DIANA","ANNIE","LILLIAN","EMILY","ROBIN","PEGGY", +"CRYSTAL","GLADYS","RITA","DAWN","CONNIE","FLORENCE","TRACY","EDNA","TIFFANY","CARMEN", +"ROSA","CINDY","GRACE","WENDY","VICTORIA","EDITH","KIM","SHERRY","SYLVIA","JOSEPHINE", +"THELMA","SHANNON","SHEILA","ETHEL","ELLEN","ELAINE","MARJORIE","CARRIE","CHARLOTTE","MONICA", +"ESTHER","PAULINE","EMMA","JUANITA","ANITA","RHONDA","HAZEL","AMBER","EVA","DEBBIE", +"APRIL","LESLIE","CLARA","LUCILLE","JAMIE","JOANNE","ELEANOR","VALERIE","DANIELLE","MEGAN", +"ALICIA","SUZANNE","MICHELE","GAIL","BERTHA","DARLENE","VERONICA","JILL","ERIN","GERALDINE", +"LAUREN","CATHY","JOANN","LORRAINE","LYNN","SALLY","REGINA","ERICA","BEATRICE","DOLORES", +"BERNICE","AUDREY","YVONNE","ANNETTE","JUNE","SAMANTHA","MARION","DANA","STACY","ANA", +"RENEE","IDA","VIVIAN","ROBERTA","HOLLY","BRITTANY","MELANIE","LORETTA","YOLANDA","JEANETTE", +"LAURIE","KATIE","KRISTEN","VANESSA","ALMA","SUE","ELSIE","BETH","JEANNE","VICKI", +"CARLA","TARA","ROSEMARY","EILEEN","TERRI","GERTRUDE","LUCY","TONYA","ELLA","STACEY", +"WILMA","GINA","KRISTIN","JESSIE","NATALIE","AGNES","VERA","WILLIE","CHARLENE","BESSIE", +"DELORES","MELINDA","PEARL","ARLENE","MAUREEN","COLLEEN","ALLISON","TAMARA","JOY","GEORGIA", +"CONSTANCE","LILLIE","CLAUDIA","JACKIE","MARCIA","TANYA","NELLIE","MINNIE","MARLENE","HEIDI", +"GLENDA","LYDIA","VIOLA","COURTNEY","MARIAN","STELLA","CAROLINE","DORA","JO","VICKIE", +"MATTIE","TERRY","MAXINE","IRMA","MABEL","MARSHA","MYRTLE","LENA","CHRISTY","DEANNA", +"PATSY","HILDA","GWENDOLYN","JENNIE","NORA","MARGIE","NINA","CASSANDRA","LEAH","PENNY", +"KAY","PRISCILLA","NAOMI","CAROLE","BRANDY","OLGA","BILLIE","DIANNE","TRACEY","LEONA", +"JENNY","FELICIA","SONIA","MIRIAM","VELMA","BECKY","BOBBIE","VIOLET","KRISTINA","TONI", +"MISTY","MAE","SHELLY","DAISY","RAMONA","SHERRI","ERIKA","KATRINA","CLAIRE","LINDSEY", +"LINDSAY","GENEVA","GUADALUPE","BELINDA","MARGARITA","SHERYL","CORA","FAYE","ADA","NATASHA", +"SABRINA","ISABEL","MARGUERITE","HATTIE","HARRIET","MOLLY","CECILIA","KRISTI","BRANDI","BLANCHE", +"SANDY","ROSIE","JOANNA","IRIS","EUNICE","ANGIE","INEZ","LYNDA","MADELINE","AMELIA", +"ALYSSA","KRISTY","CATHLEEN","ANGELINE","FRIEDA","ANTOINETTE","CECIL","JASMINE","NORMA","HANNAH", +"SANDRA","KEISHA","CLAIRE","ELLA","TAMIKA","FANNIE","CELIA","PEGGY","MICHAELA","ALYSON", +"ALTHEA","KENDRA","LORA","CORA","SHIRLEY","MATTIE","EMMA","MONICA","CATHY","ELEANOR", +"REBECCA","TERESA","CLARA","PAMELA","SYLVIA","NANCY","FLORENCE","VICTORIA","ANITA","ELIZABETH", +"JESSIE","LORRAINE","GERTRUDE","KATHERINE","VIVIAN","LOUISE","SUSAN","WANDA","PATRICIA","MARY","OMKARNATH" \ No newline at end of file diff --git a/Project-Euler/Problem022/Problem022.js b/Project-Euler/Problem022/Problem022.js new file mode 100644 index 0000000000..841b4a69e9 --- /dev/null +++ b/Project-Euler/Problem022/Problem022.js @@ -0,0 +1,35 @@ +/* +Names Scores + +Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. +For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714. +What is the total of all the name scores in the file? +*/ +import fs from 'fs' + +export const getAlphabeticalValue = (name) => { + return [...name].reduce( + (sum, char) => sum + (char.charCodeAt(0) - 'A'.charCodeAt(0) + 1), + 0 + ) +} + +export const calculateTotalNameScore = (filePath) => { + return new Promise((resolve, reject) => { + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + return reject(err) + } + + const names = data.replace(/"/g, '').split(',').sort() + + const totalScore = names.reduce((total, name, index) => { + const alphabeticalValue = getAlphabeticalValue(name) + const nameScore = alphabeticalValue * (index + 1) + return total + nameScore + }, 0) + + resolve(totalScore) + }) + }) +} diff --git a/Project-Euler/Problem022/Problem022.test.js b/Project-Euler/Problem022/Problem022.test.js new file mode 100644 index 0000000000..be3690f87b --- /dev/null +++ b/Project-Euler/Problem022/Problem022.test.js @@ -0,0 +1,33 @@ +import { describe, it, expect } from 'vitest' +import { calculateTotalNameScore, getAlphabeticalValue } from './Problem022' +const path = require('path') + +describe('getAlphabeticalValue', () => { + it('should return correct alphabetical value for COLIN', () => { + const name = 'COLIN' + const expectedValue = 3 + 15 + 12 + 9 + 14 + expect(getAlphabeticalValue(name)).toBe(expectedValue) + }) + + it('should return correct alphabetical value for A', () => { + const name = 'A' + const expectedValue = 1 + expect(getAlphabeticalValue(name)).toBe(expectedValue) + }) + + it('should return correct alphabetical value for Z', () => { + const name = 'Z' + const expectedValue = 26 + expect(getAlphabeticalValue(name)).toBe(expectedValue) + }) +}) + +describe('calculateTotalNameScore', () => { + it('should correctly calculate the total name score', async () => { + const namesFilePath = path.join(__dirname, 'Names.txt') + const result = await calculateTotalNameScore(namesFilePath) + + const expectedScore = 4654143 + expect(result).toBe(expectedScore) + }) +}) From e7e5716e60554001217f95d45a23bd419bfcde65 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 13 Oct 2024 23:42:34 +0530 Subject: [PATCH 09/13] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Added=20Solution=20?= =?UTF-8?q?and=20Testcases=20for=20Project=20Euler=20Problem=2024?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem024.js | 21 ++++++++++++++++++ Project-Euler/test/Problem024.test.js | 31 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Project-Euler/Problem024.js create mode 100644 Project-Euler/test/Problem024.test.js diff --git a/Project-Euler/Problem024.js b/Project-Euler/Problem024.js new file mode 100644 index 0000000000..1b5a66b87c --- /dev/null +++ b/Project-Euler/Problem024.js @@ -0,0 +1,21 @@ +function factorial(n) { + return n <= 1 ? 1 : n * factorial(n - 1) +} + +export function findNthLexicographicPermutation(digits, n) { + let permutation = '' + let k = n - 1 + + while (digits.length > 0) { + const fact = factorial(digits.length - 1) + const index = Math.floor(k / fact) + permutation += digits[index] + digits.splice(index, 1) + k %= fact + } + + return permutation +} + +const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +console.log(findNthLexicographicPermutation(digits, 1000000)) diff --git a/Project-Euler/test/Problem024.test.js b/Project-Euler/test/Problem024.test.js new file mode 100644 index 0000000000..fa90c546b9 --- /dev/null +++ b/Project-Euler/test/Problem024.test.js @@ -0,0 +1,31 @@ +import { describe, it, expect } from 'vitest' +import { findNthLexicographicPermutation } from '../Problem024' + +describe('findNthLexicographicPermutation', () => { + it('should return the correct permutation for small cases', () => { + expect(findNthLexicographicPermutation([0, 1, 2], 1)).toBe('012') + expect(findNthLexicographicPermutation([0, 1, 2], 2)).toBe('021') + expect(findNthLexicographicPermutation([0, 1, 2], 6)).toBe('210') + }) + + it('should return the correct 1st permutation for digits 0-9', () => { + expect( + findNthLexicographicPermutation([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 1) + ).toBe('0123456789') + }) + + it('should return the correct millionth permutation for digits 0-9', () => { + expect( + findNthLexicographicPermutation([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 1000000) + ).toBe('2783915460') + }) + + it('should return the correct permutation for a smaller set of digits', () => { + expect(findNthLexicographicPermutation([1, 2, 3, 4], 12)).toBe('2431') + expect(findNthLexicographicPermutation([1, 2, 3, 4], 24)).toBe('4321') + }) + + it('should handle large values of n', () => { + expect(findNthLexicographicPermutation([0, 1, 2, 3, 4], 120)).toBe('43210') + }) +}) From 9fde20a55401f9d90f82c4d88eb9d0b1a2dc016c Mon Sep 17 00:00:00 2001 From: pomkarnath98 Date: Sun, 13 Oct 2024 18:13:23 +0000 Subject: [PATCH 10/13] Updated Documentation in README.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7f6484cae5..be382925ce 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -290,7 +290,10 @@ * [Problem019](Project-Euler/Problem019.js) * [Problem020](Project-Euler/Problem020.js) * [Problem021](Project-Euler/Problem021.js) + * **Problem022** + * [Problem022](Project-Euler/Problem022/Problem022.js) * [Problem023](Project-Euler/Problem023.js) + * [Problem024](Project-Euler/Problem024.js) * [Problem025](Project-Euler/Problem025.js) * [Problem028](Project-Euler/Problem028.js) * [Problem035](Project-Euler/Problem035.js) From 0eb0a9b157466019d57c6215e28b421d1e91fd89 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 13 Oct 2024 23:45:33 +0530 Subject: [PATCH 11/13] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20Problem?= =?UTF-8?q?=20Statement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem024.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Project-Euler/Problem024.js b/Project-Euler/Problem024.js index 1b5a66b87c..85668741ea 100644 --- a/Project-Euler/Problem024.js +++ b/Project-Euler/Problem024.js @@ -1,3 +1,11 @@ +/* +A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are: + +012 021 102 120 201 210 + +What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? +*/ + function factorial(n) { return n <= 1 ? 1 : n * factorial(n - 1) } From ca7a5e812e6b488220de0ab21528fcf2e654c9d1 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 13 Oct 2024 23:52:41 +0530 Subject: [PATCH 12/13] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20code=20improvements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem022/Problem022.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Project-Euler/Problem022/Problem022.js b/Project-Euler/Problem022/Problem022.js index 841b4a69e9..2ebb4743e0 100644 --- a/Project-Euler/Problem022/Problem022.js +++ b/Project-Euler/Problem022/Problem022.js @@ -17,10 +17,6 @@ export const getAlphabeticalValue = (name) => { export const calculateTotalNameScore = (filePath) => { return new Promise((resolve, reject) => { fs.readFile(filePath, 'utf8', (err, data) => { - if (err) { - return reject(err) - } - const names = data.replace(/"/g, '').split(',').sort() const totalScore = names.reduce((total, name, index) => { From bb0b2d6e6f5997c532a144dd989f51e9916a424e Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 13 Oct 2024 23:53:38 +0530 Subject: [PATCH 13/13] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20code=20improvement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem022/Problem022.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem022/Problem022.js b/Project-Euler/Problem022/Problem022.js index 2ebb4743e0..124e6f21d0 100644 --- a/Project-Euler/Problem022/Problem022.js +++ b/Project-Euler/Problem022/Problem022.js @@ -15,8 +15,8 @@ export const getAlphabeticalValue = (name) => { } export const calculateTotalNameScore = (filePath) => { - return new Promise((resolve, reject) => { - fs.readFile(filePath, 'utf8', (err, data) => { + return new Promise((resolve) => { + fs.readFile(filePath, 'utf8', (_, data) => { const names = data.replace(/"/g, '').split(',').sort() const totalScore = names.reduce((total, name, index) => {