Birinchisida


File 25 Haqiqiy raqamlar fayli berilgan.Undagi barcha elementlarni kvadratlari bilan almashtiring . Код (Python)


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

File 25
Haqiqiy raqamlar fayli berilgan.Undagi barcha elementlarni kvadratlari bilan almashtiring .
Код (Python)

  1. # -*- coding: utf-8 -*-

  2. import random

  3. import string

  4. import os

  5. import sys


  6. def GenerateReals(fname):

  7. N = random.randint(2,15)

  8. print("N = ",N)

  9. L = []

  10. x = round(random.uniform(0, 10),1)

  11. L.append(x)

  12. for i in range(1,N):

  13. lst_rnd = [round(random.uniform(0, 10),1) for _ in range(0,10)]

  14. try:

  15. lst_rnd.remove(x)

  16. except:

  17. pass

  18. x = random.choice(lst_rnd)

  19. L.append(x)

  20. print(L)

  21. try:

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

  23. try:

  24. for x in L:

  25. line = str(x)+"\n"

  26. f.write(line)

  27. finally:

  28. f.close()

  29. except IOError:

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



  31. file1 = "file25.txt"

  32. GenerateReals(file1)

  33. print("Read from:",file1)


  34. try:

  35. #file for temporary data

  36. N = random.randrange(5,8)

  37. S = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase \

  38. + string.digits) for _ in range(N))

  39. temp_file = "temp_" + S + ".txt"

  40. #print()

  41. print("Temp file:",temp_file)


  42. with open(file1, 'r') as f_in, open(temp_file, 'w') as f_out:

  43. for line in f_in:

  44. x = float(line.strip())

  45. y = x * x

  46. print("%1.1f : %2.2f" % (x, y))

  47. #print(x,":",y)

  48. s = str("%2.2f" % y)+'\n'

  49. f_out.write(s)


  50. #sys.exit()


  51. with open(file1, 'w') as f_out, open(temp_file, 'r') as f_in:

  52. for line in f_in:

  53. f_out.write(line)


  54. try:

  55. os.remove(temp_file)

  56. except OSError as e:

  57. print("\nError:", e)


  58. except IOError:

  59. print('Open error: ',file1)



File 26
Haqiqiy raqamlar fayli berilgan.Undagi minimal va maksimal elementlarni almashtiring.
Kod  (Python)
# -*- coding: utf-8 -*-

  1. import random

  2. import string

  3. import os

  4. import sys


  5. def GenerateReals(fname):

  6. N = random.randint(2,15)

  7. print("N = ",N)

  8. L = []

  9. x = round(random.uniform(0, 10),1)

  10. L.append(x)

  11. for i in range(1,N):

  12. lst_rnd = [round(random.uniform(0, 10),1) for _ in range(0,10)]

  13. try:

  14. lst_rnd.remove(x)

  15. except:

  16. pass

  17. x = random.choice(lst_rnd)

  18. L.append(x)

  19. print(L)

  20. try:

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

  22. try:

  23. for x in L:

  24. line = str(x)+"\n"

  25. f.write(line)

  26. finally:

  27. f.close()

  28. except IOError:

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



  30. file1 = "file26.txt"

  31. GenerateReals(file1)

  32. print("Read from:",file1)


  33. try:

  34. #file for temporary data

  35. N = random.randrange(5,8)

  36. S = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase \

  37. + string.digits) for _ in range(N))

  38. temp_file = "temp_" + S + ".txt"

  39. #print()

  40. print("Temp file:",temp_file)


  41. with open(file1, 'r') as f_in, open(temp_file, 'w') as f_out:

  42. line = f_in.readline()

  43. x = float(line.strip())

  44. mx = x

  45. mn = x

  46. mx_line = 1

  47. mn_line = 1

  48. line_num = 1

  49. s = str(x)+'\n'

  50. f_out.write(s)

  51. for line in f_in:

  52. line_num += 1

  53. x = float(line.strip())

  54. if mx < x:

  55. mx = x

  56. mx_line = line_num

  57. elif mn > x:

  58. mn = x

  59. mn_line = line_num

  60. s = str(x)+'\n'

  61. f_out.write(s)


  62. print("Min: %1.1f : %2d" % (mn,mn_line))

  63. print("Max: %1.1f : %2d" % (mx,mx_line))


  64. swap1_value = mx

  65. swap1_line = mn_line

  66. swap2_value = mn

  67. swap2_line = mx_line


  68. print("Swap1: %1.1f : %2d" % (swap1_value,swap1_line))

  69. print("Swap2: %1.1f : %2d" % (swap2_value,swap2_line))


  70. #sys.exit()


  71. with open(file1, 'w') as f_out, open(temp_file, 'r') as f_in:

  72. line_num = 0

  73. for line in f_in:

  74. line_num += 1

  75. if line_num == swap1_line:

  76. f_out.write(str(swap1_value)+'\n')

  77. elif line_num == swap2_line:

  78. f_out.write(str(swap2_value)+'\n')

  79. else:

  80. f_out.write(line)

  81. print(line,end="")


  82. try:

  83. os.remove(temp_file)

  84. except OSError as e:

  85. print("\nError:", e)


  86. except IOError:

  87. print('Open error: ',file1)



