Last active
August 29, 2015 14:00
-
-
Save xunkai55/11076962 to your computer and use it in GitHub Desktop.
test Shakespeare's converge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import random | |
def generate_by_shuffle(words_len_lst): | |
rst = words_len_lst[:] | |
random.shuffle(rst) | |
return rst | |
def calc_a(wll, sel_ratio): | |
m = len(wll) | |
n = len(wll) * sel_ratio | |
flag = [0] * m | |
for i in range(n): | |
t = i | |
while t < m: | |
flag[t] += 1 | |
t += wll[t] | |
for i in range(m): | |
if flag[i] == n: | |
return i | |
return -1 | |
def calc_b(wll): | |
m = len(wll) | |
flag = [0] * m | |
for i in range(m): | |
t = i | |
while t < m: | |
flag[t] += 1 | |
t += wll[t] | |
for j in reversed(range(i, m)): | |
if flag[j] == i + 1: | |
break | |
else: | |
return (i - 1) / m | |
return 1.0 | |
origin = ''' | |
If we shadows have offended | |
Think but this and all is mended | |
That you have but slumbered here | |
While these visions did appear | |
And this weak and idle theme | |
No more yielding but a dream | |
Gentles do not reprehend | |
If you pardon we will mend | |
And, as I am an honest Puck | |
If we have unearned luck | |
Now to scape the serpents tongue | |
We will make amends ere long | |
Else the Puck a liar call | |
So good night unto you all | |
Give me your hands if we be friends | |
And Robin shall restore amends | |
''' | |
owll = map(len, origin.split()) | |
if __name__ == "__main__": | |
tc = input("Test Case Number > ") | |
c20 = 0 | |
c30 = 0 | |
c40 = 0 | |
sum_b = 0 | |
for tid in range(tc): | |
wll = generate_by_shuffle(owll) | |
vb = calc_b(wll) | |
print "Test %d value_b: %.2f" % (tid, vb) | |
if vb >= 0.2: | |
c20 += 1 | |
if vb >= 0.3: | |
c30 += 1 | |
if vb >= 0.4: | |
c40 += 1 | |
sum_b += vb | |
print "Pass with ratio = 0.2: ", c20 / tc | |
print "Pass with ratio = 0.3: ", c30 / tc | |
print "Pass with ratio = 0.4: ", c40 / tc | |
print "Average B: ", sum_b / tc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment