Skip to content

Instantly share code, notes, and snippets.

@is
Created July 27, 2016 02:25
Show Gist options
  • Select an option

  • Save is/5bb8dc82a3722accbd084768a6ad6ff8 to your computer and use it in GitHub Desktop.

Select an option

Save is/5bb8dc82a3722accbd084768a6ad6ff8 to your computer and use it in GitHub Desktop.
import time
ts = time.time()
fin = open('144341511030664.txt')
lines = fin.readlines()
fin.close()
pairs = []
sets = []
setbase = 0
def newset():
s = set()
sets.append(s)
return len(sets) - 1, s
for line in lines:
t0, t1 = line.split()
t0 = int(t0) - 1
t1 = int(t1) - 1
pairs.append((t0, t1))
print(len(pairs))
print('S0')
tags = [-1] * 100000
c = 0
for pair in pairs:
t0 = pair[0]
t1 = pair[1]
si0 = tags[t0]
si1 = tags[t1]
if (si0 == -1 and si1 == -1):
sid, s = newset()
s.add(t0)
s.add(t1)
tags[t0] = sid
tags[t1] = sid
elif (si0 != -1 and si1 == -1):
s = sets[si0]
s.add(t1)
tags[t1] = si0
elif (si0 == -1 and si1 != -1):
s = sets[si1]
s.add(t0)
tags[t0] = si1
else:
if si0 != si1:
s0 = sets[si0]
s1 = sets[si1]
if len(s0) > len(s1):
for s in s1:
tags[s] = si0
s0.add(s)
else:
for s in s0:
tags[s] = si1
s1.add(s)
c += 1
if c % 10000 == 0:
print '.'
res = {}
for i in tags:
if (i != -1):
s = sets[i]
res[id(s)] = s
print('sets:', len(sets))
print('uniq sets', len(res))
s = [len(x) for x in res.values()]
print('sum-0', sum(s))
c = 0
for i in tags:
if i == -1:
c += 1
print(c)
print(len(res) + c)
print(time.time() - ts)
import time
import sys
ts = time.time()
fin = open('144341511030664.txt')
lines = fin.readlines()
fin.close()
pairs = []
for line in lines:
t0, t1 = line.split()
t0 = int(t0) - 1
t1 = int(t1) - 1
if (t1 < t0):
pairs.append((t1, t0))
else:
pairs.append((t0, t1))
print(len(pairs))
print('S0')
tags = [-1] * 100000
def header(i):
begin = i
path = [begin]
while tags[i] != -1:
i = tags[i]
if (i in path):
path.append(i)
print path
sys.exit(0)
path.append(i)
return i
c = 0
for pair in pairs:
t0 = pair[0]
t1 = pair[1]
i0 = header(t0)
i1 = header(t1)
if (i0 != i1):
tags[i0] = i1
c+=1
if c%10000:
print "."
c = 0
for i in tags:
if i == -1:
c += 1
print(c)
print(time.time() - ts)
import requests
import time
c = range(10)
cc = [ 0 for x in c]
tc = [ None for x in c]
def P(n):
return requests.get('http://www.qlcoder.com/train/spider3/%d' % n).text
def M():
for i in c:
t = P(i + 1)
if t == tc[i]:
continue
tc[i] = t
cc[i] += 1
def dump():
ss = [ (cc[i], i) for i in range(10)]
ss.sort(reverse = True)
print('-'.join(["%d:%d" % (x[1] + 1, x[0]) for x in ss]))
print('-'.join(["%d" % (x[1] + 1) for x in ss]))
for i in range(20000):
M()
dump()
import p3data as data
import math
import numpy as np
def str2pt(s):
tokens = s.split()
return [ (float(tokens[o * 2]), float(tokens[o * 2 + 1])) for o in range(len(tokens) / 2)]
def d(p1, p2):
x = p1[0] - p2[0]
y = p1[1] - p2[1]
return math.sqrt(x * x + y * y);
def buildDArray(ps, cs):
da = np.empty([len(ps), len(cs)], dtype=float)
for p in range(len(ps)):
for c in range(len(cs)):
da[p, c] = d(ps[p], cs[c])
return da
def res(da, ps, cs):
db = np.copy(da)
sum = 0
lc = len(cs)
lp = len(ps)
aps = len(ps)
pair = []
while aps > 0:
o = da.argmin()
oc = o % lc
op = (o - oc) / lc
if (aps == 40):
op, oc = 41, 41
print(op, oc)
print(da[op, oc])
sum += da[op, oc]
d = da[op, oc]
m = 99999
pair.append([op, oc])
for i in range(lc):
if i != oc:
if (db[op, i] < m):
m = db[op, i]
da[op, i] = 99999
for i in range(lp):
if i != op:
if (db[i, oc] < m):
m = db[i, oc]
da[i, oc] = 99999
print("%d - %.1f %.1f %.1f\n" % (aps, d, m, sum))
aps -= 1
print(pair)
print(sum)
print ("------")
for i in pair:
print("P%d-U%d" % (i[0] + 1, i[1] + 1))
if __name__ == '__main__':
CS = str2pt(data.CP)
PS = str2pt(data.PP)
da = buildDArray(PS, CS)
res(da, PS, CS)
# print(da.min())
# print(da.argmin(axis=0))
# print(da[da.amin()])
PP = """6.0 57.0
85.0 11.0
93.0 42.0
57.0 86.0
47.0 38.0
44.0 87.0
52.0 41.0
10.0 99.0
7.0 42.0
93.0 51.0
80.0 87.0
12.0 52.0
75.0 0.0
93.0 92.0
23.0 57.0
85.0 6.0
36.0 13.0
89.0 78.0
14.0 38.0
5.0 94.0
55.0 94.0
96.0 3.0
41.0 63.0
4.0 75.0
71.0 26.0
63.0 22.0
76.0 22.0
85.0 54.0
7.0 31.0
26.0 95.0
2.0 79.0
97.0 45.0
25.0 28.0
27.0 81.0
44.0 65.0
32.0 46.0
62.0 35.0
38.0 20.0
11.0 36.0
52.0 80.0
27.0 36.0
60.0 3.0
79.0 9.0
39.0 23.0
65.0 23.0
46.0 33.0
80.0 22.0
31.0 3.0
98.0 81.0
87.0 7.0"""
CP="""89.0 64.0
7.0 55.0
3.0 74.0
67.0 80.0
12.0 91.0
2.0 68.0
97.0 60.0
49.0 0.0
12.0 22.0
88.0 52.0
23.0 27.0
81.0 4.0
44.0 61.0
51.0 10.0
71.0 48.0
9.0 36.0
70.0 9.0
31.0 1.0
75.0 75.0
51.0 17.0
78.0 62.0
27.0 23.0
60.0 32.0
55.0 69.0
39.0 96.0
77.0 52.0
45.0 87.0
61.0 94.0
56.0 89.0
55.0 21.0
15.0 90.0
32.0 44.0
99.0 43.0
60.0 15.0
21.0 22.0
17.0 31.0
36.0 94.0
80.0 28.0
73.0 94.0
75.0 82.0
94.0 18.0
58.0 4.0
35.0 76.0
63.0 19.0
12.0 88.0
92.0 48.0
36.0 68.0
72.0 17.0
74.0 30.0
46.0 95.0
49.0 17.0
60.0 93.0
30.0 81.0
66.0 64.0
16.0 62.0
22.0 8.0
47.0 48.0
1.0 32.0
13.0 55.0
83.0 89.0
44.0 29.0
96.0 17.0
80.0 82.0
28.0 8.0
7.0 35.0
7.0 42.0
66.0 99.0
77.0 75.0
80.0 26.0
40.0 21.0
61.0 36.0
86.0 22.0
86.0 59.0
20.0 3.0
14.0 52.0
32.0 99.0
31.0 96.0
51.0 48.0
69.0 37.0
6.0 2.0
68.0 30.0
20.0 22.0
45.0 90.0
23.0 1.0
59.0 31.0
35.0 50.0
68.0 23.0
28.0 44.0
32.0 5.0
32.0 56.0
21.0 76.0
58.0 48.0
39.0 33.0
78.0 63.0
56.0 32.0
75.0 17.0
60.0 24.0
37.0 57.0
11.0 67.0
0.0 29.0"""
import os
c = 194285
fin = open('santi.txt', encoding='utf8')
txt = fin.read()
fin.close()
print(len(txt))
chars = {}
for c in txt:
try:
chars[c] += 1
except KeyError:
chars[c] = 1
print(len(chars))
chars = { x: chars[x] for x in chars if chars[x] > 100}
print(len(chars))
words = {}
for i in range(len(txt) - 1):
word = txt[i:i + 2]
c0 = word[0]
c1 = word[1]
if c0 not in chars or c1 not in chars:
continue
try:
words[word] += 1
except KeyError:
words[word] = 1
wordsv = {}
maxv = 0
maxw = None
for word in words:
c0 = word[0]
c1 = word[1]
v = words[word] * 1.0 / chars[c0] / chars[c1]
if (v >= maxv):
maxv = v
maxw = word
print("%d,%f,%s" % (maxv, maxv * 194285, maxw))
print(len(words))
import sys
import requests
COOKIES = {
'Hm_lpvt_420590b976ac0a82d0b82a80985a3b8a':'1445434795',
'Hm_lvt_420590b976ac0a82d0b82a80985a3b8a':'1444961736',
'XSRF-TOKEN': 'eyJpdiI6InVOTWkrYStoaEZiZnB2SlhUSlZuYWc9PSIsInZhbHVlIjoiTGtKVFpsUm0zcHpBa3JjdzVRSDdXSHZNYlZZeGhPRHJJeTIxb045OGhlRnRDdGFUVWQzQ25kOStpSmxIQWNhOTZqMkQ5Q1NIM2NtTDZMeHVWT1RIVWc9PSIsIm1hYyI6IjE1NDc0NTBkMDFkZjhiYTAyYjBiYTcwYzMwOWJlNjE3NTQ1MmE5OWNjMzczYzVkZGQ0MGRmMDc1MWUyZGE0MjIifQ%3D%3D',
'laravel_session':'eyJpdiI6IjFGVUpUXC9ySndHTjF6VmlLOE1VZlF3PT0iLCJ2YWx1ZSI6Im9zSWgrTUFNWFFVNWQzREIwUW9uN00wbUdaTElxdHV6akVjK0NSVzAwV2tSMFFmdzJTZzVZNE4xSlNjXC9JR21VOVFVdVFrNlN1cFk3MHc2ajV2TzRFZz09IiwibWFjIjoiMTRlMGJkNDE3MzhjYzJhYTNhMzg5ZTkwMjY2Nzc3ZTdlZTdhM2EyNWRjZDI0ZmExMDM2OTJmN2U2MjU4MzA1MyJ9'
}
HEADERS = {
'referer': 'http://cpc.people.com.cn',
}
DATA = {
'answer': 'referer',
'_token': 'XKJxEWyGm2yTrvzkUVO1KAJq46GiajqDEkOSrpNT'
}
r = requests.post('http://www.qlcoder.com/task/17/solve',
cookies = COOKIES, headers = HEADERS, data = DATA)
print(r.request)
print(r.request.body)
print(r.text)
print('OK')
import smtplib
fromaddr = 'is-validate@office.makenv.com'
toaddr = 'yuxin@makenv.com'
s = smtplib.SMTP('192.168.1.137', 25, 'office.makenv.com')
s.set_debuglevel(1)
msg = '''From: %s
To: %s
Subject: sample mail
This is a test mail
.
''' % (fromaddr, toaddr)
#s.login('m0@fog.io8.org', '2b3e8fbb762d44ea9de8b8b20d94996f')
s.sendmail(fromaddr, toaddr, msg)
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment