How to resolve the algorithm Number names step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Number names step by step in the Racket programming language

Table of Contents

Problem Statement

Show how to spell out a number in English. You can use a preexisting implementation or roll your own, but you should support inputs up to at least one million (or the maximum value of your language's default bounded integer type, if that's less). Support for inputs other than positive integers (like zero, negative integers, and floating-point numbers) is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Number names step by step in the Racket programming language

Source code in the racket programming language

#lang racket

(define smalls
  (map symbol->string
       '(zero one two three four five six seven eight nine ten eleven twelve
         thirteen fourteen fifteen sixteen seventeen eighteen nineteen)))

(define tens
  (map symbol->string
       '(zero ten twenty thirty forty fifty sixty seventy eighty ninety)))

(define larges
  (map symbol->string
       '(thousand million billion trillion quadrillion quintillion sextillion
         septillion octillion nonillion decillion undecillion duodecillion
         tredecillion quattuordecillion quindecillion sexdecillion
         septendecillion octodecillion novemdecillion vigintillion)))

(define (integer->english n)
  (define (step div suffix separator [subformat integer->english])
    (define-values [q r] (quotient/remainder n div))
    (define S (if suffix (~a (subformat q) " " suffix) (subformat q)))
    (if (zero? r) S (~a S separator (integer->english r))))
  (cond [(< n 0) (~a "negative " (integer->english (- n)))]
        [(< n 20) (list-ref smalls n)]
        [(< n 100) (step 10 #f "-" (curry list-ref tens))]
        [(< n 1000) (step 100 "hundred" " and ")]
        [else (let loop ([N 1000000] [D 1000] [unit larges])
                (cond [(null? unit)
                       (error 'integer->english "number too big: ~e" n)]
                      [(< n N) (step D (car unit) ", ")]
                      [else (loop (* 1000 N) (* 1000 D) (cdr unit))]))]))

(for ([n 10])
  (define e (expt 10 n))
  (define r (+ (* e (random e)) (random e)))
  (printf "~s: ~a\n" r (integer->english r)))


  

You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the XPL0 programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Forth programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Maple programming language
You may also check:How to resolve the algorithm Curzon numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Substitution cipher step by step in the Java programming language