예제 #1
0
파일: app_bisect.py 프로젝트: njues/Sypy
def insort_right(a, x, lo=0, hi=-1):
    """Insert item x in list a, and keep it sorted assuming a is sorted.

If x is already in a, insert it to the right of the rightmost x.

Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched."""
    n = bisect_right(a, x, lo, hi)
    a.insert(n, x)
 def threeSum(self):
     f = {}
     for i in self.n:
         f[i] = f.get(i, 0) + 1
     self.n = sorted(f)
     a = []
     for i, I in enumerate(self.n):
         if not I:
             if f[I] > 2: a.append((0, 0, 0))
         elif f[I] > 1 and -2 * I in f: a.append((I, I, -2 * I))
         if I < 0:
             t = -I
             l = bisect_left(self.n, t - n[-1], i + 1)
             r = bisect_right(self.n, t // 2, l)
             for J in n[l:r]:
                 K = t - J
                 if K in f and K != J: a.append((I, J, K))
     return a
예제 #3
0
 def test_bisect_right(self):
     from _bisect import bisect_right
     a = [0, 5, 6, 6, 6, 7]
     assert bisect_right(a, None) == 0
     assert bisect_right(a, -3) == 0
     assert bisect_right(a, 0) == 1
     assert bisect_right(a, 3) == 1
     assert bisect_right(a, 5) == 2
     assert bisect_right(a, 5.5) == 2
     assert bisect_right(a, 6) == 5
     assert bisect_right(a, 6.0) == 5
     assert bisect_right(a, 6.1) == 5
     assert bisect_right(a, 7) == 6
     assert bisect_right(a, 8) == 6
     a = []
     assert bisect_right(a, 123) == 0
     a = [9]
     assert bisect_right(a, -123) == 0
     assert bisect_right(a, 9) == 1
     assert bisect_right(a, 123) == 1
     a = [9, 9]
     assert bisect_right(a, -123) == 0
     assert bisect_right(a, 9) == 2
     assert bisect_right(a, 123) == 2
     a = [4, 6, 6, 9]
     assert bisect_right(a, 6, 0) == 3
     assert bisect_right(a, 6, 1) == 3
     assert bisect_right(a, 6, 2) == 3
     assert bisect_right(a, 6, 3) == 3
     assert bisect_right(a, 6, 4) == 4
     assert bisect_right(a, 6, 0, 0) == 0
     assert bisect_right(a, 6, 0, 1) == 1
     assert bisect_right(a, 6, 0, 2) == 2
     assert bisect_right(a, 6, 0, 3) == 3
     assert bisect_right(a, 6, 0, 4) == 3
예제 #4
0
 def test_bisect_right(self):
     from _bisect import bisect_right
     a = [0, 5, 6, 6, 6, 7]
     assert bisect_right(a, None) == 0
     assert bisect_right(a, -3) == 0
     assert bisect_right(a, 0) == 1
     assert bisect_right(a, 3) == 1
     assert bisect_right(a, 5) == 2
     assert bisect_right(a, 5.5) == 2
     assert bisect_right(a, 6) == 5
     assert bisect_right(a, 6.0) == 5
     assert bisect_right(a, 6.1) == 5
     assert bisect_right(a, 7) == 6
     assert bisect_right(a, 8) == 6
     a = []
     assert bisect_right(a, 123) == 0
     a = [9]
     assert bisect_right(a, -123) == 0
     assert bisect_right(a, 9) == 1
     assert bisect_right(a, 123) == 1
     a = [9, 9]
     assert bisect_right(a, -123) == 0
     assert bisect_right(a, 9) == 2
     assert bisect_right(a, 123) == 2
     a = [4, 6, 6, 9]
     assert bisect_right(a, 6, 0) == 3
     assert bisect_right(a, 6, 1) == 3
     assert bisect_right(a, 6, 2) == 3
     assert bisect_right(a, 6, 3) == 3
     assert bisect_right(a, 6, 4) == 4
     assert bisect_right(a, 6, 0, 0) == 0
     assert bisect_right(a, 6, 0, 1) == 1
     assert bisect_right(a, 6, 0, 2) == 2
     assert bisect_right(a, 6, 0, 3) == 3
     assert bisect_right(a, 6, 0, 4) == 3
'''
Created on Aug 1, 2016

@author: Md. Rezwanul Haque
'''

import bisect
from _bisect import bisect_left, bisect_right
def Fib(n):
    a,b = 1,1
    while b<n:
        yield b 
        a,b = b, a+b 
FibA = list(Fib(10**100))
while True:
    f,s = map(int, input().split())
    if f == 0 and s == 0:
        break
    left = bisect_left(FibA,f)
    right = bisect_right(FibA,s)
    print(right - left) 
    
예제 #6
0
def CLM_NcRead_1file(ncfile, varnames_print, keep_vars, chunk_keys, \
                     startdays, enddays, adspinup, vars_all):
    odata = {}
    odata_dims = {}
    odata_tunits = ''

    try:
        startdays = float(startdays)
    except ValueError:
        startdays = -9999

    try:
        enddays = float(enddays)
    except ValueError:
        enddays = -9999

    try:
        f = Dataset(ncfile, 'r')
        if varnames_print:
            print('FILE: ' + ncfile + ' ------- ')

    except:
        return odata, odata_dims

    # If key is on the keep list and in nc file, uniquely add to a dictionary
    for key in f.variables.keys():

        # print out all variables contained in nc files
        if varnames_print:
            print(key)

        if (key not in keep_vars) and (not vars_all):
            continue  #cycle the loop, if not required by users and NOT all_vars option
        if (key not in f.variables or f.variables[key].size <= 0):
            continue  #cycle the loop, if no data

        if (len(chunk_keys) <= 0) or (
                key not in chunk_keys
        ):  # only needs to read data once, if more than one available
            odata_dims[key] = f.variables[key].dimensions

            key_val = np.asarray(f.variables[key])
            if (hasattr(f.variables[key], '_FillValue')):
                v_missing = f.variables[key]._FillValue
                if isinstance(v_missing, (np.float, np.float16, np.float32)):
                    key_val[key_val == v_missing] = np.nan
            odata[key] = key_val

        else:
            continue

        if ('time' not in odata_dims[key]):
            continue  #cycle the loop, if not time-series dataset
        # timing option - we do checking thereafter
        # because we want to have those CONSTANTs read-out, some of which ONLY available in the first CLM nc file.
        tt = np.asarray(
            f.variables['time'])  # days since model simulation starting time
        if (odata_tunits == ''): odata_tunits = f.variables['time'].units
        tdays1 = min(tt)
        tdays2 = max(tt)
        if (startdays >= 0):
            if (startdays > tdays2):
                odata = {}
                odata_dims = {}
                return odata, odata_dims, odata_tunits
            else:
                s_index = int(bisect_left(tt, startdays))
                odata[key] = odata[key][s_index:, ]

        if (enddays > 0):
            if (enddays < tdays1):
                odata = {}
                odata_dims = {}
                return odata, odata_dims, odata_tunits
            else:
                e_index = int(bisect_right(tt, enddays))
                odata[key] = odata[key][:e_index, ]

        #ad-spinup run, recal. each component of totsomc_vr, if needed
        if adspinup:
            if key in totsomc_vr:
                sub_indx = totsomc_vr.index(key)
                odata[key] = odata[key] * ad_factor[sub_indx]

            if key in totsomn_vr:
                sub_indx = totsomn_vr.index(key)
                odata[key] = odata[key] * ad_factor[sub_indx]

    # summing up of total liter/som C/N, if needed and available
    if 'TOTLITC_vr' in keep_vars:
        indx = keep_vars.index('TOTLITC_vr')
        subs = totlitc_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    if 'TOTLITN_vr' in keep_vars:
        indx = keep_vars.index('TOTLITN_vr')
        subs = totlitn_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    if 'TOTSOMC_vr' in keep_vars:
        indx = keep_vars.index('TOTSOMC_vr')
        subs = totsomc_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    if 'TOTSOMN_vr' in keep_vars:
        indx = keep_vars.index('TOTSOMN_vr')
        subs = totsomn_vr
        for isub in subs:
            if isub in odata.keys():
                if keep_vars[indx] not in odata:
                    odata[keep_vars[indx]] = odata[isub]
                    odata_dims[keep_vars[indx]] = odata_dims[isub]
                else:
                    odata[
                        keep_vars[indx]] = odata[keep_vars[indx]] + odata[isub]

    # convert 'soilliq' to 'saturation_lia', if available
    # sat = h2osoi_liq(c,j) / (watsat(c,j)*dz(c,j)*denh2o)
    denh2o = 1000.0  #kg/m3
    if 'SOILSAT_LIQ' in keep_vars:
        if 'SOILLIQ' in odata.keys():
            dary = np.array(odata['SOILLIQ'])
            dvec = np.array(porosity * dz * denh2o)
            if (len(dvec.shape) == 2):
                odata['SOILSAT_LIQ'] = dary / dvec[None, :, :]
            elif (len(dvec.shape) == 3):
                odata['SOILSAT_LIQ'] = dary / dvec[None, :, :, :]
            else:
                odata['SOILSAT_LIQ'] = dary / dvec

            odata_dims['SOILSAT_LIQ'] = odata_dims['SOILLIQ']

    denice = 917.0  #kg/m3
    if 'SOILSAT_ICE' in keep_vars:
        if 'SOILICE' in odata.keys():
            dary = np.array(odata['SOILICE'])
            dvec = np.array(porosity * dz * denice)
            if (len(dvec.shape) == 2):
                odata['SOILSAT_ICE'] = dary / dvec[None, :, :]
            elif (len(dvec.shape) == 3):
                odata['SOILSAT_ICE'] = dary / dvec[None, :, :, :]
            else:
                odata['SOILSAT_ICE'] = dary / dvec

            odata_dims['SOILSAT_ICE'] = odata_dims['SOILICE']

    # out datasets
    return odata, odata_dims, odata_tunits
예제 #7
0
import bisect
from _bisect import bisect_left, bisect_right


def GenerateFibNumbers(n):
    a, b = 1, 1
    while b < n:
        yield b
        a, b = b, a + b


FibNumbers = list(GenerateFibNumbers(10**100))

while True:
    start, end = map(int, input().split())

    if start == 0 and end == 0:  # invalid input
        break

    left = bisect_left(FibNumbers, f)  # finds start position
    right = bisect_right(FibNumbers, s)  # finds end position

    # calculate the difference to get the number of values bewtween them
    print(right - left)