Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 1. В диапазоне натуральных чисел от 2 до 99 определить, сколько из них кратны любому из чисел в диапазоне от 2 до 9.

RANGE_START = 2
RANGE_STOP = 99

nums_data = dict()
for i in range(2, 10):
nums_data[i] = 0

# print(nums_data)

for num in range(RANGE_START, RANGE_STOP + 1):
for i in nums_data.keys():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отлично.
Для краткости можно удалить .keys(). Без него всё равно будет проверка по ключам.

if num % i == 0:
nums_data[i] += 1

for i in nums_data.keys():
print(f' {i} - кратно {nums_data[i]} чисел')
11 changes: 11 additions & 0 deletions task_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Во втором массиве сохранить индексы четных элементов первого массива. Например, если дан массив со значениями 8, 3, 15, 6, 4, 2, то во второй массив надо заполнить значениями 1, 4, 5, 6 (или 0, 3, 4, 5 - если индексация начинается с нуля), т.к. именно в этих позициях первого массива стоят четные числа.

array_1=[8, 3, 15, 6, 4, 2]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отлично

array_2=[]

for i, item in enumerate(array_1):
if item % 2 == 0:
array_2.append(i)

print(array_1)
print(array_2)
32 changes: 32 additions & 0 deletions task_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 3. В массиве случайных целых чисел поменять местами минимальный и максимальный элементы.

import random

SIZE = 10
MIN_ITEM = 0
MAX_ITEM = 100

array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)]

print(array, end="\n\n")

max = dict(index=0, value=array[0])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Похоже я вас перехвалил в прошлый раз )))
Итак, торжественная обстановка. Я вручаю вам звезду. За утопленный самолёт конечно же.
Пересмотрите разбор ДЗ. Ту часть, где я говорил о плохих именах для переменных )))

min = dict(index=0, value=array[0])

for i, item in enumerate(array):

if max["value"] <= item:
max["value"] = item
max["index"] = i

if min["value"] >= item:
min["value"] = item
min["index"] = i

array[max["index"]] = min["value"]
array[min["index"]] = max["value"]

print(f'Максимальный элемент {max["value"]} с индексом {max["index"]}')
print(f'Минимальный элемент {min["value"]} с индексом {min["index"]}', end="\n\n")

print(array)
28 changes: 28 additions & 0 deletions task_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 4. Определить, какое число в массиве встречается чаще всего.

import random

SIZE = 20
MIN_ITEM = 0
MAX_ITEM = 100

array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)]

print(array, end="\n\n")

nums = dict()

for item in array:
if item in nums.keys():
nums[item] += 1
else:
nums[item] = 1

max = dict(num=0, count=0)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отлично... Наверное... Могло бы быть... )))


for num in nums.keys():
if(nums[num]>=max["count"]):
max["count"]=nums[num]
max["num"] = num

print(f'Число {max["num"]} встречается чаще всего - {max["count"]} раз')
25 changes: 25 additions & 0 deletions task_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 5. В массиве найти максимальный отрицательный элемент. Вывести на экран его значение и позицию в массиве.

import random
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отличный алгоритм


SIZE = 10
MIN_ITEM = -100
MAX_ITEM = 100

array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)]

print(array, end="\n\n")

max = dict(index=None, value=None)

for i, item in enumerate(array):
if item < 0:
if max["value"] is None or max["value"] < item:
max["value"] = item
max["index"] = i

if max["value"] is None:
print(f'В массиве нет отрицательных чисел')
else:
print(f'Максимальное отрицательное число {max["value"]} с индексом {max["index"]}')

36 changes: 36 additions & 0 deletions task_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 7. В одномерном массиве целых чисел определить два наименьших элемента. Они могут быть как равны между собой (оба являться минимальными), так и различаться.

import random
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отличное решение


SIZE = 20
MIN_ITEM = 0
MAX_ITEM = 100

array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)]

print(array, end="\n\n")

min1 = dict(index=0, value=array[0])
min2 = dict(index=1, value=array[1])

if array[0] < array[1]:
min1["value"] = array[1]
min1["index"] = 1
min2["value"] = array[0]
min2["index"] = 0

for i, item in enumerate(array):
if i > 1 and item <= min1["value"]:
if item <= min2["value"]:
min1["value"] = min2["value"]
min1["index"] = min2["index"]

min2["value"] = item
min2["index"] = i
else:
min1["value"] = item
min1["index"] = i

print(f'Минимальное число {min2["value"]} с индексом {min2["index"]}')
print(f'Минимальное число {min1["value"]} с индексом {min1["index"]}')