Advertisement
Guest User

joel

a guest
Feb 20th, 2008
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. #!//usr/bin/env python
  2.  
  3. max_digit=12
  4. max_big=30
  5.  
  6. def get_add_operation():
  7.     import random
  8.     a=random.randint(1,max_big)
  9.     b=random.randint(1,a)
  10.     if random.random()<0.5:
  11.         return ('%d+%d?'%(a-b,b),a)
  12.     else:
  13.         return ('%d+%d?'%(b,a-b),a)
  14.  
  15. def get_mult_operation():
  16.     import random
  17.     a=random.randint(1,max_digit)
  18.     b=random.randint(1,max_digit)
  19.     return ('%dx%d?'%(a,b),a*b)
  20.  
  21. def get_minus_operation():
  22.     import random
  23.     a=random.randint(1,max_big)
  24.     b=random.randint(1,a)
  25.     return ('%d-%d?'%(a,b),a-b)
  26.  
  27. def get_div_operation():
  28.     import random
  29.     a=random.randint(1,max_digit)
  30.     b=random.randint(1,max_digit)
  31.     return ('%d/%d?'%(a*b,b),a)
  32.  
  33. op_funcs = [ get_add_operation, get_mult_operation, get_minus_operation, get_div_operation ]
  34.  
  35. def get_random_operation():
  36.     return random.choice(op_funcs)()
  37.  
  38. if __name__ == '__main__':
  39.     import time
  40.     import random
  41.     import cPickle
  42.     import sys
  43.     if len(sys.argv) < 2: raise "Usage : compute.py login"
  44.     ops_per_serie=15
  45.     try:
  46.         f=open("compute_stats_%s.pck" % sys.argv[1],"rb") #read stats on disk
  47.         operations=cPickle.load(f)
  48.         f.close()
  49.         op_list = operations.items()
  50.         op_list.sort(key=lambda i:sum(i[1][0])/len(i[1][0])) #sort by accuracy score
  51.         bad_ops = [a[0] for a in op_list if sum(a[1][0])/len(a[1][0]) < 0.9] #ops the user doesn't master
  52.         op_list.sort(key=lambda e:sum(e[1][1])/len(e[1][1]),reverse=True) #sort by answer time
  53.         bad_ops.extend([a[0] for a in op_list[:10]]) #ops the user is slow to answer
  54.         bad_ops=list(set(bad_ops)) #remove duplicates
  55.     except Exception, e:
  56.         operations={} #operations["5*2"] = [a,b,c,d,e] with a=1. if good, a=0. if wrong storing the 5 last results
  57.     bad_ops=[]
  58.     before = time.time()
  59.     errors = 0
  60.     for i in range(ops_per_serie):
  61.         op_string,expected=get_random_operation()
  62.         if random.random() < len(bad_ops)/(1.5*ops_per_serie):#train user specifically on stuff he doesn't master
  63.             #print "training on bad op"
  64.             op_string,expected = random.choice(bad_ops)
  65.         #print len(bad_ops)/(1.5*ops_per_serie) #badop training probability
  66.         scores,answer_times=operations.get((op_string,expected),([],[]))
  67.         print op_string
  68.         before_op=time.time()
  69.         try:
  70.             answer = int(raw_input())
  71.         except:
  72.             answer = expected-1 # this should be always wrong
  73.         answer_time=time.time()-before_op
  74.         if len(scores) > 5 : scores.pop(0) # keep only 5 scores
  75.         if len(answer_times) > 5 : answer_times.pop(0) # keep only 5 timings
  76.         answer_times.append(answer_time)
  77.         if answer != expected:
  78.             errors += 1
  79.             scores.append(0.)
  80.             operations[(op_string,expected)] = (scores,answer_times)
  81.             print "WRONG!"
  82.         else:
  83.             scores.append(1.)
  84.             operations[(op_string,expected)] = (scores,answer_times)
  85.             print "good."
  86.     total_time = time.time() - before
  87.     print "Time: %.2fs, Errors : %d, Total points : %d" % (total_time,errors,100*ops_per_serie*(5.-errors)/total_time)
  88.     cPickle.dump(operations,open("compute_stats_%s.pck"%sys.argv[1],"wb"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement