-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add project euler problem 17 #775
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
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? | ||
|
||
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage. | ||
*/ | ||
|
||
const numberToCharMap = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the variable inside the function. |
||
0: '', | ||
1: 'one', | ||
2: 'two', | ||
3: 'three', | ||
4: 'four', | ||
5: 'five', | ||
6: 'six', | ||
7: 'seven', | ||
8: 'eight', | ||
9: 'nine', | ||
10: 'ten', | ||
11: 'eleven', | ||
12: 'twelve', | ||
13: 'thirteen', | ||
14: 'fourteen', | ||
15: 'fifteen', | ||
16: 'sixteen', | ||
17: 'seventeen', | ||
18: 'eighteen', | ||
19: 'nineteen', | ||
20: 'twenty', | ||
30: 'thirty', | ||
40: 'forty', | ||
50: 'fifty', | ||
60: 'sixty', | ||
70: 'seventy', | ||
80: 'eighty', | ||
90: 'ninety', | ||
100: 'hundred', | ||
1000: 'onethousand' | ||
} | ||
|
||
const getOnes = (n) => Math.floor(n % 10) | ||
const getTens = (n) => Math.floor((n / 10) % 10) | ||
const getHundreds = (n) => Math.floor((n / 100) % 10) | ||
|
||
const countNumberLetter = (n) => { | ||
let numberLetter = '' | ||
|
||
for (let i = 1; i <= n; i++) { | ||
const ones = getOnes(i) | ||
const tens = getTens(i) | ||
const hundreds = getHundreds(i) | ||
|
||
if (i <= 20 || (i < 100 && ones === 0) || i === 1000) | ||
numberLetter += numberToCharMap[i] | ||
else if (i < 100) | ||
numberLetter += numberToCharMap[tens * 10] + numberToCharMap[ones] | ||
else if (i < 1000) { | ||
if (ones !== 0 || tens !== 0) numberLetter += 'and' | ||
const tensPlusOnes = tens * 10 + ones | ||
numberLetter += | ||
tensPlusOnes < 20 | ||
? numberToCharMap[hundreds] + | ||
numberToCharMap[100] + | ||
numberToCharMap[tensPlusOnes] | ||
: numberToCharMap[hundreds] + | ||
numberToCharMap[100] + | ||
numberToCharMap[tens * 10] + | ||
numberToCharMap[ones] | ||
} | ||
} | ||
|
||
return numberLetter.length | ||
} | ||
|
||
export { countNumberLetter } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { countNumberLetter } from '../Problem017' | ||
|
||
describe('checkCountNumberLetter', () => { | ||
it('Solves the problem statement example', () => { | ||
const numberLetter = countNumberLetter(1000) | ||
expect(numberLetter).toBe(21124) | ||
}) | ||
|
||
it('Returns first five numbers count', () => { | ||
const numberLetter = countNumberLetter(5) | ||
expect(numberLetter).toBe(19) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.