Решения на первый блок

This commit is contained in:
justuser-31 2025-03-25 12:39:42 +03:00
parent 1e61635a97
commit 7f72988d8b
12 changed files with 280 additions and 0 deletions

View File

@ -0,0 +1 @@
print("Hello world")

View File

@ -0,0 +1,21 @@
x = int(input('Enter x: '))
y = int(input('Enter y: '))
sum = x + y
sub = x - y
mult = x * y
div = x / y
pow = x**y
sqrt = x ** (1/y)
print(f'''
Results:
-----------------------
sum: \t {sum}
sub: \t {sub}
mult: \t {mult}
div: \t {div}
pow: \t {pow}
sqrt: \t {sqrt}
-----------------------
''')

View File

@ -0,0 +1,12 @@
day = int(input('Enter day of the month: '))
month = input('Enter name of the month: ')
year = int(input('Enter the year: '))
yyyy_mm_dd = f'{year}-{month}-{day}'
dd_mm_yyyy = f'{day}-{month}-{year}'
print(f'''
Result:
yyyy_mm_dd: \t {yyyy_mm_dd}
dd_mm_yyyy: \t {dd_mm_yyyy}
''')

View File

@ -0,0 +1,28 @@
matrix = []
for i in range(10):
matrix.append([0,0,0,0,0,0,0,0,0,0])
#matrix.append([])
# Fill from left-top to right-top
for i in range(10):
matrix[0][i] = i
# Fill from left-top to left-bottom
for i in range(10):
matrix[i][0] = i
# Fill crosshairs
for x in range(1, 10):
for y in range(1, 10):
# Values from left side and top side
l_val = matrix[x][0]
t_val = matrix[0][y]
# Set value on zero-place
matrix[x][y] = l_val * t_val
for line in matrix:
for el in line:
if el >= 10:
print(f'{el} ', end='')
else:
print(f'{el} ', end='')
print()

View File

@ -0,0 +1,27 @@
from random import randint
nums = []
for i in range(10):
nums.append(randint(1, 100))
def rmin(nums, l_len = len(nums), i = None):
# If we at the end
if i == l_len - 1:
return nums[i]
# If main fun (we just started)
if i == None:
i = 0
# Compare next i and current i
n_i = rmin(nums, l_len, i+1)
if n_i < nums[i]:
return n_i
else:
return nums[i]
min = rmin(nums)
print(f'''Original list:
{nums}
Min: {min}
''')

View File

@ -0,0 +1,2 @@
1 2
2 3

View File

@ -0,0 +1,38 @@
a = int(input('Enter a: '))
b = int(input('Enter b: '))
matrix = [
[a, a+1],
[b, b+1]
]
first = True
for line in matrix:
#for el in line:
if first == True:
f_str = f'{line[0]} {line[1]}\n'
first = False
else:
s_str = f'{line[0]} {line[1]}\n'
# Write lines
with open('file', 'w') as f:
f.write(f_str)
f.write(s_str)
f.close()
readed_matrix = []
with open('file', 'r') as f:
for x in f:
if x == '' or x == '\n':
continue
x = x.replace('\n', '')
vals = x.split(' ')
readed_matrix.append(vals)
f.close()
print(f'''
Original matrix:
{matrix}
Readed matrix:
{matrix}
''')

View File

@ -0,0 +1,29 @@
[
{
"name": "Иван Иванов",
"age": 21,
"grades": {
"math": 85,
"physics": 90,
"history": 78
}
},
{
"name": "Мария Петрова",
"age": 22,
"grades": {
"math": 92,
"physics": 88,
"history": 80
}
},
{
"name": "Алексей Смирнов",
"age": 23,
"grades": {
"math": 75,
"physics": 84,
"history": 79
}
}
]

View File

@ -0,0 +1,31 @@
from json import loads
with open('data.json', 'r') as f:
raw_data = f.read()
f.close()
data = loads(raw_data)
# Data for further analyze
stud_i = []
stud_score = []
stud_80_plus = []
for i in range(len(data)):
stud_i.append(i)
average_score = (data[i]['grades']['math'] + data[i]['grades']['physics'] + data[i]['grades']['history'])/3
stud_score.append(average_score)
if data[i]['grades']['math'] > 80:
stud_80_plus.append(i)
average_best = max(stud_score)
aver_i = stud_score.index(average_best)
aver = data[aver_i]
print(f'''Student with best average score:
{aver['name']} {aver['age']} {aver['grades']['math']}-{aver['grades']['physics']}-{aver['grades']['history']}
''')
print('Students with 80+ math scores:')
for i in stud_80_plus:
stud = data[i]
print(f'''{stud['name']} {stud['age']} {stud['grades']['math']}-{stud['grades']['physics']}-{stud['grades']['history']}''')

View File

@ -0,0 +1,44 @@
from threading import Thread
from queue import Queue
from time import sleep
sum = 0
access = 0 # Var for control who is allowed access for sum, start with 0 Thread
threads = 3
th_pool = []
is_live = [0] * threads
def worker(i):
global sum, access, threads, th_pool, is_live
th_pool.append(i)
is_live[i] = 1
# Wait until all threads is registred
while len(th_pool) != threads:
sleep(0.1)
for _ in range(3):
#sleep(threads)
while access != i:
sleep(0.1)
sum += 1
print(f'T{i} | {sum}')
sleep(0.1)
# Transfer access
if len(th_pool) > 1:
ind = th_pool.index(i)
next = ind + 1
if next > threads - 1:
next = 0
access = next
else:
access = i
is_live[i] = 0
for i in range(threads):
th = Thread(target = worker, args = (i,))
th.start()
# Wait untill all ended
while is_live != [0] * threads:
sleep(0.1)
print(f'\nSUM: {sum}')

View File

@ -0,0 +1,19 @@
import socket
# Create a socket object
s = socket.socket()
port = 12345
s.connect(('127.0.0.1', port))
data = '''{
"name": "Иван Иванов",
"age": 25,
"city": "Москва"
}'''
# Send data
s.send(data.encode())
# Receive data
print(f'STATUS: {s.recv(1024).decode()}')
s.close()

View File

@ -0,0 +1,28 @@
import socket
from json import loads
# Create a socket object
s = socket.socket()
port = 12345
s.bind(('127.0.0.1', port))
print ("socket binded to %s" %(port))
# Put the socket into listening mode
s.listen(5)
while True:
# Establish connection with client.
c, addr = s.accept()
print ('Got connection from', addr )
raw_data = c.recv(1024).decode()
data = loads(raw_data)
print("\nParsed data:")
for i in data:
print(f'{i} - {data[i]}')
print()
c.send('OK'.encode())
# Close the connection with the client
c.close()