File 1
S qator berilgan.Agar S yaroqli fayl nomi bo’lsa , u holda shu nom bilan bo’sh fayl yarating va TRUE deb chop eting.Agar S nomli fayl yaratilmasa ,FALSE deb chop eting.
Kod (Python)
file_name = input('Fayl nomini kiriting: ')
try:
f = open(file_name, "w")
try:
f.write('')
print('TRUE')
finally:
f.close()
except IOError:
print('FALSE')
File 2
Fayl nomi va N(>1) butun soni berilgan.Berilgan nomli butun sonlardan iborat fayl yarating va unga birinchi N musbat juft sonni (2,4,6 …) yozing.
Kod (C/C++)
#include
using namespace std;
int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
int n;
fin >> n;
for(int i = 2; i <= n * 2; i+=2){
fout << i << " ";
}
return 0;
}
Kod (Python)
import random
file_name = "file02.txt"
N = random.randrange(1,10)
try:
f = open(file_name, "w")
f.write("N = " + (str)(N) + '\n')
try:
for i in range(N):
f.write((str)((i+1)*2) + '\n')
finally:
f.close()
except IOError:
print('Fayl yaratilmadi')
File 3
Fayl nomi va haqiqiy sonlar A va D berilgan.Berilgan nomli haqiqiy sonlar faylini tuzing va unga arifmetik progressiyaning dastlabki 10 a’zosini A boshlang’ich a’zosi va D farqi bilan yozing:A,A+D, A + 2·D, A + 3·D, ...
Kod (Python)
import random
N = random.randrange(1,10)
A = random.randrange(-10,10)
D = round(random.uniform(-5,5),1)
print("N = ", N)
print("A = ", A)
print("D = ", D)
a = [A + i*D for i in range(N)]
print(a)
file_name = "file03.txt"
try:
f = open(file_name, "w")
f.write("N = " + (str)(N) + '; ')
f.write("A = " + (str)(A) + '; ')
f.write("D = " + (str)(D) + '\n')
f.write(str(a))
except IOError:
print('Fayl ishlamadi')
finally:
f.close()
File 4
To’rtta fayl nomi berilgan.Joriy katalogdagi berilgan nomli fayllar sonini toping
Kod (Python)
from os import listdir
from os.path import isfile, join
my_files = ['file01.py', 'file02.py', 'file022.txt', 'file03.py', 'file03.txt', 'file44.py']
print("Искомые файлы:")
print(my_files)
mypath = "."
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
print("Имеющиеся файлы:")
print(onlyfiles)
set1 = set(my_files).intersection(onlyfiles)
print("Найденные файлы:")
print(set1)
print("Количество найденных файлов:", len(set1))
File 5
Butun sonlar faylining nomi berilgan.Berilgan fayldagi elementlar sonini toping .Agar bir xil nomdagi fayl bo’lmasa -1 ni chop eting.
Kod (Python)
N = 0
try:
with open('integers.txt','r') as f:
for line in f:
N += 1
print(N,"=>",int(line))
except IOError:
print("Fayl topilmadi ")
print("Elementlar miqdori : ", N)
File 6
K butun soni va manfiy bo’lmagan butun sonlarni o’z ichiga olgan fayl berilgan.Faylning K-elementini chop eting (elementlar 1 dan raqamlangan ).Agar bunday element bo’lmasa , -1 ni chop eting.
Kod (Python)
import random
K = random.randrange(1,11)
print("K:",K)
N = 0
try:
with open('integers.txt','r') as f:
for line in f:
N += 1
print(N,"=>",int(line))
if N == K:
break
except IOError:
print("Файл не найден")
if N == K:
print("Result:",K,"=>",int(line))
else:
print(-1)
Do'stlaringiz bilan baham: |