In: Computer Science
In python please :)
How to get the n th smallest even value of given array? (Use for loop for this problem. Do not use existing codes. Use your own codes). For example, in given list [11,23,58,31,56,22,43,12,65,19], if n is defined as 3. The program will print 56. (By using for loop and obtain the set of evens. By using another for loop, from the set of evens remove (n-1) observations and break the loop and find the minimum observation of the updated list). You can use the remove function to remove the observations from a list. remove function can be used as in the below example: A=[12, 41 ,5, 16,32,54 ] # List a is given # If you want to remove number 16 from the list A , You can use below code A.remove(15) # Now A=[12, 41, 5, 32, 54] # if you also want to remove 5 from the list A A.remove(5) # Now A=[12, 41, 32, 54]
Python Code
size = int(input("How many elements to be inserted in list :
"))
lists = []
# add all elements in list
for i in range(0,size):
a = int(input("Enter element : "))
lists.append(a)
i=0
n = int(input("Which n smallest element you want to display :
"))
# remove all odd element from list
while (i<len(lists)):
if lists[i]%2==1:
lists.remove(lists[i])
else:
i=i+1
# sort all remaining even elements
lists.sort()
# display nth smallest element
print(lists[n-1])