Challenge: Inserting Elements into the Array
Завдання
Swipe to start coding
Рішення
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import time
import matplotlib.pyplot as plt
# Define a list of N values
N_values = [1000, 2000, 3000, 4000, 5000]
# Create an empty list to store the time for each N value
times = []
for N in N_values:
np_array = np.array([])
# Measure the start time
array_start = time.process_time()
# Insert random numbers into the array
for _ in range(N):
random_number = np.random.randint(0, 10000)
np_array = np.insert(np_array, 0, random_number)
# Measure the end time
array_end = time.process_time()
# Calculate the time taken
array_time = array_end - array_start
# Append the time to the times list
times.append(array_time)
# Plot the dependency of time on N values
plt.figure(figsize=(8, 6))
plt.plot(N_values, times)
plt.xlabel('N values')
plt.ylabel('Time (seconds)')
plt.title('Dependency of Time on N values')
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 2. Розділ 3
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import time
import matplotlib.pyplot as plt
# Define a list of N values
N_values = [1000, 2000, 3000, 4000, 5000]
# Create an empty list to store the time for each N value
times = []
for N in N_values:
np_array = np.array([])
# Measure the start time
array_start = time.process_time()
# Insert random numbers into the array
for _ in range(N):
random_number = np.random.randint(0, 10000)
np_array = np.___(np_array, 0, random_number)
# Measure the end time
array_end = time.process_time()
# Calculate the time taken
array_time = ___ - ___
# Append the time to the times list
times.append(array_time)
# Plot the dependency of time on N values
plt.figure(figsize=(8, 6))
plt.plot(N_values, times)
plt.xlabel('N values')
plt.ylabel('Time (seconds)')
plt.title('Dependency of Time on N values')
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат