Skip to main content
  1. About
  2. For Teams
Asked
Viewed 1k times
0

From a given array, extract all the elements which are greater than 'm' and less than 'n'. Note: 'm' and 'n' are integer values provided as input.

Input format:

A list of integers on line one

Integer 'm' on line two

Integer 'n' on line three

Output format:

1-D array containing integers greater than 'm' and smaller than 'n'.

Sample input:

[ 1, 5, 9, 12, 15, 7, 12, 9 ] (array)

6 (m)

12 (n)

Sample output:

[ 9 7 9 ]

Sample input:

[ 1, 5, 9, 12, 15, 7, 12, 9 ]

12

6

Sample output:

[ ]

2
  • Hi and welcome to SO. It is important for the community that you also demonstrate that you are working to solve your issue. The best way to do that is to include the text version of the source code you have tried so far.
    JonSG
    –  JonSG
    2021-09-01 17:33:44 +00:00
    Commented Sep 1, 2021 at 17:33
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    Community
    –  Community Bot
    2021-09-04 17:57:55 +00:00
    Commented Sep 4, 2021 at 17:57

2 Answers 2

1

This should do the job.

take 2 input in int format. then iterate over the given array and put a if condition like below :-

arr  = [ 1, 5, 9, 12, 15, 7, 12, 9 ]

max = int(input("Max : "))

min = int(input("Min : "))

output = []
for a in range(len(arr)):
    if arr[a] > max and arr[a] < min :
        print(arr[a])
        output.append(arr[a])
Sign up to request clarification or add additional context in comments.

2 Comments

Since you are iterating it's index, it should be arr[a] instead of a. if arr[a] > max and arr[a] < min : output.append(arr[a])
@sittsering : I agree, it should not be index, instead it should be arr element. Updated above
0

Make the array (not a list!)

In [857]: x = np.array([ 1, 5, 9, 12, 15, 7, 12, 9 ])

test against the lower bound:

In [858]: x>6
Out[858]: array([False, False,  True,  True,  True,  True,  True,  True])

and the upper:

In [859]: x<12
Out[859]: array([ True,  True,  True, False, False,  True, False,  True])

combine them:

In [860]: (x>6)&(x<12)
Out[860]: array([False, False,  True, False, False,  True, False,  True])

Use that as a boolean mask to select elements from x:

In [861]: mask = (x>6)&(x<12)
In [862]: x[mask]
Out[862]: array([9, 7, 9])

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.