Best Python code snippet using avocado_python
gibbs_fuzzy.py
Source:gibbs_fuzzy.py  
...40    if obj_index % 2 == 0:41        cl = [obj_index, obj_index+1]42    else:43        cl = [obj_index-1, obj_index]44    possible_values = get_possible_values(obj_index=obj_index, data=data, g_values=g_values)[0]45    n = len(possible_values) - 146    for v_ind, v in enumerate(possible_values):47        l_p.append(1.)48        for obj in cl:49            obj_s = data[obj][0]50            psi_list = data[obj][1]51            for s, psi_val in zip(obj_s, psi_list):52                accuracy = accuracy_list[s]53                psi_ind = obj_s.index(s)54                g = g_values[obj][1][psi_ind]55                if (obj == obj_index and g == 1) or (obj != obj_index and g == 0):56                    if psi_val == v:57                        l_p[v_ind] *= accuracy58                    else:59                        l_p[v_ind] *= (1-accuracy)/n60    norm_const = sum(l_p)61    for v_ind in range(len(possible_values)):62        l_p[v_ind] /= norm_const63    mult_trial = list(np.random.multinomial(1, l_p, size=1)[0])64    v_new_ind = mult_trial.index(1)65    v_new = possible_values[v_new_ind]66    obj_values.update({obj_index: v_new})67    prob.update({obj_index: l_p})68    for obj in cl:69        obj_s = data[obj][0]70        psi_list = data[obj][1]71        for s, psi_val in zip(obj_s, psi_list):72            psi_ind = obj_s.index(s)73            g = g_values[obj][1][psi_ind]74            if (obj == obj_index and g == 1) or (obj != obj_index and g == 0):75                c_prev = counts[obj][1][psi_ind]76                c_new = 1 if psi_val == v_new else 077                if c_new != c_prev:78                    counts[obj][1][psi_ind] = c_new79def update_g(s, obj_index, g_values, pi_prob, obj_values, accuracy, counts, data):80    l_p = []81    possible_values = [0, 1]82    cluster = obj_index/283    psi_index = data[obj_index][0].index(s)84    psi_val = data[obj_index][1][psi_index]85    g_prev = g_values[obj_index][1][psi_index]86    for g in possible_values:87        pr_pi = pi_prob[cluster]**g*(1-pi_prob[cluster])**(1-g)88        if g == 1:89            g_values[obj_index][1][psi_index] = 190            n = len(get_possible_values(obj_index=obj_index, data=data, g_values=g_values)[0]) - 191            if psi_val == obj_values[obj_index]:92                pr_pi *= accuracy93            else:94                if n == 0:95                    pr_pi *= 0.96                else:97                    pr_pi *= (1-accuracy)/n98        else:99            g_values[obj_index][1][psi_index] = 0100            if obj_index % 2 == 0:101                n = len(get_possible_values(obj_index=obj_index+1, data=data, g_values=g_values)[0]) - 1102                if psi_val == obj_values[obj_index+1]:103                    pr_pi *= accuracy104                else:105                    if n == 0:106                        pr_pi *= 0.107                    else:108                        pr_pi *= (1-accuracy)/n109            else:110                n = len(get_possible_values(obj_index=obj_index-1, data=data, g_values=g_values)[0]) - 1111                if psi_val == obj_values[obj_index-1]:112                    pr_pi *= accuracy113                else:114                    if n == 0:115                        pr_pi *= 0.116                    else:117                        pr_pi *= (1-accuracy)/n118        l_p.append(pr_pi)119    norm_const = sum(l_p)120    if l_p[0] == l_p[1]:121        g_new = np.random.binomial(1, pi_prob[cluster], 1)[0]122    else:123        l_p[0] /= norm_const124        l_p[1] /= norm_const125        g_new = np.random.binomial(1, l_p[1], 1)[0]126    if g_new != g_prev:127        g_values[obj_index][1][psi_index] = g_new128        if g_new == 1:129            if psi_val == obj_values[obj_index]:130                counts[obj_index][1][psi_index] = 1131            else:132                counts[obj_index][1][psi_index] = 0133        else:134            if obj_index % 2 == 0:135                if psi_val == obj_values[obj_index+1]:136                    counts[obj_index][1][psi_index] = 1137                else:138                    counts[obj_index][1][psi_index] = 0139            else:140                if psi_val == obj_values[obj_index-1]:141                    counts[obj_index][1][psi_index] = 1142                else:143                    counts[obj_index][1][psi_index] = 0144    else:145        g_values[obj_index][1][psi_index] = g_prev146def get_pi(cl_ind, g_values, data):147    cl = [cl_ind*2, cl_ind*2+1]148    count_p, count_m = 0, 0149    for obj in cl:150        for g in g_values[obj][1]:151            if g == 1:152                count_p += 1153            else:154                count_m += 1155    pi_new = beta.rvs(count_p + gamma1, count_m + gamma2, size=1)[0]156    return pi_new157def get_a(counts, s_ind):158    count_p, count_m = 0, 0159    for obj in counts.keys():160        sources = counts[obj][0]161        if s_ind not in sources:162            continue163        c_ind = sources.index(s_ind)164        c = counts[obj][1][c_ind]165        if c == 1:166            count_p += 1167        else:168            count_m += 1169    a_new = beta.rvs(count_p + alpha1, count_m + alpha2, size=1)[0]170    return a_new171def get_possible_values(obj_index, data, g_values):172    if obj_index % 2 == 0:173        cl = [obj_index, obj_index+1]174    else:175        cl = [obj_index-1, obj_index]176    possible_values = []177    values = []178    for obj in cl:179        obj_obs = data[obj][1]180        obj_g_values = g_values[obj][1]181        for val, g in zip(obj_obs, obj_g_values):182            if (g == 1 and obj == obj_index) \183                    or (g == 0 and obj != obj_index):184                if val not in possible_values:185                    possible_values.append(val)186                values.append(val)187    possible_values = sorted(possible_values)188    return [possible_values, values]189def get_metrics(data, gt, prob, g_values):190    dist = 0.191    gt_objects = gt.keys()192    norm_const = len(gt_objects)193    pres_count = 0.194    for obj in gt_objects:195        possible_values = get_possible_values(obj_index=obj, data=data, g_values=g_values)[0]196        # this 'if' only for getting accuracy197        if len(possible_values) == 1:198            norm_const -= 1199            continue200        try:201            gt_val_ind = possible_values.index(gt[obj])202        except ValueError:203            norm_const -= 1204            continue205        obj_prob = prob[obj]206        dist += obj_prob[gt_val_ind]207        obj_ind = obj_prob.index(max(obj_prob))208        if gt_val_ind == obj_ind:209            pres_count += 1...sudokuHS.py
Source:sudokuHS.py  
...32                row_values += '%.2f  ' % cell['value']33            print row_values34            row_values = ""35        print ""36def get_possible_values(x,y):37    values = [1,2,3,4,5,6,7,8,9]38    #print values39    # Row40    for cell in sudoku[x]:41        42        if not cell['final']:43            continue44        current_value = cell['value']45        46        if current_value != 0 and current_value in values:47            ##print "index:" + str(values.index(current_value))48            values.remove(current_value)49    #print values50    # Column51    for i in range(9):52        if not sudoku[i][y]['final']:53            continue54        current_value = sudoku[i][y]['value']55        56        if current_value != 0 and current_value in values:57            ##print "index:" + str(values.index(current_value))58            values.remove(current_value)59    60    # Sector61    sector_index_x = x / 3 * 362    sector_index_y = y / 3 * 363    #print values64    for i in range(sector_index_x, sector_index_x + 3):65        for j in range(sector_index_y, sector_index_y + 3):66            67            if not sudoku[i][j]['final']:68                continue69            current_value = sudoku[i][j]['value']70            if current_value != 0 and current_value in values:71                values.remove(current_value)72    #print values73    return values74def choose_cell_randomly():75    """ Get the first available cell """76    cell = {'x': 0, 'y': 0}77    for i in range(9):78        for j in range(9):79            if not sudoku[i][j]["final"]:80                return {'x': i, 'y': j}81    return cell82# Value for first iteration83def calc_value(cell):84    if len(cell['possible_values']) == 1:85        cell['value'] = cell['possible_values'][0]86        cell['final'] = True87        cell['possible_values'] = []88        return cell89    else:90        cell['value'] = min(cell['possible_values']) + HMRC * (max(cell['possible_values']) - min(cell['possible_values']))91        #cell['final'] = False92        #cell['possible_values'] = 93        return cell94def fill_up():95    possible_values = []96    for i in range(len(sudoku)):97        for j in range(len(sudoku[i])):98            if not sudoku[i][j]['final']:99                sudoku[i][j]['possible_values'] = get_possible_values(i, j)100                #print "{0} {1}".format(i, j)101                #print possible_values102                sudoku[i][j] = calc_value(sudoku[i][j])103def new_value(cell, iterations):104    values = [cell['value'], cell['value']]105    values[0] += HMRC * PAR * 0.5 * iterations106    values[1] -= HMRC * PAR * 0.5 * iterations107    if int(values[0]) in cell['possible_values']:108        cell['value'] = values[0]109        return cell110    elif int(values[1]) in cell['possible_values']:111        cell['value'] = values[1]112        return cell113    return cell114# number of iteration over a cell value115def num_iterations(cell):116    return (cell['value'] - cell['possible_values'][0]) / ( HMRC * PAR * .5 )117# Get next value for all cells118def iterate():119    possible_values = []120    for i in range(len(sudoku)):121        for j in range(len(sudoku[i])):122            if not sudoku[i][j]['final']:123                #possible_values = get_possible_values(i, j)124                iterations = num_iterations(sudoku[i][j])125                print sudoku[i][j]['value']126                print iterations127                sudoku[i][j] = new_value(sudoku[i][j], iterations)128                #sudoku[i][j]['possible_values'] = get_possible_values(i, j)129    # Mark new final values130    for i in range(len(sudoku)):131        for j in range(len(sudoku[i])):132            sudoku[i][j]['possible_values'] = get_possible_values(i, j)133def solve():134    display()135    fill_up()136    display()137    iterate()138    display()139def main():140    #display(sudoku)141    ##print get_possible_values(7,0)    142    solve()143if __name__ == '__main__':...day3p2.py
Source:day3p2.py  
...56    """57    Converts binary strings to decimal58    """59    return int(binary_str, 2)60def get_possible_values(filename, bit_str):61    """62    Creates a returns of all the possible lists from the 63    given bit string64    """65    possible_strings = []66    with open(filename) as file:67        for line in file:68            if line.strip()[:len(bit_str)] == bit_str:69                possible_strings.append(line.strip())70    return possible_strings71def oxygen_generator_rating(filename):72    """73    Calculates the oxygen generator rating74    """75    bit_str = ""76    bit_index = 077    bit = get_popular_bit_file(filename, bit_index)78    bit_str += str(bit)79    bit_index += 180    possible_values = get_possible_values(filename, bit_str)81    while len(possible_values) != 1:82        bit = get_popular_bit_list(possible_values, bit_index)83        bit_str += str(bit)84        bit_index += 185        possible_values = get_possible_values(filename, bit_str)86    return possible_values[0]87def co2_scrubber_rating(filename):88    """89    Calculates the CO2 scrubber rating90    """91    bit_str = ""92    bit_index = 093    bit = 094    if get_popular_bit_file(filename, bit_index) == 0:95        bit = 196    bit_str += str(bit)97    bit_index += 198    possible_values = get_possible_values(filename, bit_str)99    while len(possible_values) != 1:100        bit = get_least_popular_bit_list(possible_values, bit_index)101        bit_str += str(bit)102        bit_index += 1103        possible_values = get_possible_values(filename, bit_str)104    return possible_values[0]105def life_support_rating(filename):106    """107    Determines the life support rating after converting the 108    oxygen rating and CO2 scrubber rating from binary to 109    decimal and returns it110    """111    oxygen_rating = binary_to_decimal(oxygen_generator_rating(filename))112    co2_rating = binary_to_decimal(co2_scrubber_rating(filename))113    return oxygen_rating * co2_rating114def main():115    # print(get_popular_bit("day3small.txt", 1)) 116    # print(get_gamma_binary("day3small.txt"))117    # print(get_epsilon_binary("day3small.txt"))118    # print(binary_to_decimal('10110'))119    # print(binary_to_decimal('01001'))120    # print(power_consumption("day3small.txt"))121    # print(power_consumption("day3.txt"))122    # print(get_possible_values("day3small.txt", "10111"))123    # print(oxygen_generator_rating("day3small.txt"))124    # print(co2_scrubber_rating("day3small.txt"))125    print(life_support_rating("day3.txt"))126if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
