From 6651a34183dc86ea59cf0fe2d7e00491bdc98919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Sun, 20 Aug 2023 18:39:42 +0200 Subject: [PATCH] Add helper function to print numbers as ordinal numerals (#1405) --- src/AbstractAlgebra.jl | 3 +- src/{Pluralize.jl => PrintHelper.jl} | 37 ++++++++++++++++++++++ test/AbstractAlgebra-test.jl | 2 +- test/Pluralize-test.jl | 24 --------------- test/PrintHelper-test.jl | 46 ++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 26 deletions(-) rename src/{Pluralize.jl => PrintHelper.jl} (80%) delete mode 100644 test/Pluralize-test.jl create mode 100644 test/PrintHelper-test.jl diff --git a/src/AbstractAlgebra.jl b/src/AbstractAlgebra.jl index 30c8ea81f6..ed1a7c353d 100644 --- a/src/AbstractAlgebra.jl +++ b/src/AbstractAlgebra.jl @@ -261,6 +261,7 @@ export NotImplementedError export NotInvertibleError export NumField export NumFieldElem +export ordinal_number_string export parent_type export pluralize export PolyRing @@ -292,7 +293,7 @@ export ZZ include("Attributes.jl") include("AliasMacro.jl") -include("Pluralize.jl") +include("PrintHelper.jl") # alternative names for some functions from Base export is_empty, is_even, is_equal, is_finite, is_inf, is_integer, is_less, is_odd, is_one, is_real, is_subset, is_valid, is_zero diff --git a/src/Pluralize.jl b/src/PrintHelper.jl similarity index 80% rename from src/Pluralize.jl rename to src/PrintHelper.jl index 5a79da8c0d..2bac10f0e6 100644 --- a/src/Pluralize.jl +++ b/src/PrintHelper.jl @@ -109,3 +109,40 @@ function Base.show(io::IO, quantity::ItemQuantity) print(io, pluralize(quantity.noun)) end end + +""" + ordinal_number_string(number::Int) + +A helper function which returns `number` as a string in ordinal form. + +# Examples +```julia +julia> ordinal_number_string(1) +"1st" + +julia> ordinal_number_string(2) +"2nd" + +julia> ordinal_number_string(3) +"3rd" + +julia> ordinal_number_string(4) +"4rd" +``` +""" +function ordinal_number_string(number::Int) + number >= 0 || error("number must be non-negative") + mod100 = mod(number, 100) + mod10 = mod(number, 10) + if mod100 in 11:13 + return "$(number)th" + elseif mod10 == 1 + return "$(number)st" + elseif mod10 == 2 + return "$(number)nd" + elseif mod10 == 3 + return "$(number)rd" + else + return "$(number)th" + end +end diff --git a/test/AbstractAlgebra-test.jl b/test/AbstractAlgebra-test.jl index 41c265bf07..0e9a842bc8 100644 --- a/test/AbstractAlgebra-test.jl +++ b/test/AbstractAlgebra-test.jl @@ -14,5 +14,5 @@ include("Maps-test.jl") include("Benchmark-test.jl") include("Factor-test.jl") include("PrettyPrinting-test.jl") -include("Pluralize-test.jl") +include("PrintHelper-test.jl") include("misc-test.jl") diff --git a/test/Pluralize-test.jl b/test/Pluralize-test.jl deleted file mode 100644 index 6708c691c1..0000000000 --- a/test/Pluralize-test.jl +++ /dev/null @@ -1,24 +0,0 @@ -import AbstractAlgebra: pluralize, ItemQuantity - -@testset "pluralize" begin - @test pluralize("basis") == "bases" - @test pluralize("child") == "children" - @test pluralize("generator") == "generators" - @test pluralize("indeterminate") == "indeterminates" - @test pluralize("matrix") == "matrices" - @test pluralize("maximum") == "maxima" - @test pluralize("polyhedron") == "polyhedra" - @test pluralize("tetrahedron") == "tetrahedra" - @test pluralize("variable") == "variables" - @test pluralize("variety") == "varieties" - @test pluralize("vertex") == "vertices" -end - -@testset "ItemQuantity" begin - @test string(ItemQuantity(0, "generator")) == "0 generators" - @test string(ItemQuantity(1, "generator")) == "1 generator" - @test string(ItemQuantity(2, "generator")) == "2 generators" - @test string(ItemQuantity(0, "ox", "oxen")) == "0 oxen" - @test string(ItemQuantity(1, "ox", "oxen")) == "1 ox" - @test string(ItemQuantity(2, "ox", "oxen")) == "2 oxen" -end diff --git a/test/PrintHelper-test.jl b/test/PrintHelper-test.jl new file mode 100644 index 0000000000..a95111db8d --- /dev/null +++ b/test/PrintHelper-test.jl @@ -0,0 +1,46 @@ +@testset "pluralize" begin + @test pluralize("basis") == "bases" + @test pluralize("child") == "children" + @test pluralize("generator") == "generators" + @test pluralize("indeterminate") == "indeterminates" + @test pluralize("matrix") == "matrices" + @test pluralize("maximum") == "maxima" + @test pluralize("polyhedron") == "polyhedra" + @test pluralize("tetrahedron") == "tetrahedra" + @test pluralize("variable") == "variables" + @test pluralize("variety") == "varieties" + @test pluralize("vertex") == "vertices" +end + +@testset "ItemQuantity" begin + @test string(ItemQuantity(0, "generator")) == "0 generators" + @test string(ItemQuantity(1, "generator")) == "1 generator" + @test string(ItemQuantity(2, "generator")) == "2 generators" + @test string(ItemQuantity(0, "ox", "oxen")) == "0 oxen" + @test string(ItemQuantity(1, "ox", "oxen")) == "1 ox" + @test string(ItemQuantity(2, "ox", "oxen")) == "2 oxen" +end + +@testset "ordinal_number_string" begin + @test ordinal_number_string(0) == "0th" + @test ordinal_number_string(1) == "1st" + @test ordinal_number_string(2) == "2nd" + @test ordinal_number_string(3) == "3rd" + @test ordinal_number_string(4) == "4th" + @test ordinal_number_string(5) == "5th" + @test ordinal_number_string(10) == "10th" + @test ordinal_number_string(11) == "11th" + @test ordinal_number_string(12) == "12th" + @test ordinal_number_string(13) == "13th" + @test ordinal_number_string(14) == "14th" + @test ordinal_number_string(20) == "20th" + @test ordinal_number_string(21) == "21st" + @test ordinal_number_string(22) == "22nd" + @test ordinal_number_string(23) == "23rd" + @test ordinal_number_string(24) == "24th" + @test ordinal_number_string(100) == "100th" + @test ordinal_number_string(101) == "101st" + @test ordinal_number_string(102) == "102nd" + @test ordinal_number_string(103) == "103rd" + @test ordinal_number_string(104) == "104th" +end