Birinchisida


File 15 Haqiqiy raqamlar fayli berilgan. Uning juft sonli elementlari yig‘indisini toping. Kod  (Python)


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

File 15
Haqiqiy raqamlar fayli berilgan. Uning juft sonli elementlari yig‘indisini 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)



File 16
Butun sonlar fayli berilgan. Undagi qatorlar sonini toping (ya'ni, ketma-ket bir xil elementlar to'plami). Masalan, 1, 5, 5, 5, 4, 4, 5 elementlarga ega fayl uchun natija 4 ga teng.

Kod  (Python)



  1. import random

  2. import numpy


  3. def GenerateLine(fname):

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

  5. line = ""

  6. L = []

  7. for i in range(N):

  8. x = str(random.randint(1,3))

  9. L.append(x)

  10. line = ", ".join(L)

  11. try:

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

  13. try:

  14. f.write(line)

  15. finally:

  16. f.close()

  17. except IOError:

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

  19. print(line)


  20. f_source = "file16_source.txt"

  21. GenerateLine(f_source)


  22. try:

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

  24. lines = f.readlines()

  25. lst = []

  26. print(lines)

  27. for line in reversed(lines):

  28. for i in line.split(", "):

  29. lst.append(int(i))

  30. print(lst)

  31. k = 1

  32. i_prev = lst[0]

  33. for i in lst[1::]:

  34. if i != i_prev:

  35. k += 1

  36. i_prev = i

  37. print("Number of series:",k)


  38. except IOError:

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



File 17
Butun sonlar fayli berilgan .Manba faylning barcha seriyalari uzunligini o’z ichiga olgan yangi butun son faylini yarating (ketma –ket bir xil elementlar to’plami ,seriya uzunligi esa bu elementlarning soni).Misol uchun ,1,5,5,5,4,4,5 elementlarga ega bo’lgan manba fayl uchun natijada olingan faylning mazmuni quyidagicha bo’lishi kerak : 1,3,2,1.

Kod (Python)



  1. import random

  2. import numpy


  3. def GenerateLine(fname):

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

  5. line = ""

  6. L = []

  7. for i in range(N):

  8. x = str(random.randint(1,3))

  9. L.append(x)

  10. line = ", ".join(L)

  11. try:

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

  13. try:

  14. f.write(line)

  15. finally:

  16. f.close()

  17. except IOError:

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

  19. print(line)


  20. f_source = "file17_source.txt"

  21. GenerateLine(f_source)


  22. try:

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

  24. lines = f.readlines()

  25. lst = []

  26. lst_result = []

  27. print(lines)

  28. for line in reversed(lines):

  29. for i in line.split(", "):

  30. lst.append(int(i))

  31. print(lst)

  32. k = 1

  33. ser_num = 1

  34. i_prev = lst[0]

  35. for i in lst[1::]:

  36. if i == i_prev:

  37. k += 1

  38. else:

  39. lst_result.append(i)

  40. k = 1

  41. ser_num += 1

  42. i_prev = i

  43. print("Number of series:",ser_num)

  44. print("Length of series:",lst_result)


  45. except IOError:

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



File 18
Haqiqiy raqamlar fayli berilgan.Uning birinchi mahalliy minimumini toping (mahalliy minimum – qo’shnilaridan kichikroq element).

Kod (Python)



  1. import random


  2. def GenerateLine(fname):

  3. N = random.randrange(1,10)

  4. print("N = ",N)

  5. #N = 2

  6. #line = "1, 5, 5, 5, 4, 4, 5"

  7. try:

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

  9. try:

  10. for i in range(N):

  11. line = str(random.randint(1,4))+"\n"

  12. f.write(line)

  13. finally:

  14. f.close()

  15. except IOError:

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


  17. f_input = "file18_input.txt"

  18. #GenerateLine(f_input)


  19. i = 0

  20. flag = False

  21. try:

  22. with open(f_input,'r') as f_in:

  23. for line in f_in:

  24. i += 1

  25. x = int(line.strip())

  26. print(x)

  27. if i == 1:

  28. x1 = x

  29. elif i == 2:

  30. x2 = x

  31. if x1 < x2:

  32. l_min = x1

  33. flag = True

  34. break

  35. elif i == 3:

  36. x3 = x

  37. if x1 >= x2 and x2 < x3:

  38. l_min = x2

  39. flag = True

  40. break

  41. else:

  42. x1 = x2

  43. x2 = x3

  44. x3 = x

  45. if x1 >= x2 and x2 < x3:

  46. l_min = x2

  47. flag = True

  48. break


  49. except IOError:

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


  51. if not flag:

  52. if i == 1:

  53. l_min = x1

  54. else:

  55. l_min = x


  56. print("First local minimum:",l_min)


File 19


Haqiqiy sonlar fayli berilgan.Uning oxirgi lokal maksimalini toping (lokal maksimal qo’shnilaridan kattaroq element).
Код (Python)

  1. import random


  2. def GenerateLine(fname):

  3. N = random.randrange(1,10)

  4. print("N = ",N)

  5. #N = 2

  6. #line = "1, 5, 5, 5, 4, 4, 5"

  7. try:

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

  9. try:

  10. for i in range(N):

  11. line = str(random.randint(1,4))+"\n"

  12. f.write(line)

  13. finally:

  14. f.close()

  15. except IOError:

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


  17. f_input = "file19_input.txt"

  18. GenerateLine(f_input)


  19. i = 0

  20. flag = False

  21. try:

  22. with open(f_input,'r') as f_in:

  23. for line in f_in:

  24. i += 1

  25. x = int(line.strip())

  26. print(x)

  27. if i == 1:

  28. x1 = x

  29. elif i == 2:

  30. x2 = x

  31. elif i == 3:

  32. x3 = x

  33. if x3 >= x2:

  34. l_max_prev = x3

  35. elif x1 <= x2 and x2 > x3:

  36. l_max_prev = x2

  37. else:

  38. l_max_prev = x1

  39. else:

  40. x1 = x2

  41. x2 = x3

  42. x3 = x

  43. if x3 >= x2:

  44. l_max_next = x3

  45. elif x1 <= x2 and x2 > x3:

  46. l_max_next = x2

  47. else:

  48. l_max_next = l_max_prev


  49. except IOError:

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


  51. if i == 1:

  52. l_max = x1

  53. elif i == 2:

  54. if x1 > x2:

  55. l_max = x1

  56. else:

  57. l_max = x2

  58. elif i == 3:

  59. l_max = l_max_prev

  60. else:

  61. l_max = l_max_next


  62. print("Last local maximum:",l_max)



File 20
Haqiqiy sonlar fayli berilgan.Uning mahalliy ekstremallarining umumiy sonini toping,ya’ni mahalliy minimal va mahalliy maksimalini toping (lokal maksimal qo’shnilaridan kattaroq element va mahalliy minimum – qo’shnilaridan kichikroq element).
Kod (Python)

  1. import random


  2. def GenerateLine(fname):

  3. N = random.randrange(1,10)

  4. print("N = ",N)

  5. #N = 2

  6. #line = "1, 5, 5, 5, 4, 4, 5"

  7. try:

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

  9. try:

  10. for i in range(N):

  11. line = str(random.randint(1,4))+"\n"

  12. f.write(line)

  13. finally:

  14. f.close()

  15. except IOError:

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


  17. f_input = "file20_input.txt"

  18. #GenerateLine(f_input)


  19. unique_num = 0

  20. extr_num = 2

  21. try:

  22. with open(f_input,'r') as f_in:

  23. for line in f_in:

  24. x = int(line.strip())

  25. print(x)


  26. if unique_num > 0 and x == x_prev:

  27. #print(" : skipped")

  28. continue


  29. x_prev = x

  30. unique_num += 1

  31. if unique_num == 1:

  32. x1 = x

  33. elif unique_num == 2:

  34. x2 = x

  35. elif unique_num == 3:

  36. x3 = x

  37. #print("hello: {0},{1},{2}".format(x1,x2,x3))

  38. if (x1 < x2 and x2 > x3) or (x1 > x2 and x2 < x3):

  39. extr_num += 1

  40. else:

  41. x1 = x2

  42. x2 = x3

  43. x3 = x

  44. if (x1 < x2 and x2 > x3) or (x1 > x2 and x2 < x3):

  45. extr_num += 1

  46. #print(":",extr_num,":",unique_num)


  47. except IOError:

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


  49. print("unique_num: ",unique_num)


  50. if unique_num == 1:

  51. extr_num = 1

  52. elif unique_num == 2:

  53. extr_num = 2


  54. print("Number of local extremums:",extr_num)



File 21
Haqiqiy sonlar fayli berilgan .O’sish tartibida manba faylning barcha mahalliy maksimallari raqamlarini o’z ichiga olgan butun sonlar faylini yarating (mahalliy maksimal qo’shnilaridan kattaroq element ).
Kod (Python)

  1. import random


  2. def GenerateNumbers(fname):

  3. N = random.randint(1,11)

  4. print("N = ",N)

  5. L = []

  6. x = random.randint(1,4)

  7. L.append(x)

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

  9. lst_rnd = list(range(1,5))

  10. lst_rnd.remove(x)

  11. x = random.choice(lst_rnd)

  12. L.append(x)

  13. print(L)

  14. try:

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

  16. try:

  17. for x in L:

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

  19. f.write(line)

  20. finally:

  21. f.close()

  22. except IOError:

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


  24. def EmptyFile(fname):

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


  26. def Write2File(fname,value):

  27. try:

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

  29. try:

  30. s = str(value)+'\n'

  31. f.write(s)

  32. finally:

  33. f.close()

  34. except IOError:

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


  36. f_input = "file21_input.txt"

  37. #GenerateNumbers(f_input)


  38. f_output = "file21_output.txt"

  39. EmptyFile(f_output)


  40. i = 0

  41. flag = False

  42. try:

  43. with open(f_input,'r') as f_in:

  44. for line in f_in:

  45. i += 1

  46. x = int(line.strip())

  47. print(x)

  48. if i == 1:

  49. x1 = x

  50. l_max = x1

  51. l_max_line = 1

  52. elif i == 2:

  53. x2 = x

  54. if x1 > x2:

  55. print("Local maximum:",l_max,"; Max line:",l_max_line)

  56. Write2File(f_output,l_max_line)

  57. else:

  58. l_max = x2

  59. l_max_line = 2

  60. elif i == 3:

  61. x3 = x

  62. if x3 > x2:

  63. l_max = x3

  64. l_max_line = 3

  65. elif x1 < x2 and x2 > x3:

  66. l_max = x2

  67. print("Local maximum:",l_max,"; Max line:",l_max_line)

  68. Write2File(f_output,l_max_line)

  69. else:

  70. x1 = x2

  71. x2 = x3

  72. x3 = x

  73. if x3 > x2:

  74. l_max = x3

  75. l_max_line = i

  76. elif x1 < x2 and x2 > x3:

  77. print("Local maximum:",l_max,"; Max line:",l_max_line)

  78. Write2File(f_output,l_max_line)


  79. except IOError:

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


  81. if i == 1:

  82. print("Local maximum:",l_max,"; Max line:",l_max_line)

  83. Write2File(f_output,l_max_line)

  84. elif i == 2:

  85. if x1 < x2:

  86. print("Local maximum:",l_max,"; Max line:",l_max_line)

  87. Write2File(f_output,l_max_line)

  88. else:

  89. if x2 < x3:

  90. print("Local maximum:",l_max,"; Max line:",l_max_line)

  91. Write2File(f_output,l_max_line)



File 22
Haqiqiy sonlar fayli berilgan .Manba faylning barcha mahalliy ekstremal raqamlarini kamayish tartibida o’z ichiga olgan butun sonlar faylini yarating (mahalliy ekstremumlar ta’rifi File20 vazifasida berilgan).
Kod (Python)

  1. import random


  2. def GenerateNumbers(fname):

  3. N = random.randint(1,15)

  4. print("N = ",N)

  5. L = []

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

  7. L.append(x)

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

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

  10. lst_rnd.remove(x)

  11. x = random.choice(lst_rnd)

  12. L.append(x)

  13. print(L)

  14. try:

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

  16. try:

  17. for x in L:

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

  19. f.write(line)

  20. finally:

  21. f.close()

  22. except IOError:

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


  24. def EmptyFile(fname):

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


  26. def Write2File(fname,value):

  27. try:

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

  29. try:

  30. s = str(value)+'\n'

  31. f.write(s)

  32. finally:

  33. f.close()

  34. except IOError:

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


  36. f_input = "file22_input.txt"

  37. GenerateNumbers(f_input)


  38. f_output = "file22_output.txt"

  39. EmptyFile(f_output)


  40. i = 0

  41. try:

  42. with open(f_input,'r') as f_in:

  43. for line in f_in:

  44. i += 1

  45. x = int(line.strip())

  46. print(x)

  47. if i == 1:

  48. x1 = x

  49. print("Local extremum:",x1,"; Extremum line:",1)

  50. Write2File(f_output,1)

  51. elif i == 2:

  52. x2 = x

  53. elif i == 3:

  54. x3 = x

  55. if (x1 < x2 and x2 > x3) or (x1 > x2 and x2 < x3):

  56. print("Local extremum:",x2,"; Extremum line:",i-1)

  57. Write2File(f_output,i)

  58. else:

  59. x1 = x2

  60. x2 = x3

  61. x3 = x

  62. #print(x1,x2,x3)

  63. if (x1 < x2 and x2 > x3) or (x1 > x2 and x2 < x3):

  64. print("Local extremum:",x2,"; Extremum line:",i-1)

  65. Write2File(f_output,i)


  66. except IOError:

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


  68. if i == 2:

  69. print("Local extremum:",x2,"; Extremum line:",2)

  70. Write2File(f_output,2)

  71. elif i >= 3:

  72. print("Local extremum:",x3,"; Extremum line:",i)

  73. Write2File(f_output,i)


File 23
Haqiqiy sonlar fayli berilgan .Manba faylidagi elementlarning barcha kamayuvchi ketma-ketliklarning uzunliklarini o’z ichiga olgan butu fayl yarating.Masalan 1.7, 4.5, 3.4, 2.2, 8.5, 1.2 elementlarga ega bo’lgan manba fayl uchun natijaviy faylning mazmuni quyidagicha bo’lishi kerak: 3,2.
Kod (Python)

  1. import random


  2. def GenerateNumbers(fname):

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

  4. print("N = ",N)

  5. L = []

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

  7. L.append(x)

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

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

  10. lst_rnd.remove(x)

  11. x = random.choice(lst_rnd)

  12. L.append(x)

  13. print(L)

  14. try:

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

  16. try:

  17. for x in L:

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

  19. f.write(line)

  20. finally:

  21. f.close()

  22. except IOError:

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


  24. def EmptyFile(fname):

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


  26. def Write2File(fname,value):

  27. try:

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

  29. try:

  30. s = str(value)+'\n'

  31. f.write(s)

  32. finally:

  33. f.close()

  34. except IOError:

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


  36. f_input = "file23_input.txt"

  37. #GenerateNumbers(f_input)


  38. f_output = "file23_output.txt"

  39. EmptyFile(f_output)


  40. i = 0

  41. decrease = 0

  42. try:

  43. with open(f_input,'r') as f_in:

  44. line = f_in.readline()

  45. x = int(line.strip())

  46. i = 1

  47. x1 = x

  48. x2 = x

  49. print(x)

  50. for line in f_in:

  51. i += 1

  52. x = int(line.strip())

  53. print(x)

  54. x1 = x2

  55. x2 = x

  56. if x1 > x2:

  57. decrease += 1

  58. else:

  59. if decrease > 0:

  60. print("Decrease Length:",decrease+1)

  61. Write2File(f_output,decrease+1)

  62. decrease = 0


  63. except IOError:

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


  65. if decrease > 0:

  66. print("Decrease Length:",decrease+1)

  67. Write2File(f_output,decrease+1)


File 24
Haqiqiy raqamlar fayli berilgan.Manba fayl elementlarining barcha monotonik ketma-ketliklarining uzunliklarini o’z ichiga olgan butun sonlar faylini yarating .Masalan 1.7, 4.5, 3.4, 2.2, 8.5, 1.2 elementlariga ega bo’lgan manba fayl uchun natijada olingan faylning mazmuni : 2, 3, 2, 2 bo’lishi kerak .
Kod  (Python)

  1. import random


  2. def GenerateNumbers(fname):

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

  4. print("N = ",N)

  5. L = []

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

  7. L.append(x)

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

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

  10. lst_rnd.remove(x)

  11. x = random.choice(lst_rnd)

  12. L.append(x)

  13. print(L)

  14. try:

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

  16. try:

  17. for x in L:

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

  19. f.write(line)

  20. finally:

  21. f.close()

  22. except IOError:

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


  24. def EmptyFile(fname):

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


  26. def Write2File(fname,value):

  27. try:

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

  29. try:

  30. s = str(value)+'\n'

  31. f.write(s)

  32. finally:

  33. f.close()

  34. except IOError:

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


  36. f_input = "file24_input.txt"

  37. GenerateNumbers(f_input)


  38. f_output = "file24_output.txt"

  39. EmptyFile(f_output)


  40. i = 0

  41. decrease = 0

  42. increase = 0

  43. try:

  44. with open(f_input,'r') as f_in:

  45. line = f_in.readline()

  46. x = int(line.strip())

  47. i = 1

  48. x1 = x

  49. x2 = x

  50. print(x)

  51. for line in f_in:

  52. i += 1

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

  54. print(x)

  55. x1 = x2

  56. x2 = x

  57. if x1 > x2:

  58. decrease += 1

  59. if increase > 0:

  60. print("Increase Length:",increase+1)

  61. Write2File(f_output,increase+1)

  62. increase = 0

  63. else:

  64. increase += 1

  65. if decrease > 0:

  66. print("Decrease Length:",decrease+1)

  67. Write2File(f_output,decrease+1)

  68. decrease = 0


  69. except IOError:

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


  71. if decrease > 0:

  72. print("Decrease Length:",decrease+1)

  73. Write2File(f_output,decrease+1)

  74. if increase > 0:

  75. print("Increase Length:",increase+1)

  76. Write2File(f_output,increase+1)



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