How to resolve the algorithm Four is magic step by step in the q programming language
How to resolve the algorithm Four is magic step by step in the q programming language
Table of Contents
Problem Statement
Write a subroutine, function, whatever it may be called in your language, that takes an integer number and returns an English text sequence starting with the English cardinal representation of that integer, the word 'is' and then the English cardinal representation of the count of characters that made up the first word, followed by a comma. Continue the sequence by using the previous count word as the first word of the next phrase, append 'is' and the cardinal count of the letters in that word. Continue until you reach four. Since four has four characters, finish by adding the words 'four is magic' and a period. All integers will eventually wind up at four. For instance, suppose your are given the integer 3. Convert 3 to Three, add is , then the cardinal character count of three, or five, with a comma to separate if from the next phrase. Continue the sequence five is four, (five has four letters), and finally, four is magic. For reference, here are outputs for 0 through 9.
You can choose to use a library, (module, external routine, whatever) to do the cardinal conversions as long as the code is easily and freely available to the public.
If you roll your own, make the routine accept at minimum any integer from 0 up to 999999. If you use a pre-made library, support at least up to unsigned 64 bit integers. (or the largest integer supported in your language if it is less.)
Four is magic is a popular code-golf task. This is not code golf. Write legible, idiomatic and well formatted code.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Four is magic step by step in the q programming language
Source code in the q programming language
C:``one`two`three`four`five`six`seven`eight`nine`ten,
`eleven`twelve`thirteen`fourteen`fifteen`sixteen`seventeen`eighteen`nineteen / cardinal numbers <20
T:``ten`twenty`thirty`forty`fifty`sixty`seventy`eighty`ninety / tens
M:``thousand`million`billion`trillion`quadrillion`quintillion`sextillion`septillion / magnitudes
st:{ / stringify <1000
$[x<20; C x;
x<100; (T;C)@'10 vs x;
{C[y],`hundred,$[z=0;`;x z]}[.z.s] . 100 vs x] }
s:{$[x=0; "zero"; {" "sv string except[;`]raze x{$[x~`;x;x,y]}'M reverse til count x} st each 1000 vs x]} / stringify
fim:{@[;0;upper],[;"four is magic.\n"] raze 1_{y," is ",x,", "}prior s each(count s@)\[x]} / four is magic
1 raze fim each 0 4 8 16 25 89 365 2586 25865 369854 40000000001; / tests
q)show sl:count each string C / string lengths
0 3 3 5 4 4 3 5 5 4 3 6 6 8 8 7 7 9 8 8
q)sl\[18]
18 8 5 4
q)sl\[19]
19 8 5 4
You may also check:How to resolve the algorithm Pascal's triangle step by step in the 8th programming language
You may also check:How to resolve the algorithm Program name step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the Frink programming language
You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the J programming language
You may also check:How to resolve the algorithm Create a file step by step in the Lua programming language