Birinchisida
Download 90.45 Kb.
|
File 1
- Bu sahifa navigatsiya:
- File 14 Haqiqiy raqamlar fayli berilgan. Uning elementlarining o‘rtacha arifmetik qiymatini toping. Kod (Python)
File 13 Butun sonlar fayli berilgan.Ikkita yangi fayl yarating ,birinchisida asl fayldagi ijobiy raqamlar (teskari tartibda) ,ikkinchisida esa salbiy raqamlar mavjud (shuningdek ,teskari tartibda).Agar manba faylida ijoyib yoki salbiy raqamlar bo’lmasa ,natijada mos keladigan faylni bo’sh qoldiring . Kod (Python) def EmptyFile(fname): open(fname, 'w').close() def Write2File(fname,line): try: f = open(fname, "a") try: f.write(line) finally: f.close() except IOError: print('Write error: ',fname) f_source = "file13_source.txt" f_positive = "file13_p.txt" f_negative = "file13_n.txt" EmptyFile(f_positive) EmptyFile(f_negative) try: with open(f_source,'r') as f: lines = f.readlines() #print(lines) for line in reversed(lines): k = int(line.strip()) if k > 0: Write2File(f_positive,str(k)+'\n') elif k < 0: Write2File(f_negative,str(k)+'\n') except IOError: print('Open error: ',f_source) File 14 Haqiqiy raqamlar fayli berilgan. Uning elementlarining o‘rtacha arifmetik qiymatini toping. Kod (Python) import random import numpy def EmptyFile(fname): open(fname, 'w').close() def GenerateLine(fname): EmptyFile(f_source) N = random.randrange(1,20) line = "" L = [] for i in range(N): x = format(random.uniform(-10,10),'.1f') L.append(x) line = "; ".join(L) try: f = open(fname, "w") try: f.write(line) finally: f.close() except IOError: print('Write error: ',fname) print(line) f_source = "file14_source.txt" GenerateLine(f_source) try: with open(f_source,'r') as f: lines = f.readlines() l_float = [] print(lines) for line in reversed(lines): for i in line.split("; "): l_float.append(float(i)) print(l_float) x = numpy.mean(l_float) print("Mean:",format(x,'.3f')) except IOError: print('Open error: ',f_source) Download 90.45 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling