BaşlayınÜcretsiz Başlayın

Finding an element

Using the right data structure can significantly increase the performance of your code. For example, if you want to find a certain element in the data, you might want to choose between list and set. In this exercise, you will implement performance tests with pytest to compare the speed of the in operator applied correspondingly to the two data structures: list and set. The pytest package has already been imported.

Bu egzersiz

Introduction to Testing in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Pass benchmark as an argument into the test functions.
  • Then call benchmark() in the test functions passing find() as the first argument.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

def create_list():
    return [i for i in range(1000)]
def create_set():
    return set([i for i in range(1000)])
def find(it, el=50):
    return el in it

# Write the performance test for a list
def test_list(____):
    ____(____, ____)

# Write the performance test for a set
def test_set(____):
    ____(____, ____)
Kodu Düzenle ve Çalıştır