Skip to content

Commit abb5b28

Browse files
authored
Add say (#383)
1 parent 56f7934 commit abb5b28

File tree

8 files changed

+250
-0
lines changed

8 files changed

+250
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,14 @@
752752
"prerequisites": [],
753753
"difficulty": 5
754754
},
755+
{
756+
"slug": "say",
757+
"name": "Say",
758+
"uuid": "0f01ba32-e46b-47ac-93c7-e0c3739424ae",
759+
"practices": [],
760+
"prerequisites": [],
761+
"difficulty": 5
762+
},
755763
{
756764
"slug": "spiral-matrix",
757765
"name": "Spiral Matrix",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Instructions
2+
3+
Given a number, your task is to express it in English words exactly as your friend should say it out loud.
4+
Yaʻqūb expects to use numbers from 0 up to 999,999,999,999.
5+
6+
Examples:
7+
8+
- 0 → zero
9+
- 1 → one
10+
- 12 → twelve
11+
- 123 → one hundred twenty-three
12+
- 1,234 → one thousand two hundred thirty-four
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
Your friend Yaʻqūb works the counter at the busiest deli in town, slicing, weighing, and wrapping orders for a never-ending line of hungry customers.
4+
To keep things moving, each customer takes a numbered ticket when they arrive.
5+
6+
When it’s time to call the next person, Yaʻqūb reads their number out loud, always in full English words to make sure everyone hears it clearly.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"say.coffee"
8+
],
9+
"test": [
10+
"say.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Given a number from 0 to 999,999,999,999, spell out that number in English.",
17+
"source": "A variation on the JavaRanch CattleDrive, Assignment 4",
18+
"source_url": "https://web.archive.org/web/20240907035912/https://coderanch.com/wiki/718804"
19+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Say
2+
@say: (number) ->
3+
throw new Error 'input out of range' if number < 0 or number > 999999999999
4+
5+
if number is 0
6+
return 'zero'
7+
8+
chunks = @chunksFromRight number.toString()
9+
words = (chunks.map Number).map @toWords.bind @
10+
words = words.map @applyScale.bind @
11+
words = words.filter (word) -> word isnt ''
12+
words.reverse().join(' ')
13+
14+
@chunksFromRight: (str) ->
15+
chunks = []
16+
working = str
17+
18+
while working.length > 0
19+
chunks.push working.slice -3
20+
working = working.slice 0, -3
21+
22+
chunks
23+
24+
@toWords: (chunk) ->
25+
words = []
26+
if chunk < 20
27+
words.push @firstTwentyNumbers[chunk]
28+
else if chunk < 100
29+
tens = @wordsForTens[Math.floor(chunk / 10)]
30+
ones = @firstTwentyNumbers[chunk % 10]
31+
if ones
32+
words.push "#{tens}-#{ones}"
33+
else
34+
words.push tens
35+
else
36+
hundreds = @firstTwentyNumbers[Math.floor(chunk / 100)]
37+
rest = @toWords(chunk % 100)
38+
if rest
39+
words.push "#{hundreds} hundred #{rest}"
40+
else
41+
words.push "#{hundreds} hundred"
42+
43+
words.join ' '
44+
45+
@applyScale: (word, index) ->
46+
if word is ''
47+
return ''
48+
if index == 0
49+
return word
50+
if index == 1
51+
return "#{word} thousand"
52+
if index == 2
53+
return "#{word} million"
54+
if index == 3
55+
return "#{word} billion"
56+
else
57+
return word
58+
59+
@firstTwentyNumbers: [
60+
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
61+
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
62+
]
63+
64+
@wordsForTens: [
65+
'', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
66+
]
67+
68+
module.exports = Say
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[5d22a120-ba0c-428c-bd25-8682235d83e8]
13+
description = "zero"
14+
15+
[9b5eed77-dbf6-439d-b920-3f7eb58928f6]
16+
description = "one"
17+
18+
[7c499be1-612e-4096-a5e1-43b2f719406d]
19+
description = "fourteen"
20+
21+
[f541dd8e-f070-4329-92b4-b7ce2fcf06b4]
22+
description = "twenty"
23+
24+
[d78601eb-4a84-4bfa-bf0e-665aeb8abe94]
25+
description = "twenty-two"
26+
27+
[f010d4ca-12c9-44e9-803a-27789841adb1]
28+
description = "thirty"
29+
30+
[738ce12d-ee5c-4dfb-ad26-534753a98327]
31+
description = "ninety-nine"
32+
33+
[e417d452-129e-4056-bd5b-6eb1df334dce]
34+
description = "one hundred"
35+
36+
[d6924f30-80ba-4597-acf6-ea3f16269da8]
37+
description = "one hundred twenty-three"
38+
39+
[2f061132-54bc-4fd4-b5df-0a3b778959b9]
40+
description = "two hundred"
41+
42+
[feed6627-5387-4d38-9692-87c0dbc55c33]
43+
description = "nine hundred ninety-nine"
44+
45+
[3d83da89-a372-46d3-b10d-de0c792432b3]
46+
description = "one thousand"
47+
48+
[865af898-1d5b-495f-8ff0-2f06d3c73709]
49+
description = "one thousand two hundred thirty-four"
50+
51+
[b6a3f442-266e-47a3-835d-7f8a35f6cf7f]
52+
description = "one million"
53+
54+
[2cea9303-e77e-4212-b8ff-c39f1978fc70]
55+
description = "one million two thousand three hundred forty-five"
56+
57+
[3e240eeb-f564-4b80-9421-db123f66a38f]
58+
description = "one billion"
59+
60+
[9a43fed1-c875-4710-8286-5065d73b8a9e]
61+
description = "a big number"
62+
63+
[49a6a17b-084e-423e-994d-a87c0ecc05ef]
64+
description = "numbers below zero are out of range"
65+
66+
[4d6492eb-5853-4d16-9d34-b0f61b261fd9]
67+
description = "numbers above 999,999,999,999 are out of range"

exercises/practice/say/say.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Say
2+
@say: (number) ->
3+
4+
module.exports = Say
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Say = require './say'
2+
3+
describe 'Say', ->
4+
it 'zero', ->
5+
expect(Say.say 0).toEqual 'zero'
6+
7+
xit 'one', ->
8+
expect(Say.say 1).toEqual 'one'
9+
10+
xit 'fourteen', ->
11+
expect(Say.say 14).toEqual 'fourteen'
12+
13+
xit 'twenty', ->
14+
expect(Say.say 20).toEqual 'twenty'
15+
16+
xit 'twenty-two', ->
17+
expect(Say.say 22).toEqual 'twenty-two'
18+
19+
xit 'thirty', ->
20+
expect(Say.say 30).toEqual 'thirty'
21+
22+
xit 'ninety-nine', ->
23+
expect(Say.say 99).toEqual 'ninety-nine'
24+
25+
xit 'one hundred', ->
26+
expect(Say.say 100).toEqual 'one hundred'
27+
28+
xit 'one hundred twenty-three', ->
29+
expect(Say.say 123).toEqual 'one hundred twenty-three'
30+
31+
xit 'two hundred', ->
32+
expect(Say.say 200).toEqual 'two hundred'
33+
34+
xit 'nine hundred ninety-nine', ->
35+
expect(Say.say 999).toEqual 'nine hundred ninety-nine'
36+
37+
xit 'one thousand', ->
38+
expect(Say.say 1000).toEqual 'one thousand'
39+
40+
xit 'one thousand two hundred thirty-four', ->
41+
expect(Say.say 1234).toEqual 'one thousand two hundred thirty-four'
42+
43+
xit 'one million', ->
44+
expect(Say.say 1000000).toEqual 'one million'
45+
46+
xit 'one million two thousand three hundred forty-five', ->
47+
expect(Say.say 1002345).toEqual 'one million two thousand three hundred forty-five'
48+
49+
xit 'one billion', ->
50+
expect(Say.say 1000000000).toEqual 'one billion'
51+
52+
xit 'a big number', ->
53+
expect(Say.say 987654321123).toEqual 'nine hundred eighty-seven billion ' +
54+
'six hundred fifty-four million ' +
55+
'three hundred twenty-one thousand ' +
56+
'one hundred twenty-three'
57+
58+
xit 'numbers below zero are out of range', ->
59+
expect ->
60+
Say.say -1
61+
.toThrow new Error 'input out of range'
62+
63+
xit 'numbers above 999,999,999,999 are out of range', ->
64+
expect ->
65+
Say.say 1000000000000
66+
.toThrow new Error 'input out of range'

0 commit comments

Comments
 (0)