17 занятие
589.14K
Category: programmingprogramming

Кода. Основы программирования на языке Python

1. 17 занятие

Школа::Кода
«Основы программирования на языке Python»

2.

Проверка присутствия
2

3.

3

4.

file = open(‘input.txt’,’r’)
N = int(file.readline())
K = int(file.readline())
I = int(file.readline())
file.close()
with open('output.txt', 'w') as file:
file.write(str(N - 2*K + I))
4

5.

5

6.

N=list(input())
N.sort(reverse=True)
print(''.join(N))
‘2345’
[‘2’,’3’,’4’,’5’]
[‘5’,’4’,’3’,’2’]
5432
6

7.

7

8.

40 20 31 33 19
44 23 22 3 -81
8

9.

with open('input.txt') as f:
f.readline()
x = list(map(int, f.readline().split()))
with open('output.txt', 'w') as f:
f.write(' '.join(map(str, sorted(x, key=lambda x: 10*(x//10-x%10)+x//10))))
9

10.

10

11.

f = open('input.txt')
for line in f:
n=int(line)
f.close()
a=[]
for i in range(n):
a.append([])
for j in range(n):
a[i].append(0)
a[0][0]=1
maswir = n – 1 3
maswirrrr = 0
while(maswir>=maswirrrr):
i = maswirrrr
j = maswirrrr
while j <= maswir:
if j!=0:
a[i][j] = a[i][j-1] + 1
j+=1
i = maswirrrr
j = maswir
while i <= maswir:
if (i != maswirrrr):
a[i][j] = a[i-1][j] + 1
i+=1
i = maswir
j = maswir
while j >= maswirrrr:
if (j != maswir):
a[i][j] = a[i][j+1] + 1
j-=1
i = maswir
j = maswirrrr
while i > maswirrrr:
if (i != maswir):
a[i][j] = a[i+1][j] + 1
i-=1
maswir -= 1;
maswirrrr += 1;
f = open('output.txt','w')
for i in range(n):
f.writelines (' '.join(map(str,a[i])))
f.write('\n')
f.close()
11

12.

12

13.

def fib(n):
if n < 3:
return 1
return fib(n-1) + fib(n-2)
print(fib(int(input())))
5
3+
2+
1
1+1=2
2=5
2
13

14.

14

15.

string = input()
while 'fu' in string:
string = string.replace('fu', '')
print(string)
15

16.

16

17.

f = open('input.txt')
i=1
for line in f:
if i==1:
N_1, N_2 = map(int, line.split())
elif i==2:
f = open('output.txt','w')
f.writelines (' '.join(a1))
f.write('\n')
f.writelines (' '.join(a2))
f.write('\n')
f.close()
a1=line.split()
else:
a2=line.split()
i+=1
f.close()
mas=[]
for i in range(N_1):
mas.append(a1[i])
for i in range(N_2):
mas.append(a2[i])
mas.sort(key=len)
for i in range(N_1):
a1[i]=mas[i]
for i in range(N_2):
a2[i]=mas[i+N_1]
17

18.

18

19.

{name:’helen’, year:2020}
19

20.

f = open('input.txt')
i=0
items=[]
for line in f:
if i==0:
N = int(line)
elif i<=N:
a=line.split()
for i in range(len(mas)-1):
for j in range(len(mas)-i-1):
if mas[j]['year'] > mas[j+1]['year']:
mas[j], mas[j+1] = mas[j+1], mas[j]
elif mas[j]['year'] == mas[j+1]['year']:
if mas[j]['surname'] > mas[j+1]['surname']:
mas[j], mas[j+1] = mas[j+1], mas[j]
elif mas[j]['surname'] == mas[j+1]['surname']:
d= {'surname': a[0], 'name': a[1], 'year': int(a[2])}
if mas[j]['name'] > mas[j+1]['name']:
items.append(d)
mas[j], mas[j+1] = mas[j+1], mas[j]
else:
f = open('output.txt','w')
leftY, rightY=map(int, line.split())
f.writelines (str(len(mas)))
i+=1
f.write('\n')
f.close()
for i in mas:
mas=[]
f.writelines (i['surname']+' '+i['name']+' '+str(i['year']))
for i in items:
f.write('\n')
if leftY<=i['year']<=rightY:
f.close()
mas.append(i)
3425
243 5
234 5
20
English     Русский Rules