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

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Number names step by step in the Go 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 Go programming language

The Go program defines a function say that converts a 64-bit integer n into its English representation. It handles negative values, numbers less than 20 (using small), tens and units (using tens), hundreds (using small and "hundred"), and large numbers by iterating through thousands, millions, billions, etc. (using illions). The program initializes slices small and tens with predefined English words and illions with number scales. Here's a breakdown of the code:

package main: Defines the main package.

import "fmt": Imports the fmt package for input/output.

func main(): The program's entry point.

It iterates through a slice of int64 values [12, 1048576, 9e18, -2, 0] and prints the English representation of each number using fmt.Println(say(n)).

func say(n int64) string: Defines a function that takes an int64 value n and returns its English representation.

It initializes t as an empty string.

If n is negative, it sets t to "negative " and makes n positive (to handle math.MinInt64).

It uses a switch statement to handle different ranges of n:

If n is less than 20, it returns the corresponding word from the small slice. If n is less than 100, it returns the tens digit (tens slice) and the units digit (small slice). If n is less than 1000, it returns the hundreds digit (small slice) and the remaining digits as a string. If n is greater than or equal to 1000, it iterates through thousands, millions, billions, etc., and appends the representation of each thousand to sx. Finally, it returns the combined string t.

The program demonstrates converting large numbers into their English representations by handling negative numbers, single digits, tens and units, hundreds, and large numbers with scales.

Source code in the go programming language

package main

import "fmt"

func main() {
	for _, n := range []int64{12, 1048576, 9e18, -2, 0} {
		fmt.Println(say(n))
	}
}

var small = [...]string{"zero", "one", "two", "three", "four", "five", "six",
	"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
	"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
var tens = [...]string{"", "", "twenty", "thirty", "forty",
	"fifty", "sixty", "seventy", "eighty", "ninety"}
var illions = [...]string{"", " thousand", " million", " billion",
	" trillion", " quadrillion", " quintillion"}

func say(n int64) string {
	var t string
	if n < 0 {
		t = "negative "
		// Note, for math.MinInt64 this leaves n negative.
		n = -n
	}
	switch {
	case n < 20:
		t += small[n]
	case n < 100:
		t += tens[n/10]
		s := n % 10
		if s > 0 {
			t += "-" + small[s]
		}
	case n < 1000:
		t += small[n/100] + " hundred"
		s := n % 100
		if s > 0 {
			t += " " + say(s)
		}
	default:
		// work right-to-left
		sx := ""
		for i := 0; n > 0; i++ {
			p := n % 1000
			n /= 1000
			if p > 0 {
				ix := say(p) + illions[i]
				if sx != "" {
					ix += " " + sx
				}
				sx = ix
			}
		}
		t += sx
	}
	return t
}


  

You may also check:How to resolve the algorithm String case step by step in the M4 programming language
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Negative base numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm OpenGL step by step in the OCaml programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the JavaScript programming language