第一题
def run(lines): lines = [tuple(line) for line in lines] lines_f = sorted(lines, key=lambda x: x[0]) lines_e = sorted(lines, key=lambda x: x[1]) dic_f, dic_e = {},{} for line in lines: dic_f[line] = lines_f.index(line) dic_e[line] = lines_e.index(line) for i,line in enumerate(lines_f): ei = dic_e[line] for j in range(i): lj = lines_f[j] if lj[0] == line[0]&nbs***bsp;lj[1] == line[1]: return True if ei < dic_e[lj]: return True return False run(lines)第二题
def run(lines): j,d = lines[0], lines[1] ji, di = 0, 0 jc, dc = 0, 0 ta = [] while len(j) != ji&nbs***bsp;len(d) != di: fj,fd = True,True while fj and len(j) != ji: c = j[ji] ji += 1 ci = -1 try: ci = ta.index(c) except: pass if ci != -1: jc += len(ta) - ci + 1 ta = ta[0:ci] else: ta.append(c) fj = False while fd and len(d) != di: c = d[di] di += 1 ci = -1 try: ci = ta.index(c) except: pass if ci != -1: dc += len(ta) - ci + 1 ta = ta[0:ci] else: ta.append(c) fd = False for c in ta: if c % 2 == 0: dc += 1 else: jc += 1 return jc, dc lines = [[1,2,3,4,10,5],[5,6,7,8,2]] arr = run(lines) print(arr[0], arr[1])
第三题
def run(line): a,b,c,q = line if b == 1: return 1 if c == 1: if (q - a) % b == 0: return 1 ar = set() ar.add(a) if q == a: return 1 while True: nar = set() for i in ar: nar.add(i+b) nar.add(i*c) if q in nar: return 1 if min(nar) > q: return 0 ar = nar print(run([2,3,2,10]))
第四题
def run(arr,mn): narr = [] for i,n in enumerate(arr): for _ in range(n): narr.append(i) return select(narr, 0, [],[],mn) def select(arr, i, r,l,mn): if i == len(arr): ra,la = [],[] build(r,[],ra) build(l,[],la) for nr in ra: for nl in la: mn = max(mn, nr*nl) return mn n = arr[i] r.append(n) mn = select(arr, i+1, r,l, mn) r.remove(n) l.append(n) mn = select(arr, i+1, r,l, mn) l.remove(n) return mn def build(arr, r,all_arr): if 0 == len(arr): if len(r) != 0 and r[0] != '0': all_arr.append(int(''.join(r))) return for n in arr: narr = arr[:] narr.remove(n) r.append(str(n)) build(narr,r,all_arr) r.remove(str(n)) run([0,4,0,0,0,0,0,0,0],0)
全部评论
(5) 回帖