Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Défi : Insertion d'Éléments dans le Tableau | Liste et Tableau
Aperçu des Algorithmes et des Structures de Données

book
Défi : Insertion d'Éléments dans le Tableau

Tâche

Swipe to start coding

Solution

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')

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
single

single

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')

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt