Advertisement
VladSmirN

task66_lab1_toi

Sep 22nd, 2020
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import math;
  2. n = [15,20,35,40]
  3. N = 110;
  4.  
  5. print("Извлечение 1");
  6. PA = [ni/N for ni in n];
  7. for i in range(len(PA)):
  8.     print('Событие A'+str(i)+'- вынут шар '+str(i)+', p(A'+str(i)+')='+str(n[i])+'/'+str(N));
  9. HA = 0;
  10. for i in range(len(PA)):
  11.     HA = HA  - PA[i]*math.log2(PA[i]);
  12. print("HA="+str(HA));  
  13.  
  14. print("Извлечение 2");
  15. N = N-1;
  16. PAB = [];
  17. for i in range(len(PA)):
  18.     pb = [];
  19.     for j in range(len(PA)):
  20.         nj = n[j]
  21.         if i == j :
  22.             nj = nj -1;
  23.         print('PA'+str(i)+'(B'+str(j)+')='+str(nj)+'/'+str(N));
  24.         pb.append(nj/N);
  25.     PAB.append(pb);  
  26.  
  27. print('\n');
  28. HAB = 0;
  29. for i in range(len(PA)):
  30.     h = 0 ;
  31.     for j in range(len(PA)):
  32.         h = h - PAB[i][j]*math.log2(PAB[i][j]);
  33.     print('HA'+str(i)+'(B)='+str(h));
  34.     HAB = HAB + h*PA[i];
  35. print('\n');
  36. print('HAB='+str(HAB)+'\n');
  37.  
  38. print('HAB + HA='+str(HAB+HA));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement