-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Esame #1 - 2023/2024 #1
Comments
def func1(int_list, n):
to_return = {}
for elem in int_list:
if int_list.count(elem) >= n:
to_return[elem] = int_list.count(elem)
return to_return
def func2(dict1, a, b):
to_return = []
for elem in dict1:
if a.lower() <= elem[0].lower() <= b.lower():
to_return.append(dict1[elem])
return sorted(to_return, key=lambda x: (-len(x), x))
def func3(str1, str2):
to_return = ''
str2 = str2[::-1]
for i in range(min(len(str1), len(str2))):
to_return += str1[i].swapcase() + str2[i]
return to_return
def func4(input_filename, output_filename, expr):
lines = []
to_return = {}
with open(input_filename, 'r', encoding='utf-8') as file:
for i, line in enumerate(file):
if expr.lower() in line.lower():
lines.append((i + 1, line))
to_print = ''
for elem in lines:
to_print += elem[1]
to_return[elem[0]] = (
len(elem[1]), len([w for w in elem[1].replace('\t', ' ').replace('\n', ' ').split(' ') if w != '']),
sum([elem[1].count(' '), elem[1].count('\t'), elem[1].count('\n')]))
with open(output_filename, 'w', encoding='utf-8') as file:
file.write(to_print)
return to_return
def func5(input_file_name, output_file_name):
img = images.load(input_file_name)
isolated = 0
img2 = [[(0, 0, 0) for _ in range(len(img[0]))] for _ in range(len(img))]
for l in range(len(img)):
for p in range(len(img[0])):
if img[l][p] != (0, 0, 0):
if p == 0 and img[l][p + 1] == (0, 0, 0):
isolated += 1
img2[l][p] = img[l][p]
elif p == len(img[0]) - 1 and img[l][p - 1] == (0, 0, 0):
isolated += 1
img2[l][p] = img[l][p]
elif img[l][p - 1] == (0, 0, 0) and img[l][p + 1] == (0, 0, 0):
isolated += 1
img2[l][p] = img[l][p]
images.save(img2, output_file_name)
return isolated
def ex1(dirin, to_return=None, i=0):
if to_return is None:
to_return = []
for elem in os.listdir(dirin):
percorso = f'{dirin}/{elem}'
if percorso.endswith('.txt'):
numerici = 0
with open(percorso, 'r', encoding='utf-8') as file:
testo = file.read()
for char in testo:
if char.isdigit():
numerici += 1
if numerici:
to_return.append((percorso, numerici, i))
elif os.path.isdir(percorso):
ex1(percorso, to_return, i + 1)
if i == 0:
to_return = sorted(to_return, key=lambda x: (-x[1], x[2], x[0]))
to_return = [elem[0] for elem in to_return]
return to_return
def ex2(root, to_return=None, i=0):
if to_return is None:
to_return = []
somma = 0
if root.right:
somma += ex2(root.right, to_return, i + 1)
if root.left:
somma += ex2(root.left, to_return, i + 1)
if root.left or root.right:
if root.value > somma:
to_return.append(0)
somma += root.value
if i == 0:
return len(to_return)
return somma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: