forked from mohitjain/leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path273_integer_to_english_words.rb
More file actions
72 lines (65 loc) · 1.84 KB
/
273_integer_to_english_words.rb
File metadata and controls
72 lines (65 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Leetcode Problem: https://leetcode.com/problems/integer-to-english-words/
# Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
#
# Example 1:
#
# Input: 123
# Output: "One Hundred Twenty Three"
#
# Example 2:
#
# Input: 12345
# Output: "Twelve Thousand Three Hundred Forty Five"
#
# Example 3:
#
# Input: 1234567
# Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
#
# Example 4:
#
# Input: 1234567891
# Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
#
# ----------------------------------------------------------------------------------------------------------------------
require_relative 'core/stack'
# @param {Integer} num
# @return {String}
def number_to_words(num)
ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
teens = %w[Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen]
tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
thousands = ['', 'Thousand', 'Million', 'Billion']
return 'Zero' if num.zero?
stack = Stack.new
group = 0
while num.positive?
digit3 = num % 10
num /= 10
digit2 = num % 10
num /= 10
digit1 = num % 10
num /= 10
if (digit3 * 100 + digit2 * 10 + digit1).positive?
stack.push thousands[group]
end
if digit2 == 1
stack.push(teens[digit3])
else
stack.push(ones[digit3])
stack.push(tens[digit2])
end
if digit1.positive?
stack.push 'Hundred'
stack.push ones[digit1]
end
group += 1
end
output = ''
until stack.empty?
element = stack.pop
output = output + ' ' + element unless element.empty?
end
output.strip
end
p number_to_words(101)