Advertisement
jovanovski

ОС Лаб5

Apr 1st, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Print the contents of the files listed on the command line.
  3. import sys, os
  4. from string import *
  5. if len(sys.argv) == 1:
  6.     sys.exit("Usage: "+sys.argv[0]+" files...");
  7. for fn in sys.argv[1:]:
  8.     fin = open(fn, 'r')
  9.     filename = find(fn, '.')
  10.     if filename != -1:
  11.         fn = fn[0:filename] + ".tsv"
  12.     fin2 = open(fn, 'w')
  13.     while 1:
  14.         line = fin.readline().rstrip()
  15.         if not line: break;
  16.         parts = split(line, ',')
  17.         newline = ""
  18.         for part in parts[0:]:
  19.             newline = newline + part + "\t"
  20.         newline = newline + "\n"
  21.         fin2.write(newline)
  22.     fin.close()
  23.     fin2.close()
  24.  
  25.    
  26. ######### vtora zadaca
  27.  
  28. #!/usr/bin/python
  29. import sys, os, glob
  30. from string import *
  31. totalka = 0
  32. files = glob.glob('*.py')
  33. files += glob.glob('*.sh')
  34. for file in files[0:]:
  35.     #print str(os.stat(file).st_size) + " za " + file
  36.     totalka += os.stat(file).st_size
  37. print totalka
  38.  
  39. ######### treta zadaca
  40.  
  41. #!/usr/bin/python
  42. import sys, os, glob
  43. from string import *
  44.  
  45. if len(sys.argv) == 1:
  46.     sys.exit("Usage: "+sys.argv[0]+" files...");
  47. mapa = {}
  48. for fn in sys.argv[1:]:
  49.     fin = open(fn, 'r')
  50.     while 1:
  51.         line = fin.readline().rstrip()
  52.         if not line: break;
  53.         line = line.replace(".", "")
  54.         line = line.replace(",", "")
  55.         line = line.replace("!", "")
  56.         line = line.replace(";", "")
  57.         line = line.replace("\t", "")
  58.         line = line.replace("?", "")
  59.         parts = split(line, ' ')
  60.         for part in parts[0:]:
  61.             part = part.upper();
  62.             if len(part.) > 3:
  63.                 if mapa.has_key(part):
  64.                     mapa[part]+=1;
  65.                 else:
  66.                     mapa[part]=1
  67.        
  68.     fin.close()
  69. for key, value in sorted(mapa.iteritems(), key=lambda (k,v): (v,k), reverse=True):
  70.     print "%s: %s" % (key, value)
  71.    
  72.    
  73.    
  74.    
  75. ######### cetvrta zadaca
  76.  
  77. #!/usr/bin/python
  78. import sys, os, glob
  79. from string import *
  80. brojnapredmeti=0
  81. predmeti = []
  82. pprosek = {}
  83. fin = open("rezultati.txt", 'r')
  84. line = fin.readline().rstrip()
  85. if not line: sys.exit("Prazen fajl!");
  86. parts = split(line, '\t')
  87. for part in parts[1:]:
  88.     predmeti.append(part)
  89.     brojnapredmeti+=1
  90.     pprosek[part] = 0
  91. print "Rabotime so slednive predmeti:"
  92. print predmeti
  93. brojnastudenti=0
  94. tmpprosek=0
  95. while 1:
  96.     line = fin.readline().rstrip()
  97.     if not line: break;
  98.     brojnastudenti+=1
  99.     tmpprosek=0
  100.     brojac=0
  101.     parts = split(line, '\t')
  102.     for part in parts[1:]:
  103.         pprosek[predmeti[brojac]]+=int(part)
  104.         tmpprosek+=int(part)
  105.         brojac+=1
  106.     odnos = (tmpprosek*1.0)/brojnapredmeti
  107.     print parts[0] + " ima prosek: " + str(odnos)
  108. print
  109. print "Prosek za predmeti:"
  110. for predmet in predmeti[0:]:
  111.     tmpprosek = (pprosek[predmet]*1.0)/brojnastudenti
  112.     print predmet + ": " + str(tmpprosek)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement