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))
Sep 25, 2018 in Python by riyawilliams
• 160 points
1,325 views

1 answer to this question.

+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 27, 2018 by bug_seeker
• 15,510 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How to get all links and all elements that are clickable on a page of system dynamically?

Hello, You can do it by using find_elements_by_xpath() driver ...READ MORE

answered Aug 27, 2020 in Python by Niroj
• 82,840 points
5,499 views
0 votes
1 answer

How to check the value is double in the existing data that when ever the values are double print thats value

Hey, @There, I guess regarding your query you ...READ MORE

answered Nov 3, 2020 in Python by Gitika
• 65,770 points
859 views
0 votes
1 answer

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

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

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
867 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
694 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,463 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP