Birinchisida
File 25 Haqiqiy raqamlar fayli berilgan.Undagi barcha elementlarni kvadratlari bilan almashtiring . Код (Python)
Download 90.45 Kb.
|
File 1
- Bu sahifa navigatsiya:
- File 26 Haqiqiy raqamlar fayli berilgan.Undagi minimal va maksimal elementlarni almashtiring. Kod (Python)
- File 30 Juft sonli elementlardan iborat butun sonlar fayli berilgan.Berilgan fayldan elementlarning ikkinchi yarmini o’chiring. Kod (Python)
File 25
Haqiqiy raqamlar fayli berilgan.Undagi barcha elementlarni kvadratlari bilan almashtiring . Код (Python) # -*- coding: utf-8 -*- import random import string import os import sys def GenerateReals(fname): N = random.randint(2,15) print("N = ",N) L = [] x = round(random.uniform(0, 10),1) L.append(x) for i in range(1,N): lst_rnd = [round(random.uniform(0, 10),1) for _ in range(0,10)] try: lst_rnd.remove(x) except: pass x = random.choice(lst_rnd) L.append(x) print(L) try: f = open(fname, "w") try: for x in L: line = str(x)+"\n" f.write(line) finally: f.close() except IOError: print('Write error: ',fname) file1 = "file25.txt" GenerateReals(file1) print("Read from:",file1) try: #file for temporary data N = random.randrange(5,8) S = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase \ + string.digits) for _ in range(N)) temp_file = "temp_" + S + ".txt" #print() print("Temp file:",temp_file) with open(file1, 'r') as f_in, open(temp_file, 'w') as f_out: for line in f_in: x = float(line.strip()) y = x * x print("%1.1f : %2.2f" % (x, y)) #print(x,":",y) s = str("%2.2f" % y)+'\n' f_out.write(s) #sys.exit() with open(file1, 'w') as f_out, open(temp_file, 'r') as f_in: for line in f_in: f_out.write(line) try: os.remove(temp_file) except OSError as e: print("\nError:", e) except IOError: print('Open error: ',file1) File 26 Haqiqiy raqamlar fayli berilgan.Undagi minimal va maksimal elementlarni almashtiring. Kod (Python) # -*- coding: utf-8 -*- import random import string import os import sys def GenerateReals(fname): N = random.randint(2,15) print("N = ",N) L = [] x = round(random.uniform(0, 10),1) L.append(x) for i in range(1,N): lst_rnd = [round(random.uniform(0, 10),1) for _ in range(0,10)] try: lst_rnd.remove(x) except: pass x = random.choice(lst_rnd) L.append(x) print(L) try: f = open(fname, "w") try: for x in L: line = str(x)+"\n" f.write(line) finally: f.close() except IOError: print('Write error: ',fname) file1 = "file26.txt" GenerateReals(file1) print("Read from:",file1) try: #file for temporary data N = random.randrange(5,8) S = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase \ + string.digits) for _ in range(N)) temp_file = "temp_" + S + ".txt" #print() print("Temp file:",temp_file) with open(file1, 'r') as f_in, open(temp_file, 'w') as f_out: line = f_in.readline() x = float(line.strip()) mx = x mn = x mx_line = 1 mn_line = 1 line_num = 1 s = str(x)+'\n' f_out.write(s) for line in f_in: line_num += 1 x = float(line.strip()) if mx < x: mx = x mx_line = line_num elif mn > x: mn = x mn_line = line_num s = str(x)+'\n' f_out.write(s) print("Min: %1.1f : %2d" % (mn,mn_line)) print("Max: %1.1f : %2d" % (mx,mx_line)) swap1_value = mx swap1_line = mn_line swap2_value = mn swap2_line = mx_line print("Swap1: %1.1f : %2d" % (swap1_value,swap1_line)) print("Swap2: %1.1f : %2d" % (swap2_value,swap2_line)) #sys.exit() with open(file1, 'w') as f_out, open(temp_file, 'r') as f_in: line_num = 0 for line in f_in: line_num += 1 if line_num == swap1_line: f_out.write(str(swap1_value)+'\n') elif line_num == swap2_line: f_out.write(str(swap2_value)+'\n') else: f_out.write(line) print(line,end="") try: os.remove(temp_file) except OSError as e: print("\nError:", e) except IOError: 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) # -*- coding: utf-8 -*- import random import string import os import sys import math def GenerateInts(fname): #generate random real numbers and save them to file N = random.randint(2,15) N = 9 #N = 10 print("N = ",N) try: f = open(fname, "w") try: for i in range(0,N): #x = random.randint(1,11) x = i + 1 line = str(x)+"\n" f.write(line) finally: f.close() except IOError: print('Write error: ',fname) def CountFile(fname): #count number of rows in given text-file N = 0 try: with open(fname,'r') as f: for line in f: N += 1 #print(N,":",line) except IOError: print("Open error:",fname) return -1 finally: return N def GetLineK(fname,K): line = "" N = 0 try: with open(fname,'r') as f: for line in f: N += 1 if K == N: break except IOError: print("Open error:",fname) return -1 finally: return line file1 = "file27.txt" GenerateInts(file1) print("Read from:",file1) try: #file for temporary data N = random.randrange(5,8) S = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase \ + string.digits) for _ in range(N)) temp_file_1 = "temp27_" + S + "_1.txt" temp_file_2 = "temp27_" + S + "_2.txt" #print() print("Temp file 1:",temp_file_1) print("Temp file 2:",temp_file_2) N = CountFile(file1) N1 = int(math.ceil(N/2)) N2 = N - int(N/2) try: f_in = open(file1, 'r') try: print("===== Part 1 =====") f_out = open(temp_file_1, 'w') for i in range(1,N1+1): line = f_in.readline() print(i,":",line,end="") f_out.write(line) except IOError: print('Read Error: ',temp_file_1) finally: f_out.close() try: print("===== Part 2 =====") f_out = open(temp_file_2, 'w') for i in range(N,N2,-1): #print(i) line = GetLineK(file1,i) print(i,":",line,end="") f_out.write(line) except IOError: print('Read Error: ',temp_file_2) finally: f_out.close() except IOError: print('Read Error: ',file1) finally: f_in.close() try: f_out = open(file1, 'w') try: f_in1 = open(temp_file_1, 'r') f_in2 = open(temp_file_2, 'r') for i in range(0,N2): line = f_in1.readline() f_out.write(line) line = f_in2.readline() f_out.write(line) finally: f_in2.close() f_in1.close() f_out.close() except IOError: print('Write Error: ',file1) try: os.remove(temp_file_1) os.remove(temp_file_2) except OSError as e: print("\nError:", e) except IOError: 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 -*- import random def GenerateReals(fname): #generate random real numbers and save them to file N = random.randint(2,15) #N = 9 print("N = ",N) L = [] x = round(random.uniform(0, 10),1) L.append(x) for i in range(1,N): lst_rnd = [round(random.uniform(0, 10),1) for _ in range(0,10)] try: lst_rnd.remove(x) except: pass x = random.choice(lst_rnd) L.append(x) print(L) try: f = open(fname, "w") try: for x in L: line = str(x)+"\n" f.write(line) finally: f.close() except IOError: print('Write error: ',fname) f_input = "file28_in.txt" f_output = "file28_out.txt" GenerateReals(f_input) print("Read from:",f_input) print("Write to:",f_output) try: with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out: line = f_in.readline() x1 = float(line.strip()) f_out.write(line) line = f_in.readline() x2 = float(line.strip()) for line in f_in: x = float(line.strip()) x_avg = (x1 + x2 + x) / 3 s_avg = str(x_avg)+"\n" f_out.write(s_avg) x1 = x2 x2 = x f_out.write(line) except IOError: 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) # -*- coding: utf-8 -*- import random def GenerateNumbers(fname): N = random.randint(50,65) print("N = ",N) L = [] x = random.randrange(1,10) L.append(x) for i in range(1,N): lst_rnd = list(range(1,10)) lst_rnd.remove(x) x = random.choice(lst_rnd) L.append(x) print(L) try: f = open(fname, "w") try: for x in L: line = str(x)+"\n" f.write(line) finally: f.close() except IOError: print('Write error: ',fname) f_input = "file29_in.txt" f_output = "file29_out.txt" GenerateNumbers(f_input) print("Read from:",f_input) print("Write to:",f_output) try: with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out: for i in range(50): line = f_in.readline() f_out.write(line) except IOError: print('Open error: ',file1) File 30 Juft sonli elementlardan iborat butun sonlar fayli berilgan.Berilgan fayldan elementlarning ikkinchi yarmini o’chiring. Kod (Python) # -*- coding: utf-8 -*- import random def GenerateNumbers(fname): N = random.randint(1,10) * 2 print("N = ",N) L = [] x = random.randrange(1,10) L.append(x) for i in range(1,N): lst_rnd = list(range(1,10)) lst_rnd.remove(x) x = random.choice(lst_rnd) L.append(x) print(L) try: f = open(fname, "w") try: for x in L: line = str(x)+"\n" f.write(line) finally: f.close() except IOError: print('Write error: ',fname) def CountFile(fname): #count number of rows in given text-file N = 0 try: with open(fname,'r') as f: for line in f: N += 1 #print(N,":",line) except IOError: print("Open error:",fname) return -1 finally: return N f_input = "file30_in.txt" f_output = "file30_out.txt" GenerateNumbers(f_input) print("Read from:",f_input) print("Write to:",f_output) N = CountFile(f_input) N = int(round(N/2.0)) try: with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out: for i in range(N): line = f_in.readline() f_out.write(line) except IOError: print('Open error: ',file1) 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