MINISTRY OF DEVELOPMENT OF INFORMATION TECHNOLOGIES AND COMMUNICATIONS OF THE REPUBLIC OF UZBEKISTAN
TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES NAMED AFTER MUHAMMAD AL-KHWARIZMI
DATA STRUCTURE AND ALGORITHMS
LABORATORY WORK №2
Done by the student of 716-20 group Jo’raqulov Iftixor
Checked by Zarina Ildarovna
LABORATORY WORK №2
Theme: Implementation of functions, parameters and data transfer
Aim: Developing skills in programming semi-static data structures.
Tas1
open_p = ["[","{","("]
close_p = ["]","}",")"]
def solve_task1(s):
stack = []
for i in s:
if i in open_p:
stack.append(i)
elif i in close_p:
pos = close_p.index(i)
if ((len(stack) > 0) and
(open_p[pos] == stack[len(stack)-1])):
stack.pop()
else:
return "Not matched"
if len(stack) == 0:
return "Matched"
else:
return "Not matched"
string = "{()[()]}"
print(string,"->", solve_task1(string))
string = "[{}())[]"
print(string,"->", solve_task1(string))
string = "[][(])"
print(string,"->",solve_task1(string))
Tas 2
14. Write a procedure for deleting elements which are prime number.
import math
def is_prime(n):
count = 0
for i in range(1, n):
if n % i == 0:
count += 1
if count < 2:
return True
return False
def solve_task_2(array):
for elem in array:
if is_prime(elem):
del array[array.index(elem)]
return array
array = [2, 25, 66, 9, 7, 15, 13]
print(solve_task_2(array))
Task 1
Dostları ilə paylaş: |