Four is magic

From Rosetta Code
Revision as of 17:19, 28 September 2017 by Thundergnat (talk | contribs) (sigh, another typo)
Four is magic is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Four is magic.

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.

 Three is five, five is four, four is magic.

For reference, here are outputs for 0 through 9.

 Zero is four, four is magic.
 One is three, three is five, five is four, four is magic.
 Two is three, three is five, five is four, four is magic.
 Three is five, five is four, four is magic.
 Four is magic.
 Five is four, four is magic.
 Six is three, three is five, five is four, four is magic.
 Seven is five, five is four, four is magic.
 Eight is five, five is four, four is magic.
 Nine is four, four is magic.


Some task guidelines
  • You may assume the input will only contain integer numbers.
  • Cardinal numbers between 20 and 100 may use either hyphens or spaces as word separators but they must use a word separator. (23 is twenty three or twenty-three not twentythree.)
  • Cardinal number conversions should follow the English short scale. (billion is 1e9, trillion is 1e12, etc.)
  • Cardinal numbers should not include commas. (20140 is twenty thousand one hundred forty not twenty thousand, one hundred forty.)
  • When converted to a string, 100 should be one hundred, not a hundred or hundred, 1000 should be one thousand, not a thousand or thousand.
  • When converted to a string, there should be no and in the cardinal string. 130 should be one hundred thirty not one hundred and thirty.
  • When counting characters, count all of the characters in the cardinal number including spaces and hyphens. One hundred fifty-one should be 21 not 18.
  • The output should follow the format "N is K, K is M, M is ... four is magic." (unless the input is 4, in which case the output should simply be "four is magic.")
  • The output can either be the return value from the function, or be displayed from within the function.
  • You are encouraged, though not mandated to use proper sentence capitalization.
  • You may optionally support negative numbers. -7 is negative seven.
  • Show the output here for a small representative sample of values, at least 5 but no more than 25. You are free to chose which which numbers to use for output demonstration.

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.

Kotlin

This uses the code I wrote for the Number names task, appropriately adjusted to deal with this task. Input is limited to signed 64 bit integers as Kotlin doesn't currently support unsigned types. <lang scala>// version 1.1.4-3

val names = mapOf(

   1 to "one",
   2 to "two",
   3 to "three",
   4 to "four",
   5 to "five",
   6 to "six",
   7 to "seven",
   8 to "eight",
   9 to "nine",
   10 to "ten",
   11 to "eleven",
   12 to "twelve",
   13 to "thirteen",
   14 to "fourteen",
   15 to "fifteen",
   16 to "sixteen",
   17 to "seventeen",
   18 to "eighteen",
   19 to "nineteen",
   20 to "twenty",
   30 to "thirty",
   40 to "forty",
   50 to "fifty",
   60 to "sixty",
   70 to "seventy",
   80 to "eighty",
   90 to "ninety"

) val bigNames = mapOf(

   1_000L to "thousand",
   1_000_000L to "million",
   1_000_000_000L to "billion",
   1_000_000_000_000L to "trillion",
   1_000_000_000_000_000L to "quadrillion",
   1_000_000_000_000_000_000L to "quintillion"

)

fun numToText(n: Long): String {

   if (n == 0L) return "zero"
   val neg = n < 0L
   val maxNeg = n == Long.MIN_VALUE
   var nn = if (maxNeg) -(n + 1) else if (neg) -n else n
   val digits3 = IntArray(7)
   for (i in 0..6) {  // split number into groups of 3 digits from the right
       digits3[i] = (nn % 1000).toInt()
       nn /= 1000
   }
   fun threeDigitsToText(number: Int) : String {
       val sb = StringBuilder()
       if (number == 0) return ""
       val hundreds = number / 100
       val remainder = number % 100
       if (hundreds > 0) {
           sb.append(names[hundreds], " hundred")
           if (remainder > 0) sb.append(" ")
       }
       if (remainder > 0) {
           val tens = remainder / 10
           val units = remainder % 10
           if (tens > 1) {
               sb.append(names[tens * 10])
               if (units > 0) sb.append("-", names[units])
           }
           else sb.append(names[remainder])
       }
       return sb.toString()
   }
   val strings = Array<String>(7) { threeDigitsToText(digits3[it]) }
   var text = strings[0]
   var big = 1000L
   for (i in 1..6) {
       if (digits3[i] > 0) {
           var text2 = strings[i] + " " + bigNames[big]
           if (text.length > 0) text2 += " "
           text = text2 + text
       }
       big *= 1000
   }
   if (maxNeg) text = text.dropLast(5) + "eight"
   if (neg) text = "negative " + text
   return text

}

