Finding number of positive numbers in list

odaiwa

I'm trying to find number of positive numbers in list using python and i want the code to run in O(log(n)) the list is of type Positive-Negative if there's negative number they will be in the right hand side of the list and all the positives is in the left side. for example if the input is :

lst=[2,6,0,1,-3,-19]

the output is

4

I've tried some methods but i didn't get the result that i wanted this is last form that i get until now:

def pos_neg(lst):
    if int(len(lst)) is 0 or (int(len(lst)) is 1 and lst[0] <0):
        return 0
    elif len(lst) is 1 and lst[0] >=0:
        return 1
    leng = (int(len(lst))) // 2
    counter = 0
    index = 0
    while(leng != 0):
        if lst[leng]  >=0 and lst[leng+1] <0:
            index = leng
            break
        if lst[leng] >=0 and lst[leng + 1] >=0:
            if int(len(lst)) < leng + int(len(lst)):
                return 0
            else:
                leng = (leng + int(len(lst))) // 2
        if lst[leng] <0 and lst[leng + 1] <0:
        leng = leng // 2
    return index
    
Epsi95

You can use recursion. Every time the list is halved, so the complexity is O(logn)

def find_pos(start, end, l, count):
    if(start > end):
        return count
    middle = start + (end-start)//2
    if(l[middle] >= 0): # that means all left side is positive
        count += (middle-start) + 1
        return find_pos(middle+1, end, l, count)
    else: # that means I am in wrong region
        return find_pos(start, middle-1, l, count)
lst=[2,6,0,1,-3,-19]
find_pos(0, len(lst)-1, lst, 0)

>>> 4

Update: If you want one function passing only lst

def find_positives(l):
    return find_pos(0, len(l)-1, l, 0)
find_positives(lst)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding number of positive numbers in list

From Dev

List all negative numbers and minimum positive number from table

From Dev

Return positive numbers of a list in python

From Dev

Find the smallest positive number not in list

From Dev

Finding first positive number and return a different correlating number

From Java

How to make type="number" to positive numbers only

From Dev

PROLOG-number of positive and negative numbers

From Dev

Finding groups of increasing numbers in a list

From Dev

Finding the average of large list of numbers

From Dev

Finding missing numbers in a list in Perl

From Dev

How to find the positive numbers in a list in Prolog?

From Dev

Compute the sum of the positive numbers in a list in Python

From Dev

Python 2 lists of positive integers finding prime number

From Dev

Python 2 lists of positive integers finding prime number

From Dev

Finding the smallest positive number in O(nlogn) and O(logn) additional space

From Dev

Finding number of unique elements in the list

From Dev

Finding k closest numbers to a given number

From Dev

Finding a duplicate numbers in an array and then counting the number of duplicates

From Dev

Finding the highest number in a set of numbers the user enters

From Dev

Finding closest sum of numbers to a given number

From Dev

Finding all numbers that evenly divide a number

From Dev

Finding all combinations of numbers that equal a specific number

From Dev

How to find the first positive integer number that is not in the list

From Dev

Drop a given number of positive items of a given list

From Dev

Algorithm for finding a group of numbers in a list that equal a target

From Dev

Finding prime numbers using list comprehention

From Dev

Regular expression for finding tag numbers in a list of cats

From Dev

Finding the 5 smallest numbers from a list in Python

From Dev

Finding integers and floating point numbers in a list

Related Related

  1. 1

    Finding number of positive numbers in list

  2. 2

    List all negative numbers and minimum positive number from table

  3. 3

    Return positive numbers of a list in python

  4. 4

    Find the smallest positive number not in list

  5. 5

    Finding first positive number and return a different correlating number

  6. 6

    How to make type="number" to positive numbers only

  7. 7

    PROLOG-number of positive and negative numbers

  8. 8

    Finding groups of increasing numbers in a list

  9. 9

    Finding the average of large list of numbers

  10. 10

    Finding missing numbers in a list in Perl

  11. 11

    How to find the positive numbers in a list in Prolog?

  12. 12

    Compute the sum of the positive numbers in a list in Python

  13. 13

    Python 2 lists of positive integers finding prime number

  14. 14

    Python 2 lists of positive integers finding prime number

  15. 15

    Finding the smallest positive number in O(nlogn) and O(logn) additional space

  16. 16

    Finding number of unique elements in the list

  17. 17

    Finding k closest numbers to a given number

  18. 18

    Finding a duplicate numbers in an array and then counting the number of duplicates

  19. 19

    Finding the highest number in a set of numbers the user enters

  20. 20

    Finding closest sum of numbers to a given number

  21. 21

    Finding all numbers that evenly divide a number

  22. 22

    Finding all combinations of numbers that equal a specific number

  23. 23

    How to find the first positive integer number that is not in the list

  24. 24

    Drop a given number of positive items of a given list

  25. 25

    Algorithm for finding a group of numbers in a list that equal a target

  26. 26

    Finding prime numbers using list comprehention

  27. 27

    Regular expression for finding tag numbers in a list of cats

  28. 28

    Finding the 5 smallest numbers from a list in Python

  29. 29

    Finding integers and floating point numbers in a list

HotTag

Archive