Birinchisida


Download 90.45 Kb.
bet1/3
Sana06.04.2023
Hajmi90.45 Kb.
#1332101
  1   2   3
Bog'liq
File 1


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)



  1. def EmptyFile(fname):

  2. open(fname, 'w').close()


  3. def Write2File(fname,line):

  4. try:

  5. f = open(fname, "a")

  6. try:

  7. f.write(line)

  8. finally:

  9. f.close()

  10. except IOError:

  11. print('Write error: ',fname)


  12. f_source = "file13_source.txt"

  13. f_positive = "file13_p.txt"

  14. f_negative = "file13_n.txt"

  15. EmptyFile(f_positive)

  16. EmptyFile(f_negative)


  17. try:

  18. with open(f_source,'r') as f:

  19. lines = f.readlines()

  20. #print(lines)

  21. for line in reversed(lines):

  22. k = int(line.strip())

  23. if k > 0:

  24. Write2File(f_positive,str(k)+'\n')

  25. elif k < 0:

  26. Write2File(f_negative,str(k)+'\n')

  27. except IOError:

  28. print('Open error: ',f_source)



File 14
Haqiqiy raqamlar fayli berilgan. Uning elementlarining o‘rtacha arifmetik qiymatini toping.

Kod  (Python)



  1. import random

  2. import numpy


  3. def EmptyFile(fname):

  4. open(fname, 'w').close()


  5. def GenerateLine(fname):

  6. EmptyFile(f_source)

  7. N = random.randrange(1,20)

  8. line = ""

  9. L = []

  10. for i in range(N):

  11. x = format(random.uniform(-10,10),'.1f')

  12. L.append(x)

  13. line = "; ".join(L)

  14. try:

  15. f = open(fname, "w")

  16. try:

  17. f.write(line)

  18. finally:

  19. f.close()

  20. except IOError:

  21. print('Write error: ',fname)

  22. print(line)


  23. f_source = "file14_source.txt"

  24. GenerateLine(f_source)


  25. try:

  26. with open(f_source,'r') as f:

  27. lines = f.readlines()

  28. l_float = []

  29. print(lines)

  30. for line in reversed(lines):

  31. for i in line.split("; "):

  32. l_float.append(float(i))

  33. print(l_float)

  34. x = numpy.mean(l_float)

  35. print("Mean:",format(x,'.3f'))


  36. except IOError:

  37. print('Open error: ',f_source)




Download 90.45 Kb.

Do'stlaringiz bilan baham:
  1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling