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
80 changes: 80 additions & 0 deletions task_41.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#Проанализировать скорость и сложность одного любого алгоритма, разработанных
# в рамках домашнего задания первых трех уроков.
# P.s. текст задания вставлялся после профайлинга функций. Нумерация строк смещена


#В массиве случайных целых чисел поменять местами минимальный
# и максимальный элементы.

import cProfile
import random


def my_function(size):
SIZE = size
array = [random.randint(0, SIZE * SIZE) for _ in range(SIZE)]
minimum = maximum = array[0]
for i in range(1, len(array)):
if array[i] > maximum:
maximum = array[i]
elif array[i] < minimum:
minimum = array[i]
else:
pass
new_array = []
for i in range(len(array)):
if array[i] == maximum:
new_array += [minimum]
elif array[i] == minimum:
new_array += [maximum]
else:
new_array += [array[i]]
return new_array

#task_41.my_function(10)
#100 loops, best of 3: 92 usec per loop

#task_41.my_function(100)
#100 loops, best of 3: 832 usec per loop

#task_41.my_function(1000)
#100 loops, best of 3: 8.92 msec per loop


def alex_function(size):
SIZE = size
array = [random.randint(-100, 100) for _ in range(SIZE)]
idx_min = 0
idx_max = 0
for i in range(len(array)):
if array[i] < array[idx_min]:
idx_min = i
elif array[i] > array[idx_max]:
idx_max = i
spam = array[idx_min]
array[idx_min] = array[idx_max]
array[idx_max] = spam
return array

#task_41.alex_function(10)
#100 loops, best of 3: 80.2 usec per loop

#task_41.alex_function(100)
#100 loops, best of 3: 713 usec per loop

#task_41.alex_function(1000)
#100 loops, best of 3: 7.13 msec per loop



#cProfile.run('my_function(10)') 1 0.000 0.000 0.001 0.001 task_41.py:7(my_function)
#cProfile.run('my_function(100)') 1 0.000 0.000 0.004 0.004 task_41.py:7(my_function)
#cProfile.run('my_function(1000)') 1 0.002 0.002 0.013 0.013 task_41.py:7(my_function)

#cProfile.run('alex_function(10)') 1 0.000 0.000 0.000 0.000 task_41.py:38(alex_function)
#cProfile.run('alex_function(100)') 1 0.000 0.000 0.001 0.001 task_41.py:38(alex_function)
#cProfile.run('alex_function(1000)') 1 0.001 0.001 0.010 0.010 task_41.py:38(alex_function)

# =============================================================================
# Вывод: alex_function - WIN! Сложность практически линейная.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Отлично

# =============================================================================
104 changes: 104 additions & 0 deletions task_42.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#Написать два алгоритма нахождения i-го по счёту простого числа.
#Без использования Решета Эратосфена;
#Использовать алгоритм решето Эратосфена
# P.s. текст задания вставлялся после профайлинга функций. Нумерация строк смещена



def my_simple_numbers(n): #Максимально бессмысленный и беспощадный вариант
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Отлично

simple_numbers = [2]
i = 2
while len(simple_numbers) < n:
i += 1
k = 0
for j in range(2, i):
if i % j == 0:
k += 1
if k == 0:
simple_numbers += [i]
else:
k = 0
return simple_numbers[len(simple_numbers) - 1]

#"task_42.my_simple_numbers(10)"
#100 loops, best of 3: 130 usec per loop

#my_simple_numbers(20)
#100 loops, best of 3: 596 usec per loop

#task_42.my_simple_numbers(30)
#100 loops, best of 3: 1.38 msec per loop


def clear_simple_numbers(n): #Банально брал верхнюю границу по известным простым числам.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1-е и 2-е простые числа вообще не ищет. А 3-е оказывается 2. Странно, всегда думал, что 5. Да и вариант выше со мной согласен.

sieve = [i for i in range(n)]
sieve[1] = 0

for i in range(2, n):
if sieve[i] != 0:
j = i + i
while j < n:
sieve[j] = 0
j += i

res = [i for i in sieve if i != 0]
return res[len(res) - 1]

#task_42.clear_simple_numbers(30)
#100 loops, best of 3: 35.1 usec per loop

#task_42.clear_simple_numbers(72)
#100 loops, best of 3: 76.4 usec per loop

#"task_42.clear_simple_numbers(114)"
#100 loops, best of 3: 123 usec per loop


def full_eratosthenes(n): # Так и не понял, как определить верхнюю границу
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут ситуация лучше. Ищет простые числа верно, кроме 1-го.

# диапазона натуральных чисел для поиска...
# Так что это сурово-экзотический вариант.
# И это точно Эратосфен, т.к. при необходимости массив
# с дырочками также можно вывести.
simple_numbers = []
k = 1
while n > len(simple_numbers):
sieve = [i for i in range(int(n*k))]
sieve[1] = 0
for i in range(2, int(n*k)):
if sieve[i] != 0:
j = i + i
while j < int(n*k):
sieve[j] = 0
j += i

simple_numbers = [i for i in sieve if i != 0]
k += 1
return simple_numbers[n - 1]

#task_42.full_eratosthenes(10)
#100 loops, best of 3: 122 usec per loop
#"task_42.full_eratosthenes(20)"
#100 loops, best of 3: 413 usec per loop
#"task_42.full_eratosthenes(30)"
#100 loops, best of 3: 672 usec per loop



#cProfile.run('my_simple_numbers(10)') 1 0.000 0.000 0.000 0.000 task_42.py:4(my_simple_numbers)
#cProfile.run('my_simple_numbers(20)') 1 0.001 0.001 0.001 0.001 task_42.py:4(my_simple_numbers)
#cProfile.run('my_simple_numbers(30)') 1 0.003 0.003 0.003 0.003 task_42.py:4(my_simple_numbers)

#cProfile.run('clear_simple_numbers(30)') 1 0.000 0.000 0.000 0.000 task_42.py:29(clear_simple_numbers)
#cProfile.run('clear_simple_numbers(72)') 1 0.000 0.000 0.000 0.000 task_42.py:29(clear_simple_numbers)
#cProfile.run('clear_simple_numbers(114)') 1 0.000 0.000 0.000 0.000 task_42.py:29(clear_simple_numbers)

#cProfile.run('full_eratosthenes(10)') 1 0.000 0.000 0.000 0.000 task_42.py:53(full_eratosthenes)
#cProfile.run('full_eratosthenes(20)') 1 0.000 0.000 0.000 0.000 task_42.py:53(full_eratosthenes)
#cProfile.run('full_eratosthenes(30)') 1 0.000 0.000 0.001 0.001 task_42.py:53(full_eratosthenes)

# =============================================================================
#Вывод:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Вариант 2 нельзя сравнивать с 1 и 3 по асимптотике, т.к. он изначально работает неправильно.
В остальном всё верно

# Не смотря на всю свою экзотичность full_eratosthenes побеждает my_simple_numbers
# Но лучше всего уметь определять верхнюю границу для поиска.
# Зависимость нелинейная, возможно, показательная.
# =============================================================================