Best Python code snippet using avocado_python
calc.py
Source:calc.py
1import sys2import json3import random4import math5# Fixed values6with open("core_jemstone.json") as f:7 DATA = json.load(f)8#Exp per core9CORE_EXP = 5010#Core type flags11ENHANCE_FLAG = 012SKILL_FLAG = 113USELESS_FLAG = 214#Core probabilities15ENHANCE_PROB = 0.816SKILL_PROB = 0.117USELESS_PROB = 0.118#Fragment gain from core19ENHANCE_FRAG = 1020SKILL_FRAG = 4021USELESS_FRAG = 5022#Fragment requirements to generate core23ENHANCE_NEED_FRAG = 7024SKILL_NEED_FRAG = 14025#Lv - exp map26ENHANCE_NEED = []27[ENHANCE_NEED.append( (3 * n + 19) * n * 5 // 2 ) for n in range(25)]28SKILL_NEED = []29[SKILL_NEED.append( (n + 21) * n * 5 // 2 ) for n in range(25)]30#Skill levels that aim to achieve31AIM_MAIN = 5032AIM_SUB = 2533AIM_SKILL = 2034class User():35 def __init__(self, jobname, level = 200):36 self.job = jobname37 self.frags = 038 39 self.aim_main = AIM_MAIN40 self.aim_sub = AIM_SUB41 self.aim_skill = AIM_SKILL42 43 #corelist : List of collected cores44 # [0, 1, ... main-1, main, main+1, ... , main + sub -1, ..., total - 1]45 46 self.core_list = []47 self.core_amount = []48 49 self.core_attr = []50 self.core_head = []51 self.skill_list = []52 53 self.total = DATA[self.job]["core_total"]54 self.main = len(DATA[self.job]["main"])55 self.sub = len(DATA[self.job]["sub"])56 self.skillneed = DATA[self.job]["vskill"]57 58 #self.skillneed = 0 #Modify if program applies skill usages59 60 self.enhance_complete_flag = False61 62 self.slot = 4 + (level - 200) // 5 - self.skillneed #Available slots, excepting <skillneed>.63 self.mat_point = level - 200 #matrix point64 65 [self.core_list.append([]) for i in range(self.total)]66 [self.core_amount.append(0) for i in range(self.total)]67 [self.core_head.append([i,-1,-1]) for i in range(self.total)]68 [self.skill_list.append(0) for i in range(self.skillneed)]69 [self.core_attr.append(0) for i in range(self.total)]70 71 def get_core_amount_exp(self, amount):72 return amount * CORE_EXP73 def get_level(self, exp, _type = ENHANCE_FLAG):74 i = 075 if _type == ENHANCE_FLAG:76 while ENHANCE_NEED[i] < exp:77 i += 178 elif _type == SKILL_FLAG:79 #print("Log at skill flaged exp calculation..EXp : " + str(exp))80 while SKILL_NEED[i] < exp:81 i += 182 return i83 84 def get_level_at_core_list(self, index):85 return self.get_level(self.get_core_amount_exp(self.core_amount[index]))86 87 def get_core(self):88 prob = random.random()89 if prob < ENHANCE_PROB:90 if self.enhance_complete_flag:91 self.frags += ENHANCE_FRAG 92 else:93 core = random.sample(range(self.total),3)94 self.core_list[core[0]].append(core)95 self.core_amount[core[0]] += 196 97 elif prob < ENHANCE_PROB + SKILL_PROB:98 core = random.randint(0, self.skillneed - 1)99 if self.skillneed == 0:100 self.frags += SKILL_FRAG101 else:102 self.skill_list[core] += CORE_EXP103 else:104 self.frags += USELESS_FRAG105 106 def update_core_head(self):107 attrs = []108 109 #Initialize attraction point110 for i in range(self.main):111 attrs.append(self.aim_main)112 for i in range(self.sub):113 attrs.append(self.aim_sub)114 while len(attrs) != self.total:115 attrs.append(0)116 117 def get_attr(_core):118 retval = 0119 for i in range(3):120 retval += attrs[_core[i]]121 return retval122 123 for i in range(self.total):124 if self.core_amount[i] > 0:125 test_core = self.core_list[i][-1]126 127 if get_attr(test_core) > get_attr(self.core_head[i]):128 self.core_head[i] = test_core129 #update attrs130 for k in range(3):131 attrs[self.core_head[i][k]] -= self.get_level_at_core_list(i)132 if attrs[self.core_head[i][k]] < 0:133 attrs[self.core_head[i][k]] = 0134 135 def get_important_indices(self, _print = False):136 retlist = []137 [retlist.append(i) for i in range(self.total)]138 #Iterate 7 times : be cleverer! But it also can make us stupid..139 for repeat in range(7):140 attrs = []141 #Initialize attraction point142 for i in range(self.main):143 attrs.append(self.aim_main)144 for i in range(self.sub):145 attrs.append(self.aim_sub)146 while len(attrs) != self.total:147 attrs.append(0)148 149 def get_attr_from_index(_index):150 retval = 0151 for k in range(3):152 if _index in retlist:153 retval += max(attrs[self.core_head[_index][k]] + self.get_level_at_core_list(_index), 0)154 else:155 retval += max(attrs[self.core_head[_index][k]], 0)156 return retval157 158 for i in retlist:159 #update attrs160 for k in range(3):161 attrs[self.core_head[i][k]] -= self.get_level_at_core_list(i)162 163 index_attrs = {}164 for i in range(self.total):165 index_attrs[i] = get_attr_from_index(i)166 167 retlist = []168 #find picks169 while len(retlist) < self.slot:170 pick = list(index_attrs.keys())[0]171 for i in index_attrs:172 if index_attrs[pick] < index_attrs[i]:173 pick = i174 index_attrs.pop(pick)175 retlist.append(pick)176 177 if _print:178 print("---importancy calculation---")179 print("attrs: " + str(attrs))180 print("retlist: " + str(retlist))181 print("----------------------------")182 return retlist183 184 #Calculate lack exp for target index core upgrade 185 def calculate_lack_exp(self, index, lack, flag = ENHANCE_FLAG):186 def exp_from_lvup_at(_lv, _flag):187 if _flag == ENHANCE_FLAG:188 return (40 + 15 * _lv)189 elif _flag == SKILL_FLAG:190 return (50 + 5 * _lv)191 retexp = 0192 lvlist = []193 if flag == ENHANCE_FLAG:194 for i in range(self.total):195 if index in self.core_head[i]:196 lvlist.append(self.get_level_at_core_list(i))197 count = lack198 199 while count > 0:200 lvlist.sort()201 retexp += exp_from_lvup_at(lvlist[0], flag)202 lvlist[0] += 1203 count -= 1 204 205 elif flag == SKILL_FLAG:206 for i in range(self.get_level(self.skill_list[index], flag), lack):207 retexp += exp_from_lvup_at(i, flag)208 209 else:210 raise TypeError211 212 return retexp213 214 def judge(self, _print = None):215 lv_list = []216 enhance_list = []217 [enhance_list.append(0) for i in range(self.total)]218 #Initialize attraction point219 for i in range(self.main):220 lv_list.append(self.aim_main)221 for i in range(self.sub):222 lv_list.append(self.aim_sub)223 while len(lv_list) != self.total:224 lv_list.append(0)225 226 vskill_lv_list = []227 vskill_lv_current = []228 [vskill_lv_list.append(self.aim_skill) for i in range(self.skillneed)]229 [vskill_lv_current.append(0) for i in range(self.skillneed)]230 231 #Preprocessing with matrix point..232 #First invest every points into vskills233 mat_point = self.mat_point234 for i in range(self.skillneed * 5):235 vskill_lv_list[i % self.skillneed] -= 1236 mat_point -= 1237 if mat_point <= 0:238 break239 #Use remain points for reducing levels in enhancing cores.240 while mat_point > 0:241 for i in range(3): #Triple reducing for enhancement. <-- Some-how Underestimates low-needing jobs :: Need to fix better.242 lv_list[lv_list.index(max(lv_list))] -= 1243 mat_point -= 1244 #Matrix point pre-processing END.245 246 imp = self.get_important_indices()247 248 #Calculate status of enhancement cores249 for i in imp:250 if self.core_head[i][1] != -1:251 for k in range(3):252 lv_list[self.core_head[i][k]] -= self.get_level_at_core_list(i)253 enhance_list[self.core_head[i][k]] += self.get_level_at_core_list(i)254 if lv_list[self.core_head[i][k]] < 0:255 lv_list[self.core_head[i][k]] = 0256 257 #Calculate lacks258 needs = 0259 260 #Enhance skill lack261 for i in range(self.total):262 need_i = 0263 if lv_list[i] != 0:264 need_i += self.calculate_lack_exp(i, lv_list[i], flag = ENHANCE_FLAG)265 needs += ((need_i - 1) // CORE_EXP + 1) * ENHANCE_NEED_FRAG266 267 #Raise enhance completion flag if do not need enhancement any more268 if needs == 0:269 self.enhance_complete_flag = True270 271 #VSkill lack272 for i in range(self.skillneed):273 need_i = self.calculate_lack_exp(i, vskill_lv_list[i], flag = SKILL_FLAG)274 needs += ((need_i - 1) // CORE_EXP + 1) * SKILL_NEED_FRAG275 276 #Generate fragments from none - using enhance cores277 frags_clever = self.frags278 for i in range(self.total):279 if i not in imp:280 frags_clever += self.core_amount[i] * ENHANCE_FRAG281 282 if _print:283 print("Judegement Status(lack)")284 print(lv_list)285 print("Enhanced Status")286 print(enhance_list)287 print("---Fragment Need------")288 print(needs)289 print("----------------------")290 291 if needs > frags_clever:292 return False293 else:294 return True295 296 def simulate(self, _print = False):297 self.frags = 0298 self.enhance_complete_flag = False299 300 for i in range(self.total):301 self.core_amount[i] = 0302 self.core_list[i] = []303 self.core_attr[i] = 0304 self.core_head[i] = [i, -1, -1]305 306 for i in range(self.skillneed):307 self.skill_list[i] = 0308 309 cores = 0310 while not self.judge():311 self.get_core()312 self.update_core_head()313 cores += 1314 315 if cores % 100 == 0 and _print:316 self.judge(_print = True)317 self.print_status(_print = True)318 print("Cores :" + str(cores))319 print("\n\n")320 if _print:321 print("*** Calculation end ***")322 self.judge(True)323 self.print_status()324 print("Cores : " + str(cores))325 return cores326 327 def print_status(self, _print = False):328 print("<Core Extraction status>")329 imp = self.get_important_indices(_print)330 for i in range(self.total):331 printstr = "At core head" + str(self.core_head[i]) + " : " + str(self.core_amount[i]) + ", Level : " + str(self.get_level_at_core_list(i))332 if i in imp:333 printstr += " <-- selected"334 print(printstr)335 print("---- VSkill Cores ----")336 for i in range(self.skillneed):337 print("VSkill core #" + str(i) + " : Lv." + str(self.get_level(self.skill_list[i], SKILL_FLAG)))338 print("----------------------\n")339 print("fragments:" + str(self.frags) + "\n")340if __name__ == "__main__":341 #name = sys.stdin.readline()342 #user = User(name.split("\n")[0])343 user = User("ìë°", level = 230)344 iter_num = 20345 346 sum_ = 0347 sq_sum = 0348 349 for i in range(iter_num):350 core = user.simulate(_print = False)351 sum_ += core352 sq_sum += core * core353 354 print("Job : " + user.job)355 print("Trial : " + str(iter_num))356 print("Average requirement : " + str((sum_ / iter_num)))357 stdev = math.sqrt(sq_sum * iter_num - sum_ * sum_) / iter_num...
linkedList.py
Source:linkedList.py
1# -*- coding: utf-8 -*-2from node import *3class LinkedList:4 def __init__(self,head = None):5 self.head = head6 def addNode(self, p_data):7 lv_node = Node(p_data,self.head)8 self.head = lv_node9 return True10 def reverse(self):11 lv_curr = self.head12 lv_list = LinkedList()13 while lv_curr:14 lv_list.addNode(getattr(lv_curr, 'data', 'empty'))15 lv_curr = getattr(lv_curr, 'nextNode', 'None')16 return lv_list;17 18 def printNode(self):19 lv_curr = self.head20 while lv_curr:21 print(getattr(lv_curr, 'data', 'empty'))22 lv_curr = getattr(lv_curr, 'nextNode', 'None')23 def reverseAfter(self, p_val):24 lv_curr = self.head25 lv_list = LinkedList()26 # First run27 while lv_curr:28 lv_list.addNode(getattr(lv_curr, 'data', 'empty'))29 if getattr(lv_curr, 'data', 'empty') == p_val:30 lv_list = lv_list.reverse()31 lv_curr = getattr(lv_curr, 'nextNode', 'None')32 return lv_list33# Runing 34gv_list = LinkedList()35print("Entre simbol by one")36while 1:37 lv_value = raw_input("")38 lv_value = lv_value.rstrip("\r")39 if lv_value:40 gv_list.addNode(lv_value)41 else:42 break;43print("Entre HEAD of reverse")44gv_value = raw_input("")45gv_value = gv_value.rstrip("\r")46print("----Result----")47gv_list = gv_list.reverseAfter(gv_value)48gv_list.printNode()...
C.py
Source:C.py
1from itertools import permutations2def solve(n, m, w_list, lv_list):3 min_v = min([lv[1] for lv in lv_list])4 max_w = max(w_list)5 if max_w > min_v:6 return -17 w_dict = dict()8 # pow9 for i in range(1, 2 ** n):10 w = 011 for j in range(n):12 if i & (2 ** j):13 w += w_list[j]14 w_dict[w] = 015 # max_len16 for l, v in lv_list:17 for w in w_dict.keys():18 if v < w:19 w_dict[w] = max(w_dict[w], l)20 res = 10 ** 921 for p in permutations(range(n), n):22 d_list = [0]23 for i in range(1, n):24 w = w_list[p[i]]25 d = 026 for j in range(i - 1, -1, -1):27 w += w_list[p[j]]28 d = max(d, d_list[j] + w_dict[w])29 d_list.append(d)30 res = min(res, d_list[-1])31 return res32def main():33 n, m = map(int, input().split())34 w_list = list(map(int, input().split()))35 lv_list = [list(map(int, input().split())) for _ in range(m)]36 res = solve(n, m, w_list, lv_list)37 print(res)38def test():39 assert solve(3, 2, [1, 4, 2], [[10, 4], [2, 6]]) == 1040 assert solve(2, 1, [12, 345], [[1, 1]]) == -141 assert solve(8, 1, [1, 1, 1, 1, 1, 1, 1, 1], [[100000000, 1]]) == 70000000042if __name__ == "__main__":43 test()...
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!!