File 27
A1,A2,…AN elementlari bo’lgan butun sonlar fayli berilgan.(N- fayldagi elementlari soni ).Uning elementlarining asl joylashuvini quyidagi bilan almashtiring : A1, AN, A2, AN−1, A3, ...
Kod  (Python)

  1. # -*- coding: utf-8 -*-

  2. import random

  3. import string

  4. import os

  5. import sys

  6. import math


  7. def GenerateInts(fname):

  8. #generate random real numbers and save them to file

  9. N = random.randint(2,15)

  10. N = 9

  11. #N = 10

  12. print("N = ",N)

  13. try:

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

  15. try:

  16. for i in range(0,N):

  17. #x = random.randint(1,11)

  18. x = i + 1

  19. line = str(x)+"\n"

  20. f.write(line)

  21. finally:

  22. f.close()

  23. except IOError:

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


  25. def CountFile(fname):

  26. #count number of rows in given text-file

  27. N = 0

  28. try:

  29. with open(fname,'r') as f:

  30. for line in f:

  31. N += 1

  32. #print(N,":",line)

  33. except IOError:

  34. print("Open error:",fname)

  35. return -1

  36. finally:

  37. return N


  38. def GetLineK(fname,K):

  39. line = ""

  40. N = 0

  41. try:

  42. with open(fname,'r') as f:

  43. for line in f:

  44. N += 1

  45. if K == N:

  46. break

  47. except IOError:

  48. print("Open error:",fname)

  49. return -1

  50. finally:

  51. return line


  52. file1 = "file27.txt"

  53. GenerateInts(file1)

  54. print("Read from:",file1)


  55. try:

  56. #file for temporary data

  57. N = random.randrange(5,8)

  58. S = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase \

  59. + string.digits) for _ in range(N))

  60. temp_file_1 = "temp27_" + S + "_1.txt"

  61. temp_file_2 = "temp27_" + S + "_2.txt"

  62. #print()

  63. print("Temp file 1:",temp_file_1)

  64. print("Temp file 2:",temp_file_2)


  65. N = CountFile(file1)

  66. N1 = int(math.ceil(N/2))

  67. N2 = N - int(N/2)


  68. try:

  69. f_in = open(file1, 'r')

  70. try:

  71. print("===== Part 1 =====")

  72. f_out = open(temp_file_1, 'w')

  73. for i in range(1,N1+1):

  74. line = f_in.readline()

  75. print(i,":",line,end="")

  76. f_out.write(line)

  77. except IOError:

  78. print('Read Error: ',temp_file_1)

  79. finally:

  80. f_out.close()


  81. try:

  82. print("===== Part 2 =====")

  83. f_out = open(temp_file_2, 'w')

  84. for i in range(N,N2,-1):

  85. #print(i)

  86. line = GetLineK(file1,i)

  87. print(i,":",line,end="")

  88. f_out.write(line)

  89. except IOError:

  90. print('Read Error: ',temp_file_2)

  91. finally:

  92. f_out.close()


  93. except IOError:

  94. print('Read Error: ',file1)

  95. finally:

  96. f_in.close()


  97. try:

  98. f_out = open(file1, 'w')

  99. try:

  100. f_in1 = open(temp_file_1, 'r')

  101. f_in2 = open(temp_file_2, 'r')

  102. for i in range(0,N2):

  103. line = f_in1.readline()

  104. f_out.write(line)

  105. line = f_in2.readline()

  106. f_out.write(line)

  107. finally:

  108. f_in2.close()

  109. f_in1.close()

  110. f_out.close()


  111. except IOError:

  112. print('Write Error: ',file1)


  113. try:

  114. os.remove(temp_file_1)

  115. os.remove(temp_file_2)

  116. except OSError as e:

  117. print("\nError:", e)


  118. except IOError:

  119. print('Open error: ',file1)



File 28


