-
Notifications
You must be signed in to change notification settings - Fork 0
/
try.py
219 lines (188 loc) · 4.45 KB
/
try.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 08 11:41:59 2016
@author: BoXiao
"""
import time
import random
import string
import math
import matplotlib.pyplot as plt
#file = open('words.txt')
#
#count1 = 0
#count2 = 0
#for line in file:
# count1+=1.0
# if not 'e' in line.strip():
# print line.strip()
# count2+=1
#
#print count2/count1
#def is_reverse(nu):
# if nu[:]==nu[::-1]:
# return True
# return False
#
#def is_fit(nu):
# if is_reverse(str(nu)[-4:]):
# if is_reverse(str(nu+1)[-5:]):
# if is_reverse(str(nu+2)[-5:-1]):
# if is_reverse(str(nu+3)[:]):
# return True
# return False
#
#for i in range(100000,1000000):
# if is_fit(i):
# print i
#
#def count_time(b):
# count=0
# now_age=0
# a=0
# while a<100 and b<100:
# if str(a).zfill(2)==str(b).zfill(2)[::-1]:
# count+=1
# if count == 6:
# now_age=a,b
# a+=1
# b+=1
# return [count,now_age]
#
#for i in range(99):
# if count_time(i)[0]==8:
# print count_time(i)[1]
#
#def readtxtap(f):
# t=time.time()
# s=[]
# for i in f:
# s.append(i.strip())
# print len(s)
# return time.time()-t
#
#
#def readtxtplus(f):
# t=time.time()
# s=[]
# for i in f:
# s = s + [i.strip()]
# print len(s)
# return time.time()-t
#
'''
def getf(f):
s = dict()
for i in f:
i=i.strip()
sort_i=tuple(sorted(i))
if not sort_i in s:
s[sort_i]=[i]
else:
s[sort_i].append(i)
return s
with open('words.txt') as file:
tup=getf(file)
longest = []
for key,value in tup.iteritems():
if len(value)>1:
longest.append((len(value),value))
longest.sort(reverse=True)
for tim,value in longest[:]:
print value
'''
def histogram(s):
d=dict()
for i in s:
d[i]=d.get(i,0)+1
return d
def chose_from_hist(s):
t=[]
for key,value in s.items():
t.extend([key]*value)
return random.choice(t)
def process_file(f,delete=True):
hist=dict()
if delete:
for line in f:
if line.startswith('*END*THE SMALL PRINT!'):
break
for line in f:
process_line(line,hist)
return hist
def process_line(line,hist):
line = line.replace('-',' ')
for word in line.split():
word = word.strip(string.whitespace+string.punctuation)
word=word.lower()
hist[word]=hist.get(word,0)+1
def print_most_common(hist,n=10):
s=[]
for key,value in hist.items():
s.append((value,key))
s.sort(reverse=True)
for key,value in s[:n]:
print value,'\t',key
def substract1(d1,d2):
s=dict()
for key in d1:
if not key in d2:
s[key]=None
return s
def substract2(d1,d2):
return set(d1.keys()).difference(set(d2.keys()))
def create_sum(t):
s=[t[0]]
length=len(t)
for i in range(1,length):
sumb=s[-1]
s.append(sumb+t[i])
return s
def bisect(sl,n):
length=len(sl)
if length==1:
if sl[0]>=n:
return 0
else:
return 1
if n<sl[length/2]:
if bisect(sl[0:length/2],n)==None:
return None
else:
return bisect(sl[0:length/2],n)
else:
if bisect(sl[length/2:],n)==None:
return None
else:
return length/2+bisect(sl[length/2:],n)
if __name__=='__main__':
with open('emma.txt') as file:
hist1 = process_file(file)
with open('words.txt') as file:
hist2 = process_file(file,False)
print_most_common(hist1,12)
# histdiff=substract1(hist1,hist2)
# for words in histdiff:
# print words,
print '\n'
print bisect([3,7,9],10)
keys=[]
values=[]
for key,value in hist1.items():
keys.append(key)
values.append(value)
# print values
sumlist=create_sum(values)
print len(sumlist),len(values),sumlist[-1]
print keys[bisect(sumlist,random.randint(1,len(values)))]
t=[]
for key,value in hist1.items():
t.append((value,key))
t.sort(reverse=True)
r=[]
f=[]
for i in range(len(t)):
r.append(math.log(i+1))
f.append(math.log(t[i][0]))
plt.plot(r,f)
plt.show()