fun fourIsMagic(n: Long): String {

   if (n == 4L) return "Four is magic."
   var text = numToText(n).capitalize()
   val sb = StringBuilder()
   while (true) {
       val len = text.length.toLong()
       if (len == 4L) return sb.append("$text is four, four is magic.").toString()
       val text2 = numToText(len)
       sb.append("$text is $text2, ")
       text = text2
   }

}

fun main(args: Array<String>) {

   val la = longArrayOf(0, 4, 6, 11, 13, 75, 100, 337, -164, 9_223_372_036_854_775_807L)
   for (i in la) {
       println(fourIsMagic(i))
       println()
   }

}</lang>

Output:
Zero is four, four is magic.

Four is magic.

Six is three, three is five, five is four, four is magic.

Eleven is six, six is three, three is five, five is four, four is magic.

Thirteen is eight, eight is five, five is four, four is magic.

Seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic.

One hundred is eleven, eleven is six, six is three, three is five, five is four, four is magic.

Three hundred thirty-seven is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.

Negative one hundred sixty-four is thirty-one, thirty-one is ten, ten is three, three is five, five is four, four is magic.

Nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven is one hundred ninety-six, one hundred ninety-six is twenty-two, twenty-two is ten, ten is three, three is five, five is four, four is magic.

Perl 6

Works with: Rakudo version 2017.09

Lingua::EN::Numbers::Cardinal module available from the [Perl 6 ecosystem].

<lang perl6>use Lingua::EN::Numbers::Cardinal;

sub card ($n) { cardinal($n).subst(/','/, , :g) }

sub magic (Int $int is copy) {

   my $string;
   loop {
      $string ~= "{ card($int) } is ";
      if $int = ($int == 4) ?? 0 !! card($int).chars {
          $string ~= "{ card($int) }, ";
      } else {
          $string ~= "magic.\n";
          last;
      }
  }
  $string.tc

};

.&magic.say for 0, 4, 6, 11, 13, 75, 337, -164, 9876543209, 2**256;</lang>

Output:
Zero is four, four is magic.

Four is magic.

Six is three, three is five, five is four, four is magic.

Eleven is six, six is three, three is five, five is four, four is magic.

Thirteen is eight, eight is five, five is four, four is magic.

Seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic.

Three hundred thirty-seven is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.

Negative one hundred sixty-four is thirty-one, thirty-one is ten, ten is three, three is five, five is four, four is magic.

Nine billion eight hundred seventy-six million five hundred forty-three thousand two hundred nine is ninety-seven, ninety-seven is twelve, twelve is six, six is three, three is five, five is four, four is magic.

One hundred fifteen quattuorvigintillion seven hundred ninety-two trevigintillion eighty-nine duovigintillion two hundred thirty-seven unvigintillion three hundred sixteen vigintillion one hundred ninety-five novemdecillion four hundred twenty-three octodecillion five hundred seventy septendecillion nine hundred eighty-five sexdecillion eight quindecillion six hundred eighty-seven quattuordecillion nine hundred seven tredecillion eight hundred fifty-three duodecillion two hundred sixty-nine undecillion nine hundred eighty-four decillion six hundred sixty-five nonillion six hundred forty octillion five hundred sixty-four septillion thirty-nine sextillion four hundred fifty-seven quintillion five hundred eighty-four quadrillion seven trillion nine hundred thirteen billion one hundred twenty-nine million six hundred thirty-nine thousand nine hundred thirty-six is eight hundred sixty-nine, eight hundred sixty-nine is twenty-four, twenty-four is eleven, eleven is six, six is three, three is five, five is four, four is magic.

zkl

Translation of: Perl6

Limitiation: zkl only has 64 bit signed integars.

Uses the nth function from Spelling_of_ordinal_numbers#zkl

<lang zkl>fcn fourIsMagic(int){

  if(int==0) return("Zero is four, four is magic.");
  string:="";
  while(1){ c:=nth(int,False);
     string+="%s is ".fmt(c);
     if(int = ( if(int==4) 0 else c.len() )){

string+="%s, ".fmt(nth(int,False));

     }else{
        string+="magic.";

break;

     }
  }
  string[0].toUpper() + string[1,*]

}</lang> <lang zkl>foreach n in (T(0,4,6,11,13,75,337,-164,9876543209)){

  println(fourIsMagic(n),"\n")

}</lang>

Output:
Zero is four, four is magic.

Four is magic.

Six is three, three is five, five is four, four is magic.

Eleven is six, six is three, three is five, five is four, four is magic.

Thirteen is eight, eight is five, five is four, four is magic.

Seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic.

Three hundred thirty-seven is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.

Negative one hundred sixty-four is thirty-one, thirty-one is ten, ten is three, three is five, five is four, four is magic.

Nine billion eight hundred seventy-six million five hundred forty-three thousand two hundred nine is ninety-seven, ninety-seven is twelve, twelve is six, six is three, three is five, five is four, four is magic.