From 2d395ec5c696bf0ab150a6e689c3639fa238e01b Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Tue, 11 Oct 2022 23:28:25 +0200 Subject: [PATCH 1/9] :white_check_mark: tests added for Problem017 --- Project-Euler/test/Problem017.test.js | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Project-Euler/test/Problem017.test.js diff --git a/Project-Euler/test/Problem017.test.js b/Project-Euler/test/Problem017.test.js new file mode 100644 index 0000000000..5f7e786723 --- /dev/null +++ b/Project-Euler/test/Problem017.test.js @@ -0,0 +1,45 @@ +import { countNumberWordLength } from '../Problem017.js' + +describe('Number letter count', () => { + const textFixture = [ + { + name: 'should throw invalid input (NaN) error', + input: 'test', + error: 'Invalid input, please provide valid number' + }, + { + name: 'should throw error if number < 1', + input: 0, + error: 'Please provide number greater that 1' + }, + { + name: 'if the number provided is 5', + input: 5, + expected: 19 + }, + { + name: 'if the number provided is 100', + input: 100, + expected: 864 + }, + { + name: 'if the number provided is 1000', + input: 1000, + expected: 21124 + } + ] + + // it('should be invalid input if number is negative', () => { + // expect(() => nthPrime(-3)).toThrowError('Invalid Input') + // }) + + for (const tt of textFixture) { + test(tt.name, () => { + if (tt.error) { + expect(() => countNumberWordLength(tt.input)).toThrowError(tt.error) + } else { + expect(countNumberWordLength(tt.input)).toBe(tt.expected) + } + }) + } +}) From 61940752212a8aea57344c18278ea029225fae8e Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Tue, 11 Oct 2022 23:28:46 +0200 Subject: [PATCH 2/9] :sparkles: code added for Problem017 --- Project-Euler/Problem017.js | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Project-Euler/Problem017.js diff --git a/Project-Euler/Problem017.js b/Project-Euler/Problem017.js new file mode 100644 index 0000000000..4a1b6546be --- /dev/null +++ b/Project-Euler/Problem017.js @@ -0,0 +1,54 @@ +const numberToWordLength = (n) => { + let inWord = '' + + const ones = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', + 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + const tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + + if (n < 20) { + return ones[n] + } + + if (n >= 20 && n < 100) { + const unit = n % 10 + return tens[n / 10 - 2 | 0] + ((unit !== 0) ? ones[unit] : '') + } + + const hundred = Math.floor(n / 100) % 10 + const thousand = Math.floor(n / 1000) + const sub = n % 100 + + if (n > 999) { + inWord += numberToWordLength(thousand) + 'thousand' + } + + if (hundred !== 0) { + inWord += ones[hundred] + 'hundred' + } + + if (sub !== 0) { + inWord += 'and' + numberToWordLength(sub) + } + + return inWord +} + +const countNumberWordLength = (number) => { + let count = 0 + + if (Number.isNaN(parseInt(number))) { + throw new Error('Invalid input, please provide valid number') + } + + if (number < 1) { + throw new Error('Please provide number greater that 1') + } + + for (let i = 1; i <= number; i++) { + count += numberToWordLength(i).length + } + + return count +} + +export { countNumberWordLength } From 50364f05980642fb6ad64898180daae4ee6fac16 Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Tue, 11 Oct 2022 23:58:44 +0200 Subject: [PATCH 3/9] :bulb: code comments added --- Project-Euler/Problem017.js | 63 ++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/Project-Euler/Problem017.js b/Project-Euler/Problem017.js index 4a1b6546be..6ec1dab240 100644 --- a/Project-Euler/Problem017.js +++ b/Project-Euler/Problem017.js @@ -1,53 +1,106 @@ -const numberToWordLength = (n) => { +/** + * Problem 17 - Number letter counts + * + * @see {@link https://projecteuler.net/problem=17} + * + * If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. + * If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? + * + * @author Chetan07j + */ + +/** + * Function to convert number to word + * + * This function is called recursively to handle thousand and its sub part + */ +const numberToWord = (n) => { let inWord = '' + // Array of number from 0 -> 19 in words const ones = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + + // Array for tens from 20 -> 90 in words const tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + // If number is < 20 then return its corresponding value from ones if (n < 20) { return ones[n] } + /** + * To calculate tens value "n / 10 - 2" is perfomed, which might return decimal value + * to extract proper integer value Math.floor is added + * + * To find ones part, unit is identified by n % 10 + * If > 0 then ones word is apended to tens word otherwise nothing + * e.g., + * 1. 34 -> thirtyfour + * 2. 30 -> thirty + */ if (n >= 20 && n < 100) { const unit = n % 10 - return tens[n / 10 - 2 | 0] + ((unit !== 0) ? ones[unit] : '') + return tens[Math.floor(n / 10 - 2)] + ((unit !== 0) ? ones[unit] : '') } + // Find thousand, hundred and sub part const hundred = Math.floor(n / 100) % 10 const thousand = Math.floor(n / 1000) const sub = n % 100 + // Find ones for thousand part number + // e.g., thousand = 2 => inWord = twothousand if (n > 999) { - inWord += numberToWordLength(thousand) + 'thousand' + inWord += numberToWord(thousand) + 'thousand' } + // Find ones for hundred part number + // e.g., hundred = 1 => inWord = onehundred if (hundred !== 0) { inWord += ones[hundred] + 'hundred' } + // Find and part of number + // e.g., 922 => ninehundred"andtwentytwo" if (sub !== 0) { - inWord += 'and' + numberToWordLength(sub) + inWord += 'and' + numberToWord(sub) } + // return number in word format return inWord } +/** + * Function responsible for calculating total number word length + * for provided input number + * Validation is performed for input + * Loop is executed to find total word length for given number range + * starting from 1 + * + * + * @param {number} number + * @returns {number} + */ const countNumberWordLength = (number) => { let count = 0 + // Not a number check if (Number.isNaN(parseInt(number))) { throw new Error('Invalid input, please provide valid number') } + // Number should be greater than 1 if (number < 1) { throw new Error('Please provide number greater that 1') } + // Loop to calulate word length by calling {@link numberToWord} for (let i = 1; i <= number; i++) { - count += numberToWordLength(i).length + count += numberToWord(i).length } + // return final count for number word length return count } From f6e12a1b9b6667e46d39dae482f05866035c4bca Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Wed, 12 Oct 2022 00:06:34 +0200 Subject: [PATCH 4/9] :bug: fix spelling errors --- Project-Euler/Problem017.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project-Euler/Problem017.js b/Project-Euler/Problem017.js index 6ec1dab240..a8da148c7d 100644 --- a/Project-Euler/Problem017.js +++ b/Project-Euler/Problem017.js @@ -30,11 +30,11 @@ const numberToWord = (n) => { } /** - * To calculate tens value "n / 10 - 2" is perfomed, which might return decimal value + * To calculate tens value "n / 10 - 2" is performed, which might return decimal value * to extract proper integer value Math.floor is added * * To find ones part, unit is identified by n % 10 - * If > 0 then ones word is apended to tens word otherwise nothing + * If > 0 then ones word is appended to tens word otherwise nothing * e.g., * 1. 34 -> thirtyfour * 2. 30 -> thirty @@ -95,7 +95,7 @@ const countNumberWordLength = (number) => { throw new Error('Please provide number greater that 1') } - // Loop to calulate word length by calling {@link numberToWord} + // Loop to calculate word length by calling {@link numberToWord} for (let i = 1; i <= number; i++) { count += numberToWord(i).length } From 00d14d3802950206bc9f52107d54ec7add220ded Mon Sep 17 00:00:00 2001 From: Chetan Patil <31822501+Chetan07j@users.noreply.github.com> Date: Wed, 12 Oct 2022 08:29:06 +0200 Subject: [PATCH 5/9] Removed commented unused code --- Project-Euler/test/Problem017.test.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Project-Euler/test/Problem017.test.js b/Project-Euler/test/Problem017.test.js index 5f7e786723..2a2fcd539d 100644 --- a/Project-Euler/test/Problem017.test.js +++ b/Project-Euler/test/Problem017.test.js @@ -29,10 +29,6 @@ describe('Number letter count', () => { } ] - // it('should be invalid input if number is negative', () => { - // expect(() => nthPrime(-3)).toThrowError('Invalid Input') - // }) - for (const tt of textFixture) { test(tt.name, () => { if (tt.error) { From b90d121dd01034a70448097a3718b6ca02be1906 Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Wed, 12 Oct 2022 09:18:06 +0200 Subject: [PATCH 6/9] :bulb: updated comments --- Project-Euler/Problem017.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Project-Euler/Problem017.js b/Project-Euler/Problem017.js index a8da148c7d..7f43b8096a 100644 --- a/Project-Euler/Problem017.js +++ b/Project-Euler/Problem017.js @@ -32,6 +32,12 @@ const numberToWord = (n) => { /** * To calculate tens value "n / 10 - 2" is performed, which might return decimal value * to extract proper integer value Math.floor is added + * Here "-2" is performed as our tens array start with index 0 + * To get appropriate value from that for our number it is required + * e.g., + * -> 34 -> 34/10= 3.4 -> Math.floor(3.4) = 3 + * -> ones[3] = 'fifty' // this is wrong + * -> 3 - 2 = 1 -> ones[1] = 'thirty' * * To find ones part, unit is identified by n % 10 * If > 0 then ones word is appended to tens word otherwise nothing From 7f9b1ed2fa2ae23750bc61fc50fee2434a97d068 Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Wed, 12 Oct 2022 11:08:14 +0200 Subject: [PATCH 7/9] :recycle: code refractored, string concat replaced with word length --- Project-Euler/Problem017.js | 42 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/Project-Euler/Problem017.js b/Project-Euler/Problem017.js index 7f43b8096a..def4a427b8 100644 --- a/Project-Euler/Problem017.js +++ b/Project-Euler/Problem017.js @@ -9,20 +9,24 @@ * @author Chetan07j */ +// Array of number word length from 0 -> 19 +const ones = [4, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8] + +// Array for tens from 20, 30, 40, 50, 60, 70, 80, 90 in words length +const tens = [6, 6, 5, 5, 5, 7, 6, 6] + +// Word length for words thousand, hundred, and +const thousandLength = 8 +const hungredLength = 7 +const andLength = 3 + /** * Function to convert number to word * * This function is called recursively to handle thousand and its sub part */ -const numberToWord = (n) => { - let inWord = '' - - // Array of number from 0 -> 19 in words - const ones = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', - 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] - - // Array for tens from 20 -> 90 in words - const tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] +const numberToWordLength = (n) => { + let count = 0 // If number is < 20 then return its corresponding value from ones if (n < 20) { @@ -36,18 +40,18 @@ const numberToWord = (n) => { * To get appropriate value from that for our number it is required * e.g., * -> 34 -> 34/10= 3.4 -> Math.floor(3.4) = 3 - * -> ones[3] = 'fifty' // this is wrong - * -> 3 - 2 = 1 -> ones[1] = 'thirty' + * -> ones[3] = 5 // this is wrong + * -> 3 - 2 = 1 -> ones[1] = 6 * * To find ones part, unit is identified by n % 10 * If > 0 then ones word is appended to tens word otherwise nothing * e.g., - * 1. 34 -> thirtyfour - * 2. 30 -> thirty + * 1. 34 -> 10 + * 2. 30 -> 6 */ if (n >= 20 && n < 100) { const unit = n % 10 - return tens[Math.floor(n / 10 - 2)] + ((unit !== 0) ? ones[unit] : '') + return tens[Math.floor(n / 10 - 2)] + ((unit !== 0) ? ones[unit] : 0) } // Find thousand, hundred and sub part @@ -58,23 +62,23 @@ const numberToWord = (n) => { // Find ones for thousand part number // e.g., thousand = 2 => inWord = twothousand if (n > 999) { - inWord += numberToWord(thousand) + 'thousand' + count += numberToWordLength(thousand) + thousandLength } // Find ones for hundred part number // e.g., hundred = 1 => inWord = onehundred if (hundred !== 0) { - inWord += ones[hundred] + 'hundred' + count += ones[hundred] + hungredLength } // Find and part of number // e.g., 922 => ninehundred"andtwentytwo" if (sub !== 0) { - inWord += 'and' + numberToWord(sub) + count += andLength + numberToWordLength(sub) } // return number in word format - return inWord + return count } /** @@ -103,7 +107,7 @@ const countNumberWordLength = (number) => { // Loop to calculate word length by calling {@link numberToWord} for (let i = 1; i <= number; i++) { - count += numberToWord(i).length + count += numberToWordLength(i) } // return final count for number word length From 141d104234f7c5f8a088fb99a1d9bba4cc6eed52 Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Wed, 12 Oct 2022 11:09:47 +0200 Subject: [PATCH 8/9] test: refractored --- Project-Euler/test/Problem017.test.js | 42 ++++----------------------- 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/Project-Euler/test/Problem017.test.js b/Project-Euler/test/Problem017.test.js index 2a2fcd539d..401deb6d65 100644 --- a/Project-Euler/test/Problem017.test.js +++ b/Project-Euler/test/Problem017.test.js @@ -1,41 +1,11 @@ import { countNumberWordLength } from '../Problem017.js' describe('Number letter count', () => { - const textFixture = [ - { - name: 'should throw invalid input (NaN) error', - input: 'test', - error: 'Invalid input, please provide valid number' - }, - { - name: 'should throw error if number < 1', - input: 0, - error: 'Please provide number greater that 1' - }, - { - name: 'if the number provided is 5', - input: 5, - expected: 19 - }, - { - name: 'if the number provided is 100', - input: 100, - expected: 864 - }, - { - name: 'if the number provided is 1000', - input: 1000, - expected: 21124 - } - ] + test.each([[5, 19], [100, 864], [1000, 21124]])('%i! = %i', (n, expected) => { + expect(countNumberWordLength(n)).toBe(expected) + }) - for (const tt of textFixture) { - test(tt.name, () => { - if (tt.error) { - expect(() => countNumberWordLength(tt.input)).toThrowError(tt.error) - } else { - expect(countNumberWordLength(tt.input)).toBe(tt.expected) - } - }) - } + test.each([['test', 'Invalid input, please provide valid number'], [0, 'Please provide number greater that 1']])('%i! = %i', (n, expected) => { + expect(() => countNumberWordLength(n)).toThrowError(expected) + }) }) From 8ba11223bbdda3f707dd5863a79dae69d96a032d Mon Sep 17 00:00:00 2001 From: Chetan Patil Date: Wed, 12 Oct 2022 11:48:19 +0200 Subject: [PATCH 9/9] review comments addressed --- Project-Euler/Problem017.js | 4 ++-- Project-Euler/test/Problem017.test.js | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Project-Euler/Problem017.js b/Project-Euler/Problem017.js index def4a427b8..14096b6ec6 100644 --- a/Project-Euler/Problem017.js +++ b/Project-Euler/Problem017.js @@ -17,7 +17,7 @@ const tens = [6, 6, 5, 5, 5, 7, 6, 6] // Word length for words thousand, hundred, and const thousandLength = 8 -const hungredLength = 7 +const hundredLength = 7 const andLength = 3 /** @@ -68,7 +68,7 @@ const numberToWordLength = (n) => { // Find ones for hundred part number // e.g., hundred = 1 => inWord = onehundred if (hundred !== 0) { - count += ones[hundred] + hungredLength + count += ones[hundred] + hundredLength } // Find and part of number diff --git a/Project-Euler/test/Problem017.test.js b/Project-Euler/test/Problem017.test.js index 401deb6d65..3688d7bb60 100644 --- a/Project-Euler/test/Problem017.test.js +++ b/Project-Euler/test/Problem017.test.js @@ -1,11 +1,14 @@ import { countNumberWordLength } from '../Problem017.js' describe('Number letter count', () => { - test.each([[5, 19], [100, 864], [1000, 21124]])('%i! = %i', (n, expected) => { + test.each([[5, 19], [100, 864], [1000, 21124]])('Number letter count from 1 to %i', (n, expected) => { expect(countNumberWordLength(n)).toBe(expected) }) - test.each([['test', 'Invalid input, please provide valid number'], [0, 'Please provide number greater that 1']])('%i! = %i', (n, expected) => { + test.each([ + ['test', 'Invalid input, please provide valid number'], + [0, 'Please provide number greater that 1'] + ])('Should throw an error for input %i', (n, expected) => { expect(() => countNumberWordLength(n)).toThrowError(expected) }) }) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy