Counting integers that are within intervals

+4 votes

I am looking for a more efficient way than brute-forcing my way through in the following problem in python 3.

Problem statement:

Input:

  • An array of n integers, scores, where each score is denoted by scores_j

  • An array of q integers, lowerLimits, where each lowerLimits_i denotes the lower limit for score range i.

  • An array of q integers, upper limits, where each upperLimits_i denotes the upper limit for score range i.

Output: A function that returns an array of Q integers where the value at each index I denotes the number of integers that are in the inclusive range [lowerLimits_i, upperLimits_i].

Constraints:

  • 1 ≤ n ≤ 1e5
  • 1 ≤ scores_j ≤ 1e9
  • 1 ≤ q ≤ 1e5
  • 1 ≤ lowerLimits_i ≤ upperLimits_i ≤ 1e9

Example: Given scores= [5, 8, 7], lowerLimits = [3, 7], and upperLimits = [9, 7] I want to check how many of the integers are contained in each interval (inclusive). In this examples: intervals are [3,9] and [7,7], and the result would be [3, 1].

My code looks like this:

def check(scores, lowerLimits, upperLimits):
   res = []
   for l, u in zip(lowerLimits, upperLimits):
       res.append(sum([l <= y <= u for y in scores]))
   return res
if __name__ == "__main__":
   scores= [5, 8, 7]
   lowerLimits = [3, 7]
   upperLimits = [9, 7]

   print(check(scores, lowerLimits, upperLimits))


Regards,
Riya williams.
python developer
Sep 25, 2018 in Python by riyawilliams
• 160 points
33 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+1 vote
Am not sure as this would give a better TC but still what you can try is

First, sort the values
Second, iterate over the sorted list and find the lower hand limit and keep a count till the upper limit
That how you get the count of all the values and the remaining values that are not in the range can be discarded.
answered Sep 26, 2018 by bug_seeker
• 14,970 points

Related Questions In Python

0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,050 points
17 views
0 votes
1 answer

Run Unix command within Python

From the python docs, use the subprocess ...READ MORE

answered Apr 27, 2018 in Python by Nietzsche's daemon
• 4,260 points
11 views
0 votes
1 answer

What are the differences between type() and isinstance()?

To summarize the contents of other (already ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
17 views
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14 in Python by Shashank
• 1,350 points
35 views
0 votes
1 answer

What are the key features of Python?

If it makes for an introductory language ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 56,120 points
13 views
0 votes
1 answer

Generate random integers between 0 and 9

Try: from random import randint print(randint(0, 9)) More info: ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 56,120 points
16 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
23 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.