Question

In: Computer Science

The array s of ints contain integers each of which is between 1 and 1000 (inclusive)....

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" }

Solutions

Expert Solution

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:


Related Solutions

Java Language The array s of ints contain integers each of which is between 1 and...
Java Language 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",...
For each question below, calculate the number of four-digit integers (1000 through 9999 inclusive; the _rst...
For each question below, calculate the number of four-digit integers (1000 through 9999 inclusive; the _rst digit cannot be 0) that satisfy the specified property: 1. How many four-digit integers are even (for example, 2102 and 8162, but NOT 2001)? 2. How many four-digit integers are consisted of four distinct digits in strictly decreasing order (for example, 9621 and 8742, but NOT 1352)? (Hint: combination problem) 3. How many four-digit integers are consisted of two pairs of distinct digits (for...
For each question below, calculate the number of four-digit integers (1000 through 9999 inclusive; the first...
For each question below, calculate the number of four-digit integers (1000 through 9999 inclusive; the first digit cannot be 0) that satisfy the specified property: 1) How many four-digit integers are even (for example, 2102 and 8162, but NOT 2001)? 2) How many four-digit integers are consisted of four distinct digits in strictly decreasing order (for example, 9621 and 8742, but NOT 1352)? (Hint: combination problem) 3) How many four-digit integers are consisted of two pairs of distinct digits (for...
This is JAVA Return the centered average of an array of ints, which we'll say is...
This is JAVA Return the centered average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array has a length of 3 or more. 1 public int centeredAverage(int[] nums) 2 { 3...
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Find the number of integers between 100 and 1000 that are
Find the number of integers between 100 and 1000 that are (i) divisible by 7  (ii) not divisible by 7      
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
Question#1 How many positive integers between 100 and 888 inclusive, a) are divisible by 7? b)...
Question#1 How many positive integers between 100 and 888 inclusive, a) are divisible by 7? b) are odd? c) have distinct digits? d) are not divisible by 6? e) are divisible by either 4 or 7? f) are not divisible by either 4 or 7? g) are divisible by 4 but not by 7? h) are divisible by 4 and 7? Question#1 How many positive integers between 100 and 888 inclusive, a) are divisible by 7? b) are odd? c)...
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive)....
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive). Note: the tests call your function 1000 times to verify that no results are <0 or >1000. Rarely, this can score an incorrect function as correct.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT