Bisect array binary algorithm

Source: Internet
Author: User
The module inserts elements into the sorted queue list and keeps them sorted. For a list of a large amount of data, insert elements and keep them sorted. the calculation workload is very large. This module implements the bisect algorithm, mainly based on the binary algorithm. This module inserts elements into the sorted queue list and keeps them sorted. For a list of a large amount of data, insert elements and keep them sorted. the calculation workload is very large. This module implements the bisect algorithm, mainly based on the binary algorithm.

Bisect. bisect_left (a, x, lo = 0, hi = len ())

Insert element x in ordered list a to keep the order unchanged and return the inserted position. The lo and hi parameters indicate the range of the judgment list. the default value is the entire range. If the inserted element x already exists in List a, it is inserted to the left of the existing element.

Example:

# Python 3.4

Import bisect

L = [8, 5, 6, 6, 3]

L. sort ()

Print (l)

Print (bisect. bisect_left (l, 0 ))

Print (l)

Print (bisect. bisect_left (l, 5 ))

Print (bisect. bisect_left (l, 7 ))

The output is as follows:

[3, 5, 6, 6, 8]

0

[3, 5, 6, 6, 8]

1

4

Bisect. bisect_right (a, x, lo = 0, hi = len ())

Bisect. bisect (a, x, lo = 0, hi = len ())

Locate the position where x can be inserted in ordered queue a and keep the queue in order. If the inserted value is the same as the value in the queue, it is inserted to the right of the same element and the index value at this position is returned.

Example:

# Python 3.4

Import bisect

L = [8, 5, 6, 6, 3]

L. sort ()

Print (l)

Print (bisect. bisect_right (l, 0 ))

Print (l)

Print (bisect. bisect (l, 5 ))

Print (bisect. bisect (l, 7 ))

The output is as follows:

[3, 5, 6, 6, 8]

0

[3, 5, 6, 6, 8]

2

4

Bisect. insort_left (a, x, lo = 0, hi = len ())

Insert an element x to queue a and sort the queue. It must be a. insert (bisect. bisect_left (a, x, lo, hi), x ).

Example:

# Python 3.4

Import bisect

L = [8, 5, 6, 6, 3]

L. sort ()

Print (l)

Bisect. insort_left (l, 0)

Print (l)

Bisect. insort_left (l, 5)

Print (l)

Bisect. insort_left (l, 7)

Print (l)

The output is as follows:

[3, 5, 6, 6, 8]

[0, 3, 5, 6, 6, 8]

[0, 3, 5, 5, 6, 6, 8]

[0, 3, 5, 5, 6, 6, 7, 8]

Bisect. insort_right (a, x, lo = 0, hi = len ())

Bisect. insort (a, x, lo = 0, hi = len ())

Insert element x to queue a. If the same element is found, insert it to the right of the same element.

Example:

# Python 3.4

Import bisect

L = [8, 5, 6, 6, 3]

L. sort ()

Print (l)

Bisect. insort_right (l, 0)

Print (l)

Bisect. insort (l, 5)

Print (l)

Bisect. insort (l, 7)

Print (l)

The output is as follows:

[3, 5, 6, 6, 8]

[0, 3, 5, 6, 6, 8]

[0, 3, 5, 5, 6, 6, 8]

[0, 3, 5, 5, 6, 6, 7, 8]

Use the above functions for some simple encapsulation:

Def index (a, x ):

'Locate the leftmost value exactly equal to x'

I = bisect_left (a, x)

If I! = Len (a) and a [I] = x:

Return I

Raise ValueError

Def find_lt (a, x ):

'Find rightmost value less than x'

I = bisect_left (a, x)

If I:

Return a [i-1]

Raise ValueError

Def find_le (a, x ):

'Find rightmost value less than or equal to x'

I = bisect_right (a, x)

If I:

Return a [i-1]

Raise ValueError

Def find_gt (a, x ):

'Find leftmost value greater than x'

I = bisect_right (a, x)

If I! = Len ():

Return a [I]

Raise ValueError

Def find_ge (a, x ):

'Find leftmost item greater than or equal to x'

I = bisect_left (a, x)

If I! = Len ():

Return a [I]

Raise ValueError

To convert students' scores to ABCDF:

# Python 3.4

Import bisect

Def grade (score, breakpoints = [60, 70, 80, 90], grades = 'fdba '):

I = bisect. bisect (breakpoints, score)

Return grades [I]

Print ([33, 99, 77, 70, 89, 90,100])

Print ([grade (score) for score in [33, 99, 77, 70, 89, 90,100])

The output is as follows:

[33, 99, 77, 70, 89, 90,100]

['F', 'A', 'C', 'C', 'B', 'A', 'A']

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.