In: Computer Science
The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array
{ 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 }
then your code should set ordinals to the array
{ "1st", "2nd", "3rd", "4th", "5th", "10th", "11th", "12th", "13th", "21st", "22nd", "973rd", "1000th" }.
Test Cases
Test case #1
Expected result: ordinals is {"21st","22nd","973rd","1000th"}
Test case #2
Expected result: ordinals is {"1st","2nd","3rd","4th","5th"}
Test case #3
Expected result: ordinals is { "52nd", "11th", "74th", "78th", "27th" }
Test case #4
Expected result: ordinals is { "81st", "78th", "911th", "173rd", "61st" }
SINCE THE LANGUAGE IS NOT SPECIFIED, I HAVE CREATED THE PROGRAM IN PYTHON.
FOLLOWING CODE IN PYTHON WORKS FINE
#two empty lists L1 containing all numbers between 1 and
1000
#L2 containing numbers with thier suffixes
L1=[]
L2=[]
for I in range(1,1001):
L1.append(I)
S=""
L2=[]
for I in range(0,1000):
R=int((L1[I])%10)
S=""
if R==1:
S=str(L1[I])+"st"
elif R==2:
S=str(L1[I])+"nd"
elif R==3:
S=str(L1[I])+"rd"
else:
S=str(L1[I])+"th"
L2.append(S)
#creating dictionary with Keys as numbers and values as numbers
with suffixes
D1 = {}
for key in L1:
for value in L2:
D1[key] = value
L2.remove(value)
break
#taking user input to store in L3
L3=[]
N=int(input("How many numbers to see their ordinals?"))
for I in range(N):
X=int(input("Enter a number"))
L3.append(X)
for I in L3:
for Z in D1:
if I==Z:
print(D1[Z])
----------------------------------------------------------------------------------
OUTPUT: