Skip to content

Commit 54c11de

Browse files
authored
say (#63)
1 parent 60e5bbd commit 54c11de

File tree

10 files changed

+266
-0
lines changed

10 files changed

+266
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@
486486
"practices": [],
487487
"prerequisites": [],
488488
"difficulty": 6
489+
},
490+
{
491+
"slug": "say",
492+
"name": "Say",
493+
"uuid": "c1c9b94f-abbb-4356-b35b-8cff366e9415",
494+
"practices": [],
495+
"prerequisites": [],
496+
"difficulty": 6
489497
}
490498
]
491499
},

exercises/practice/say/.busted

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"say.moon"
8+
],
9+
"test": [
10+
"say_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
WORDS = {
2+
[0]: 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
3+
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
4+
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty',
5+
[30]: 'thirty', [40]: 'forty', [50]: 'fifty', [60]: 'sixty',
6+
[70]: 'seventy', [80]: 'eighty', [90]: 'ninety',
7+
}
8+
9+
10+
say_small = (n) -> WORDS[n] or "#{WORDS[n - n % 10]}-#{WORDS[n % 10]}"
11+
12+
local in_english
13+
14+
say_compound = (n, base, unit) ->
15+
quotient, remainder = n // base, n % base
16+
saying = "#{in_english quotient} #{unit}"
17+
saying ..= " #{in_english remainder}" if remainder > 0
18+
saying
19+
20+
21+
in_english = (n) ->
22+
assert 0 <= n and n < 1e12, 'input out of range'
23+
24+
if n < 100 then say_small n
25+
elseif n < 1e3 then say_compound n, 100, 'hundred'
26+
elseif n < 1e6 then say_compound n, 1e3, 'thousand'
27+
elseif n < 1e9 then say_compound n, 1e6, 'million'
28+
else say_compound n, 1e9, 'billion'
29+
30+
31+
{ :in_english }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
module_name: 'Say',
3+
4+
generate_test: (case, level) ->
5+
local lines
6+
switch type(case.expected)
7+
when 'table'
8+
lines = {
9+
"f = -> Say.in_english #{case.input.number}",
10+
"assert.has.error f,#{quote case.expected.error}"
11+
}
12+
else
13+
lines = {
14+
"result = Say.in_english #{case.input.number}",
15+
"expected = #{quote case.expected}",
16+
"assert.are.equal expected, result"
17+
}
18+
table.concat [indent line, level for line in *lines], '\n'
19+
}
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.moon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
in_english: (number) ->
3+
error 'Implement me'
4+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Say = require './say'
2+
3+
describe 'say', ->
4+
it 'zero', ->
5+
result = Say.in_english 0
6+
expected = 'zero'
7+
assert.are.equal expected, result
8+
9+
pending 'one', ->
10+
result = Say.in_english 1
11+
expected = 'one'
12+
assert.are.equal expected, result
13+
14+
pending 'fourteen', ->
15+
result = Say.in_english 14
16+
expected = 'fourteen'
17+
assert.are.equal expected, result
18+
19+
pending 'twenty', ->
20+
result = Say.in_english 20
21+
expected = 'twenty'
22+
assert.are.equal expected, result
23+
24+
pending 'twenty-two', ->
25+
result = Say.in_english 22
26+
expected = 'twenty-two'
27+
assert.are.equal expected, result
28+
29+
pending 'thirty', ->
30+
result = Say.in_english 30
31+
expected = 'thirty'
32+
assert.are.equal expected, result
33+
34+
pending 'ninety-nine', ->
35+
result = Say.in_english 99
36+
expected = 'ninety-nine'
37+
assert.are.equal expected, result
38+
39+
pending 'one hundred', ->
40+
result = Say.in_english 100
41+
expected = 'one hundred'
42+
assert.are.equal expected, result
43+
44+
pending 'one hundred twenty-three', ->
45+
result = Say.in_english 123
46+
expected = 'one hundred twenty-three'
47+
assert.are.equal expected, result
48+
49+
pending 'two hundred', ->
50+
result = Say.in_english 200
51+
expected = 'two hundred'
52+
assert.are.equal expected, result
53+
54+
pending 'nine hundred ninety-nine', ->
55+
result = Say.in_english 999
56+
expected = 'nine hundred ninety-nine'
57+
assert.are.equal expected, result
58+
59+
pending 'one thousand', ->
60+
result = Say.in_english 1000
61+
expected = 'one thousand'
62+
assert.are.equal expected, result
63+
64+
pending 'one thousand two hundred thirty-four', ->
65+
result = Say.in_english 1234
66+
expected = 'one thousand two hundred thirty-four'
67+
assert.are.equal expected, result
68+
69+
pending 'one million', ->
70+
result = Say.in_english 1000000
71+
expected = 'one million'
72+
assert.are.equal expected, result
73+
74+
pending 'one million two thousand three hundred forty-five', ->
75+
result = Say.in_english 1002345
76+
expected = 'one million two thousand three hundred forty-five'
77+
assert.are.equal expected, result
78+
79+
pending 'one billion', ->
80+
result = Say.in_english 1000000000
81+
expected = 'one billion'
82+
assert.are.equal expected, result
83+
84+
pending 'a big number', ->
85+
result = Say.in_english 987654321123
86+
expected = 'nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three'
87+
assert.are.equal expected, result
88+
89+
pending 'numbers below zero are out of range', ->
90+
f = -> Say.in_english -1
91+
assert.has.error f,'input out of range'
92+
93+
pending 'numbers above 999,999,999,999 are out of range', ->
94+
f = -> Say.in_english 1000000000000
95+
assert.has.error f,'input out of range'

0 commit comments

Comments
 (0)