In this post, we will see the solution of the Python Coding Question which is already asked in the TCS NQT Exam.
Problem Statement: Airport security team have confiscated several items of the passengers at the security check point. All the items have been dropped into a box (array). Each item possesses a certain amount of risk items [0,1,2]. Here, the risk severity of the items represents an array [] of N number of integral values. The task here is to sort the items based on their levels of risk in the array. The risk values range from 0 to 2.
Example :
Input: 6 -> Value of N [1,0,2,0,1,0]-> Element of arr[0] to arr[N-1], while input each element is separated by space. Output : 0 0 0 1 1 2 -> Element after sorting based on risk severity Example 2: input : 9 -> Value of N [2,1,0,2,1,0,0,1,2] -> Element of arr[0] to arr[N-1], while input each element is separated by a space. Output: 0 0 0 1 1 1 2 2 2 ->Elements after sorting based on risk severity. Explanation: In the above examples, the input is an array of size N consisting of only 0’s, 1’s and 2s. The output is a sorted array from 0 to 2 based on the risk severity.
Solution:
# taking inputs N = int(input()) risk_array = list(map(int,input().strip().split())) # sorted the list in ascending order risk_array.sort() # printing the outputs for risk in risk_array: print(risk, end=" ")
Output:
6 [1,0,2,0,1,0] 0 0 0 1 1 2