Question

In: Computer Science

In python please :) How to get the n th smallest even value of given array?...

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]

Solutions

Expert Solution

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])


Related Solutions

Python - You are given a sorted (from smallest to largest) array A of n distinct...
Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.
MUST BE SOLVED USING [R] How to get the nth largest even value of given array?...
MUST BE SOLVED USING [R] How to get the nth largest even value of given array? (Use while loop and if-statement for this problem. Do not use existing codes. Use your own codes). DO NOT USE ANY KINDS OF SORT FUNCTIONS. For example, in given list [10,36,58,31,56,77,43,12,65,19], if n is defined as 2. The program will print 56.
find the k-th smallest value given array[ 5, 2, 1, 15, 6, 9, 3, 4, 11]...
find the k-th smallest value given array[ 5, 2, 1, 15, 6, 9, 3, 4, 11] , k=2, the algorithm return the first two value: 1, 2 please show steps of how to output the first 2 values by using merge sort and please analyze the running time. your time is greatly appreciated.
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Python please: Given a 1D array with blank as non-obstacles and * as an obstacle, the...
Python please: Given a 1D array with blank as non-obstacles and * as an obstacle, the Dino can either traverse 1 at a time or jump minJump to maxJump spaces, can the Dino make it from the start of the array to end? [ * , , , ] regardless of input False (Dino dies on index 0) [ , , , , *] regardless of input False (Dine can never reach the end) [] True (Dino has nothing to...
It is given that the effective rate of interest for the n-th year period is in...
It is given that the effective rate of interest for the n-th year period is in = 0.01 + e^(−n) (a) Find a(t) for t being an integer. (b) If the principal is $100, find the total amount of interest earned in year 3, 4 and 5.
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
Given an array with data 3, 6, 4, 1, 5, 2, 6, 5, 3, 7, 4 using random select to find the 9 th smallest number
Given an array with data 3, 6, 4, 1, 5, 2, 6, 5, 3, 7, 4 using random select to find the 9 th smallest number (use the last element in each sequence as pivot). Show the intermediate steps (the result of each recursive step including the pivot, k’s value and grouping).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT