@Reyfli

Возможно ли решить данные задачи другим способом (более простым или более правильным) в Jupyter Notebook?

Оригинальный текст задач:
Divide the given list into three equal parts, ensuring that your code accommodates lists of varying sizes. (sample_list = [11, 32, 8, 23, 14, 29, 78, 45, 89])

Find the intersection (common) of two sets and remove those elements from the first set. You can use the intersection() and remove() method of a set. (first_set = {23, 42, 65, 57, 78, 83, 29}, second_set = {57, 83, 29, 67, 73, 43, 48})

# Задание 9

# Разделите данный список на три равные части, убедившись, что в вашем коде
# есть списки разного размера.

# Define the sample list
sample_list = [11, 32, 8, 23, 14, 29, 78, 45, 89]
# Calculate the length of the list and the size of each chunk
list_len = len(sample_list)
chunk_size = -(-list_len // 3)  # Round up division to ensure all elements are distributed
# Divide the list into three equal parts
divided_list = [sample_list[i:i+chunk_size] for i in range(0, list_len, chunk_size)]
# Print the divided list
for i, part in enumerate(divided_list, start=1):
    print(f"Part {i}: {part}") # Разораться просто.

Мой ответ:
Part 1: [11, 32, 8]
Part 2: [23, 14, 29]
Part 3: [78, 45, 89]

# Задание 10

# Найдите пересечение (общее) двух наборов и удалите эти элементы из первого набора.
# Вы можете использовать методы пересечения() и удаления() набора.

# Define the first set and the second set
first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}
# Find the intersection of the two sets
common_elements = first_set.intersection(second_set)
# Remove the common elements from the first set
for element in common_elements:
    first_set.remove(element)
# Print the updated first set after removing common elements
print("First set after removal:", first_set) # Также разобраться в принципе

Мой ответ: First set after removal: {65, 23, 42, 78}
  • Вопрос задан
  • 70 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы