Advertisement
Bkmz

Untitled

Mar 19th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from copy import deepcopy, copy
  3. from pdb import set_trace as st
  4.  
  5. from os import fork, pipe, read, write, close, fdopen
  6. from sys import exit
  7.  
  8.  
  9. import sys
  10. reload(sys)
  11. sys.setdefaultencoding('utf8')
  12.  
  13. THREADS = 4
  14. LEN = 6
  15. STEP = 1
  16.  
  17. pids = []
  18.  
  19.  
  20. def list_to_str(lst):
  21.     out = ""
  22.     for i in lst:
  23.         out += str(i)
  24.     return out
  25.  
  26.  
  27. STR = list()
  28.  
  29.  
  30. def generate_str():
  31.     global STR, STEP
  32.     STR[len(STR)-1] += STEP
  33.  
  34.     while STR[len(STR)-1] > 9:
  35.         # for i in reversed(xrange(STR))
  36.         i=len(STR)-1
  37.  
  38.         while STR[i]>9 and i>0:
  39.             STR[i] -= 10
  40.             STR[i-1] += 1
  41.             i -= 1
  42.  
  43.  
  44.     # return copy(STR)
  45.     return STR
  46.  
  47.  
  48. def check_str_loop():
  49.     global STR
  50.     flag1=0
  51.     flag2=0
  52.     res  =0
  53.  
  54.     for i in xrange(len(STR)-1):
  55.         if STR[i] > STR[i+1]:
  56.             flag1+=1
  57.  
  58.         if STR[i] < STR[i+1]:
  59.             flag2+=1
  60.  
  61.     if flag1 == len(STR)-1:
  62.         res+=1
  63.  
  64.     if flag2 == len(STR)-1:
  65.         res+=1
  66.  
  67.     return res
  68.  
  69.  
  70. def main():
  71.     global STR, STEP, ID, pids
  72.     def truth(str):
  73.         if int(str[0]) > 9:
  74.             return 0
  75.         else:
  76.             return 1
  77.  
  78.     for i in xrange(THREADS):
  79.        
  80.         r, w = pipe()
  81.         # r, w = fdopen(r,'r'), fdopen(w,'w')
  82.  
  83.         p = fork()
  84.  
  85.         if p == 0:
  86.             str = STR
  87.             avg = 0.0
  88.             index = 0
  89.             STEP = THREADS
  90.  
  91.             for j in xrange(LEN):
  92.                 STR.append(0)
  93.  
  94.             buf = copy(i)
  95.             iter = len(STR)-1
  96.  
  97.             while not buf == 0 and not iter == 0:
  98.                 STR[iter] = buf%10
  99.                 buf /= 10
  100.                 iter -= 1
  101.  
  102.             # print list_to_str(STR)
  103.  
  104.  
  105.             while truth(str):
  106.                 str = generate_str()
  107.                 status = check_str_loop()
  108.  
  109.                 # print "cur: %s\r" % (list_to_str(str)),
  110.                 if status > 0:
  111.                     # print "%s\r" % (" "*20),
  112.                     # print list_to_str(str)
  113.                     avg += float(list_to_str(str))
  114.                     index += 1
  115.  
  116.             # print "sum: %.2f" % (avg)
  117.             # print "ind: %i" % (index)
  118.             # print "avg: %.2f" % (avg/index)
  119.  
  120.             close(r)
  121.             write(w, "%i;%.2f" % (index, avg))
  122.  
  123.             sys.exit(0)
  124.         else:
  125.             pids.append([])
  126.             pids[i].append(p)
  127.             pids[i].append([r,w])
  128.  
  129.     # now working in parent thread:
  130.  
  131.     result = {"avg":0.0, "i":0}
  132.  
  133.     for i in xrange(THREADS):
  134.         # pids[i][1][1].close()
  135.         close(pids[i][1][1])
  136.         str = read(pids[i][1][0], 4096).split(";")
  137.         result['avg'] += float(str[1])
  138.         result['i'] += int(str[0])
  139.  
  140.  
  141.  
  142.     print "sum: %.2f" % (result['avg'])
  143.     print "ind: %i" % (result['i'])
  144.     print "avg: %.2f" % (result['avg']/result['i'])
  145.  
  146.  
  147.  
  148.  
  149.  
  150. if __name__ == '__main__':
  151.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement