How to resolve the algorithm N'th step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm N'th step by step in the AWK programming language
Table of Contents
Problem Statement
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.
Returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th
Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs: 0..25, 250..265, 1000..1025
Note: apostrophes are now optional to allow correct apostrophe-less English.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm N'th step by step in the AWK programming language
Source code in the awk programming language
# syntax: GAWK -f NTH.AWK
BEGIN {
prn(0,25)
prn(250,265)
prn(1000,1025)
exit(0)
}
function prn(start,stop, i) {
printf("%d-%d: ",start,stop)
for (i=start; i<=stop; i++) {
printf("%d%s ",i,nth(i))
}
printf("\n")
}
function nth(yearday, nthday) {
if (yearday ~ /1[1-3]$/) { # 11th,12th,13th
nthday = "th"
}
else if (yearday ~ /1$/) { # 1st,21st,31st,etc.
nthday = "st"
}
else if (yearday ~ /2$/) { # 2nd,22nd,32nd,etc.
nthday = "nd"
}
else if (yearday ~ /3$/) { # 3rd,23rd,33rd,etc.
nthday = "rd"
}
else if (yearday ~ /[0456789]$/) { # 4th-10th,20th,24th-30th,etc.
nthday = "th"
}
return(nthday)
}
You may also check:How to resolve the algorithm Bulls and cows step by step in the J programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the Kotlin programming language