Advertisement
fkudinov

9. Non-unique Elements / Вирішуємо задачі на Python CheckIO Українською

Aug 21st, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | Source Code | 0 0
  1. # --------------------    tasks/non_unique.py ---------------
  2.  
  3. def checkio(data: list) -> list:
  4.  
  5.     res = []
  6.  
  7.     for i in data:
  8.         if data.count(i) > 1:
  9.             res.append(i)
  10.  
  11.     return res
  12.  
  13.  
  14. def checkio_gen(data: list) -> list:
  15.     return list(i for i in data if data.count(i) > 1)
  16.  
  17.  
  18. def checkio_list_comp(data: list) -> list:
  19.     return [i for i in data if data.count(i) > 1]
  20.  
  21.  
  22. def checkio_filter(data: list) -> list:
  23.     not_unique = lambda item: data.count(item) > 1
  24.     return list(filter(not_unique, data))
  25.  
  26.  
  27.  
  28. # --------------   tests/test_non_unique.py   ------------------------
  29.  
  30.  
  31. import pytest
  32.  
  33. from tasks.non_unique import checkio, checkio_gen, checkio_list_comp, checkio_filter
  34.  
  35.  
  36. @pytest.mark.parametrize("func", [
  37.     checkio, checkio_gen, checkio_list_comp, checkio_filter
  38. ])
  39. @pytest.mark.parametrize(["collection", "res"], [
  40.     [[1, 2, 3, 1, 3], [1, 3, 1, 3]],
  41.     [[1, 2, 3, 4, 5], []],
  42.     [[5, 5, 5, 5, 5], [5, 5, 5, 5, 5]],
  43.     [[10, 9, 10, 10, 9, 8], [10, 9, 10, 10, 9]],
  44. ])
  45. def test_non_unique(func, collection, res):
  46.     assert func(collection) == res
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement