How to resolve the algorithm Spelling of ordinal numbers step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Spelling of ordinal numbers step by step in the Go programming language

Table of Contents

Problem Statement

Ordinal numbers   (as used in this Rosetta Code task),   are numbers that describe the   position   of something in a list. It is this context that ordinal numbers will be used, using an English-spelled name of an ordinal number.

The ordinal numbers are   (at least, one form of them): sometimes expressed as:

For this task, the following (English-spelled form) will be used:

Furthermore, the short scale numbering system (i.e. 2,000,000,000 is two billion) will be used here. wp:Long and short scales 2,000,000,000   is two billion,   not   two milliard.

Write a driver and a function (subroutine/routine ···) that returns the English-spelled ordinal version of a specified number   (a positive integer). Optionally, try to support as many forms of an integer that can be expressed:   123   00123.0   1.23e2   all are forms of the same integer. Show all output here.

Use (at least) the test cases of:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Spelling of ordinal numbers step by step in the Go programming language

The provided code is a Go program that converts a number to its ordinal representation. An ordinal number is a number that indicates the position of something in a sequence, such as "first", "second", "third", and so on.

The program defines a map called irregularOrdinals that contains irregular ordinal forms for some numbers, such as "one" to "first" and "two" to "second". It also defines a function called sayOrdinal that takes a number as input and returns its ordinal representation.

The sayOrdinal function first calls another function called say to convert the number to its word form. It then checks if the last word in the word form is in the irregularOrdinals map, and if so, it replaces the last word with the corresponding ordinal form. If the last word is not in the map, the function checks if it ends with "y". If it does, it replaces the "y" with "ieth". Otherwise, it simply adds "th" to the end of the word.

The say function is a recursive function that converts a number to its word form. It works by breaking the number down into its individual digits and then combining the digits into words. For example, the number 123 would be broken down into the digits 1, 2, and 3, and then combined into the word "one hundred twenty-three".

The program demonstrates the sayOrdinal function by converting a list of numbers to their ordinal representations and printing the results.

Here is an example of the output of the program:

first
second
third
fourth
fifth
eleventh
sixty-fifth
one hundredth
one hundred first
two hundred seventy-second
twenty-three thousand four hundred fifty-sixth
eight quadrillion seventy trillion six hundred billion five million four hundred three

Source code in the go programming language

import (
	"fmt"
	"strings"
)

func main() {
	for _, n := range []int64{
		1, 2, 3, 4, 5, 11, 65, 100, 101, 272, 23456, 8007006005004003,
	} {
		fmt.Println(sayOrdinal(n))
	}
}

var irregularOrdinals = map[string]string{
	"one":    "first",
	"two":    "second",
	"three":  "third",
	"five":   "fifth",
	"eight":  "eighth",
	"nine":   "ninth",
	"twelve": "twelfth",
}

func sayOrdinal(n int64) string {
	s := say(n)
	i := strings.LastIndexAny(s, " -")
	i++
	// Now s[:i] is everything upto and including the space or hyphen
	// and s[i:] is the last word; we modify s[i:] as required.
	// Since LastIndex returns -1 if there was no space/hyphen,
	// `i` will be zero and this will still be fine.
	if x, ok := irregularOrdinals[s[i:]]; ok {
		s = s[:i] + x
	} else if s[len(s)-1] == 'y' {
		s = s[:i] + s[i:len(s)-1] + "ieth"
	} else {
		s = s[:i] + s[i:] + "th"
	}
	return s
}

// Below is a copy of https://rosettacode.org/wiki/Number_names#Go

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 Ultra useful primes step by step in the Perl programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the Tcl programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Necromantus programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the R programming language