Haqiqiy sonlar fayli berilgan .Fayldagi har bir elementni ,boshlang’ich va oxirgi elementlardan tashqari ,uning o’rtacha arifmetik qiymatini oldingi va keyingi elementlar bilan almashtiring
Kod (Python)
# -*- coding: utf-8 -*-

  1. import random


  2. def GenerateReals(fname):

  3. #generate random real numbers and save them to file

  4. N = random.randint(2,15)

  5. #N = 9

  6. print("N = ",N)

  7. L = []

  8. x = round(random.uniform(0, 10),1)

  9. L.append(x)

  10. for i in range(1,N):

  11. lst_rnd = [round(random.uniform(0, 10),1) for _ in range(0,10)]

  12. try:

  13. lst_rnd.remove(x)

  14. except:

  15. pass

  16. x = random.choice(lst_rnd)

  17. L.append(x)

  18. print(L)

  19. try:

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

  21. try:

  22. for x in L:

  23. line = str(x)+"\n"

  24. f.write(line)

  25. finally:

  26. f.close()

  27. except IOError:

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


  29. f_input = "file28_in.txt"

  30. f_output = "file28_out.txt"

  31. GenerateReals(f_input)

  32. print("Read from:",f_input)

  33. print("Write to:",f_output)


  34. try:

  35. with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:

  36. line = f_in.readline()

  37. x1 = float(line.strip())

  38. f_out.write(line)

  39. line = f_in.readline()

  40. x2 = float(line.strip())

  41. for line in f_in:

  42. x = float(line.strip())

  43. x_avg = (x1 + x2 + x) / 3

  44. s_avg = str(x_avg)+"\n"

  45. f_out.write(s_avg)

  46. x1 = x2

  47. x2 = x

  48. f_out.write(line)


  49. except IOError:

  50. print('Open error: ',file1)



File 29
50 dan ortiq elementni o'z ichiga olgan butun sonlar fayli berilgan. Fayldan kerakli sonli chekli elementlarni olib tashlash orqali uning hajmini 50 ta elementgacha kamaytiring.
Kod  (Python)

  1. # -*- coding: utf-8 -*-

  2. import random


  3. def GenerateNumbers(fname):

  4. N = random.randint(50,65)

  5. print("N = ",N)

  6. L = []

  7. x = random.randrange(1,10)

  8. L.append(x)

  9. for i in range(1,N):

  10. lst_rnd = list(range(1,10))

  11. lst_rnd.remove(x)

  12. x = random.choice(lst_rnd)

  13. L.append(x)

  14. print(L)

  15. try:

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

  17. try:

  18. for x in L:

  19. line = str(x)+"\n"

  20. f.write(line)

  21. finally:

  22. f.close()

  23. except IOError:

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


  25. f_input = "file29_in.txt"

  26. f_output = "file29_out.txt"

  27. GenerateNumbers(f_input)

  28. print("Read from:",f_input)

  29. print("Write to:",f_output)


  30. try:

  31. with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:

  32. for i in range(50):

  33. line = f_in.readline()

  34. f_out.write(line)


  35. except IOError:

  36. print('Open error: ',file1)



File 30
Juft sonli elementlardan iborat butun sonlar fayli berilgan.Berilgan fayldan elementlarning ikkinchi yarmini o’chiring.
Kod  (Python)

  1. # -*- coding: utf-8 -*-

  2. import random


  3. def GenerateNumbers(fname):

  4. N = random.randint(1,10) * 2

  5. print("N = ",N)

  6. L = []

  7. x = random.randrange(1,10)

  8. L.append(x)

  9. for i in range(1,N):

  10. lst_rnd = list(range(1,10))

  11. lst_rnd.remove(x)

  12. x = random.choice(lst_rnd)

  13. L.append(x)

  14. print(L)

  15. try:

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

  17. try:

  18. for x in L:

  19. line = str(x)+"\n"

  20. f.write(line)

  21. finally:

  22. f.close()

  23. except IOError:

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


  25. def CountFile(fname):

  26. #count number of rows in given text-file

  27. N = 0

  28. try:

  29. with open(fname,'r') as f:

  30. for line in f:

  31. N += 1

  32. #print(N,":",line)

  33. except IOError:

  34. print("Open error:",fname)

  35. return -1

  36. finally:

  37. return N


  38. f_input = "file30_in.txt"

  39. f_output = "file30_out.txt"

  40. GenerateNumbers(f_input)

  41. print("Read from:",f_input)

  42. print("Write to:",f_output)


  43. N = CountFile(f_input)

  44. N = int(round(N/2.0))


  45. try:

  46. with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:

  47. for i in range(N):

  48. line = f_in.readline()

  49. f_out.write(line)


  50. except IOError:

  51. print('Open error: ',file1)



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