Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
59
Task/Four-is-magic/Crystal/four-is-magic.cr
Normal file
59
Task/Four-is-magic/Crystal/four-is-magic.cr
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
struct Int
|
||||
THOU_PREFIXES = %w(undec dec non oct sept sext quint quadr tr b m)
|
||||
.map(&.+("illion")) + ["thousand", ""]
|
||||
TENS = ["", ""] + %w(twen thir for fif six seven eigh nine).map &.+("ty")
|
||||
ONES = [""] + %w(one two three four five six seven eight nine ten) +
|
||||
%w(eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen)
|
||||
def to_words
|
||||
raise "Number to big to express in words" unless self.abs <= Int128::MAX
|
||||
return "zero" if self == 0
|
||||
String.build do |s|
|
||||
n = self
|
||||
if n < 0
|
||||
s << "negative "
|
||||
n = n.abs
|
||||
end
|
||||
ths = Array(Int128).new(THOU_PREFIXES.size, 0)
|
||||
(0...ths.size).reverse_each do |idx|
|
||||
n, ths[idx] = n.divmod(1000)
|
||||
end
|
||||
ths.zip(THOU_PREFIXES).each do |th, pref|
|
||||
next if th == 0
|
||||
hundreds, rest = th.divmod 100
|
||||
s << ONES[hundreds] << " hundred " if hundreds > 0
|
||||
if rest < 20
|
||||
s << ONES[rest] << " " if rest > 0
|
||||
else
|
||||
tens, ones = rest.divmod 10
|
||||
s << TENS[tens]
|
||||
s << "-" << ONES[ones] if ones > 0
|
||||
s << " "
|
||||
end
|
||||
s << pref << " " if pref != ""
|
||||
end
|
||||
s.chomp! 32u8
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def sequence (io, n)
|
||||
words = n.to_words.capitalize
|
||||
loop do
|
||||
io << words << " is "
|
||||
break if n == 4
|
||||
n = words.size
|
||||
words = n.to_words
|
||||
io << words << ", "
|
||||
end
|
||||
io << "magic."
|
||||
end
|
||||
|
||||
def sequence (n)
|
||||
String.build do |s|
|
||||
sequence s, n
|
||||
end
|
||||
end
|
||||
|
||||
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209, Int128::MAX].each do |start|
|
||||
puts "- " + sequence(start)
|
||||
end
|
||||
18
Task/Four-is-magic/Pluto/four-is-magic.pluto
Normal file
18
Task/Four-is-magic/Pluto/four-is-magic.pluto
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
local function four_is_magic(n)
|
||||
local s = fmt.spell(n):upper(1)
|
||||
if n < 0 then s = s:replace("Minus", "Negative") end
|
||||
local t = s
|
||||
while n != 4 do
|
||||
n = #s
|
||||
s = fmt.spell(n)
|
||||
t ..= " is " .. s .. ", " .. s
|
||||
end
|
||||
return t .. " is magic"
|
||||
end
|
||||
|
||||
for {0, 4, 6, 11, 13, 75, 100, 337, -164, 9007199254740991} as n do
|
||||
print(four_is_magic(n))
|
||||
print()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue