How to use test_list method in mailosaur-python

Best Python code snippet using mailosaur-python_python

test_dictlist.py

Source:test_dictlist.py Github

copy

Full Screen

1"""Test functions of dictlist.py."""2import re3from copy import copy, deepcopy4from pickle import HIGHEST_PROTOCOL, dumps, loads5from typing import Tuple6import pytest7from cobra.core import DictList, Object8@pytest.fixture(scope="function")9def dict_list() -> Tuple[Object, DictList]:10 obj = Object("test1")11 test_list = DictList()12 test_list.append(obj)13 return obj, test_list14def test_contains(dict_list: Tuple[Object, DictList]) -> None:15 obj, test_list = dict_list16 assert obj in test_list17 assert obj.id in test_list18 assert Object("not_in") not in test_list19 assert "not_in" not in test_list20def test_index(dict_list: Tuple[Object, DictList]) -> None:21 obj, test_list = dict_list22 assert test_list.index("test1") == 023 assert test_list.index(obj) == 024 with pytest.raises(ValueError):25 test_list.index("f")26 with pytest.raises(ValueError):27 test_list.index(Object("f"))28 # Ensure indexing with an object that is a different object29 # also raises an error30 with pytest.raises(ValueError):31 test_list.index(Object("test1"))32def test_independent() -> None:33 a = DictList([Object("o1"), Object("o2")])34 b = DictList()35 assert "o1" in a36 assert "o1" not in b37 b.append(Object("o3"))38 assert "o3" not in a39 assert "o3" in b40def test_get_by_any(dict_list: Tuple[Object, DictList]) -> None:41 obj, test_list = dict_list42 assert test_list.get_by_any(0) == [obj]43 assert test_list.get_by_any("test1") == [obj]44 with pytest.raises(KeyError):45 test_list.get_by_any("not-in-list")46 with pytest.raises(TypeError):47 test_list.get_by_any(1.1)48 assert test_list.get_by_any(obj) == [obj]49def test_append(dict_list: Tuple[Object, DictList]) -> None:50 obj, test_list = dict_list51 obj2 = Object("test2")52 test_list.append(obj2)53 with pytest.raises(ValueError):54 test_list.append(Object("test1"))55 assert test_list.index(obj2) == 156 assert test_list[1] == obj257 assert test_list.get_by_id("test2") is obj258 assert len(test_list) == 259def test_insert(dict_list: Tuple[Object, DictList]) -> None:60 obj, test_list = dict_list61 obj2 = Object("a")62 test_list.insert(0, obj2)63 assert test_list.index(obj2) == 064 assert test_list.index("test1") == 165 assert test_list.get_by_id("a") is obj266 assert len(test_list) == 267 with pytest.raises(ValueError):68 test_list.append(obj2)69def test_extend(dict_list: Tuple[Object, DictList]) -> None:70 obj, test_list = dict_list71 obj_list = [Object(f"test{i:d}") for i in range(2, 10)]72 test_list.extend(obj_list)73 assert test_list[1].id == "test2"74 assert test_list.get_by_id("test2") == obj_list[0]75 assert test_list[8].id == "test9"76 assert len(test_list) == 977 with pytest.raises(ValueError):78 test_list.extend([Object("test1")])79 # Even if the object is unique, if it is present twice in the new80 # list, it should still raise an exception81 with pytest.raises(ValueError):82 test_list.extend([Object("testd"), Object("testd")])83def test_iadd(dict_list: Tuple[Object, DictList]) -> None:84 obj, test_list = dict_list85 obj_list = [Object(f"test{i:d}") for i in range(2, 10)]86 test_list += obj_list87 assert test_list[1].id == "test2"88 assert test_list.get_by_id("test2") == obj_list[0]89 assert test_list[8].id == "test9"90 assert len(test_list) == 991def test_add(dict_list: Tuple[Object, DictList]) -> None:92 obj, test_list = dict_list93 obj_list = [Object(f"test{i:d}") for i in range(2, 10)]94 sum_ = test_list + obj_list95 assert sum_ is not test_list96 assert sum_ is not obj_list97 assert test_list[0].id == "test1"98 assert sum_[1].id == "test2"99 # noinspection PyUnresolvedReferences100 assert sum_.get_by_id("test2") == obj_list[0]101 assert sum_[8].id == "test9"102 assert len(test_list) == 1103 assert len(sum_) == 9104def test_sub(dict_list: Tuple[Object, DictList]) -> None:105 obj, test_list = dict_list106 obj_list = [Object("test%d" % i) for i in range(2, 10)]107 sum_ = test_list + obj_list108 sub = sum_ - test_list109 assert test_list[0].id == "test1"110 assert sub[0].id == "test2"111 assert len(sub) == 8112 assert sum_ - obj_list == test_list113def test_isub(dict_list: Tuple[Object, DictList]) -> None:114 obj, test_list = dict_list115 obj_list = [Object("test%d" % i) for i in range(2, 10)]116 sum_ = test_list + obj_list117 sum_ -= obj_list[2:4]118 assert len(sum_) == 7119 with pytest.raises(ValueError):120 sum_ -= [Object("bogus")]121def test_init_copy(dict_list: Tuple[Object, DictList]) -> None:122 obj, test_list = dict_list123 test_list.append(Object("test2"))124 copied = DictList(test_list)125 assert test_list is not copied126 assert isinstance(copied, test_list.__class__)127 assert len(test_list) == len(copied)128 for i, v in enumerate(test_list):129 assert test_list[i].id == copied[i].id130 assert i == copied.index(v.id)131 assert test_list[i] is copied[i]132 assert v is copied.get_by_id(v.id)133def test_slice(dict_list: Tuple[Object, DictList]) -> None:134 obj, test_list = dict_list135 test_list.append(Object("test2"))136 test_list.append(Object("test3"))137 sliced = test_list[:-1]138 assert test_list is not sliced139 assert isinstance(sliced, test_list.__class__)140 assert len(test_list) == len(sliced) + 1141 for i, v in enumerate(sliced):142 assert test_list[i].id == sliced[i].id143 assert i == sliced.index(v.id)144 assert test_list[i] is sliced[i]145 assert test_list[i] is sliced.get_by_id(v.id)146def test_copy(dict_list: Tuple[Object, DictList]) -> None:147 obj, test_list = dict_list148 test_list.append(Object("test2"))149 copied = copy(test_list)150 assert test_list is not copied151 assert isinstance(copied, test_list.__class__)152 assert len(test_list) == len(copied)153 for i, v in enumerate(test_list):154 assert test_list[i].id == copied[i].id155 assert i == copied.index(v.id)156 assert test_list[i] is copied[i]157 assert v is copied.get_by_id(v.id)158def test_deepcopy(dict_list: Tuple[Object, DictList]) -> None:159 obj, test_list = dict_list160 test_list.append(Object("test2"))161 copied = deepcopy(test_list)162 assert test_list is not copied163 assert isinstance(copied, test_list.__class__)164 assert len(test_list) == len(copied)165 for i, v in enumerate(test_list):166 assert test_list[i].id == copied[i].id167 assert i == copied.index(v.id)168 assert test_list[i] is not copied[i]169 assert v is not copied.get_by_id(v.id)170def test_pickle(dict_list: Tuple[Object, DictList]) -> None:171 obj, test_list = dict_list172 test_list.append(Object("test2"))173 for protocol in range(HIGHEST_PROTOCOL):174 pickle_str = dumps(test_list, protocol=protocol)175 copied = loads(pickle_str)176 assert test_list is not copied177 assert isinstance(copied, test_list.__class__)178 assert len(test_list) == len(copied)179 for i, v in enumerate(test_list):180 assert test_list[i].id == copied[i].id181 assert i == copied.index(v.id)182 assert test_list[i] is not copied[i]183 assert v is not copied.get_by_id(v.id)184def test_query(dict_list: Tuple[Object, DictList]) -> None:185 obj, test_list = dict_list186 obj2 = Object("test2")187 obj2.name = "foobar1"188 test_list.append(obj2)189 result = test_list.query("test1") # matches only test1190 assert len(result) == 1191 result = test_list.query("test1", "id")192 assert len(result) == 1193 assert result[0] == obj194 result = test_list.query("foo", "name") # matches only test2195 assert len(result) == 1196 assert result[0] == obj2197 result = test_list.query("test", "id") # matches test1 and test2198 assert len(result) == 2199 # Test with a regular expression200 result = test_list.query(re.compile("test[0-9]"), "id")201 assert len(result) == 2202 result = test_list.query(re.compile("test[29]"), "id")203 assert len(result) == 1204 # Test query of name205 result = test_list.query(re.compile("foobar."), "name")206 assert len(result) == 1207 # Test query with lambda function208 result = test_list.query(lambda x: x.id == "test1")209 assert len(result) == 1210def test_removal() -> None:211 obj_list = DictList(Object(f"test{i:d}") for i in range(2, 10))212 del obj_list[3]213 assert "test5" not in obj_list214 assert obj_list.index(obj_list[-1]) == len(obj_list) - 1215 assert len(obj_list) == 7216 del obj_list[3:5]217 assert "test6" not in obj_list218 assert "test7" not in obj_list219 assert obj_list.index(obj_list[-1]) == len(obj_list) - 1220 assert len(obj_list) == 5221 removed = obj_list.pop(1)222 assert obj_list.index(obj_list[-1]) == len(obj_list) - 1223 assert removed.id == "test3"224 assert "test3" not in obj_list225 assert len(obj_list) == 4226 removed = obj_list.pop()227 assert removed.id == "test9"228 assert removed.id not in obj_list229 assert len(obj_list) == 3230def test_set() -> None:231 obj_list = DictList(Object(f"test{i:d}") for i in range(10))232 obj_list[4] = Object("testa")233 assert obj_list.index("testa") == 4234 assert obj_list[4].id == "testa"235 obj_list[5:7] = [Object("testb"), Object("testc")]236 assert obj_list.index("testb") == 5237 assert obj_list[5].id == "testb"238 assert obj_list.index("testc") == 6239 assert obj_list[6].id == "testc"240 # Even if the object is unique, if it is present twice in the new241 # list, it should still raise an exception242 with pytest.raises(ValueError):243 obj_list.__setitem__(slice(5, 7), [Object("testd"), Object("testd")])244def test_sort_and_reverse() -> None:245 dl = DictList(Object(f"test{i:d}") for i in reversed(range(10)))246 assert dl[0].id == "test9"247 dl.sort()248 assert len(dl) == 10249 assert dl[0].id == "test0"250 assert dl.index("test0") == 0251 dl.reverse()252 assert dl[0].id == "test9"253 assert dl.index("test0") == 9254def test_dir(dict_list: Tuple[Object, DictList]) -> None:255 obj, test_list = dict_list256 # Make sure tab completion works257 attrs = dir(test_list)258 assert "test1" in attrs259 assert "_dict" in attrs # attribute of DictList260def test_union(dict_list: Tuple[Object, DictList]) -> None:261 obj, test_list = dict_list262 test_list.union([Object("test1"), Object("test2")])263 # Add only 1 element264 assert len(test_list) == 2...

Full Screen

Full Screen

test_SortingAlgorithms.py

Source:test_SortingAlgorithms.py Github

copy

Full Screen

1"""This file contains tests for SortingAlgorithms.py.2"""3from SortingAlgorithms import *4from LinkedList import *5from DoublyLinkedList import *6from typing import Optional, Callable7########################################8# Base tests for non-mutating sorts9########################################10class BaseNoMutatingSort:11 """The base class for a suit of tests that work with in place sorting algorithms.12 Inherit from this class and assign an algorithm to be used in the tests.13 """14 algorithm: Callable[[], None] = staticmethod(None)15 def test_unsortedlist(self) -> None:16 """Test algorithm on an unsorted list"""17 test_list = [9, 7, 5, 2, 4, 5, 3, 3, 2, 1, 10, 200]18 actual = self.algorithm(test_list)19 expected = sorted(test_list)20 assert actual == expected21 def test_sortedlist(self) -> None:22 """Test algorithm on a sorted list"""23 test_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]24 actual = self.algorithm(test_list)25 expected = sorted(test_list)26 assert actual == expected27 def test_reversedlist(self) -> None:28 """Test algorithm on a reversed list"""29 test_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]30 actual = self.algorithm(test_list)31 expected = sorted(test_list)32 assert actual == expected33 def test_key_reverse(self) -> None:34 """Test algorithm on a list, sort with a reversing key."""35 test_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]36 actual = self.algorithm(test_list, (lambda x: -x))37 expected = sorted(test_list, key=(lambda x: -x))38 assert actual == expected39 def test_unsorted_linked_list(self) -> None:40 """Test algorithm on an unsorted list"""41 test_list = LinkedList([9, 7, 5, 2, 4, 5, 3, 3, 2, 1, 10, 200])42 actual = self.algorithm(test_list).to_list()43 expected = sorted(test_list.to_list())44 assert actual == expected45 def test_sorted_linked_list(self) -> None:46 """Test algorithm on a sorted list"""47 test_list = LinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])48 actual = self.algorithm(test_list).to_list()49 expected = sorted(test_list.to_list())50 assert actual == expected51 def test_reversed_linked_list(self) -> None:52 """Test algorithm on a reversed list"""53 test_list = LinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])54 actual = self.algorithm(test_list).to_list()55 expected = sorted(test_list.to_list())56 assert actual == expected57 def test_key_reverse_linked_list(self) -> None:58 """Test algorithm on a list, sort with a reversing key."""59 test_list = LinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])60 actual = self.algorithm(test_list, (lambda x: -x)).to_list()61 expected = sorted(test_list.to_list(), key=(lambda x: -x))62 assert actual == expected63 def test_unsorted_doubly_linked_list(self) -> None:64 """Test algorithm on an unsorted list"""65 test_list = DoublyLinkedList([9, 7, 5, 2, 4, 5, 3, 3, 2, 1, 10, 200])66 actual = self.algorithm(test_list).to_list()67 expected = sorted(test_list.to_list())68 assert actual == expected69 def test_sorted_doubly_linked_list(self) -> None:70 """Test algorithm on a sorted list"""71 test_list = DoublyLinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])72 actual = self.algorithm(test_list).to_list()73 expected = sorted(test_list.to_list())74 assert actual == expected75 def test_reversed_doubly_linked_list(self) -> None:76 """Test algorithm on a reversed list"""77 test_list = DoublyLinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])78 actual = self.algorithm(test_list).to_list()79 expected = sorted(test_list.to_list())80 assert actual == expected81 def test_key_reverse_doubly_linked_list(self) -> None:82 """Test algorithm on a list, sort with a reversing key."""83 test_list = DoublyLinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])84 actual = self.algorithm(test_list, (lambda x: -x)).to_list()85 expected = sorted(test_list.to_list(), key=(lambda x: -x))86 assert actual == expected87########################################88# Base tests for mutating sorts89########################################90class BaseInPlaceSort:91 """The base class for a suit of tests that work with in place sorting algorithms.92 Inherit from this class and assign an algorithm to be used in the tests.93 """94 algorithm: Callable[[], None] = staticmethod(None)95 def test_unsortedlist(self) -> None:96 """Test algorithm on an unsorted list"""97 test_list = [9, 7, 5, 2, 4, 5, 3, 3, 2, 1, 10, 200]98 expected = sorted(test_list.copy())99 self.algorithm(test_list)100 actual = test_list.copy()101 assert actual == expected102 def test_sortedlist(self) -> None:103 """Test algorithm on an sorted list"""104 test_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]105 expected = sorted(test_list.copy())106 self.algorithm(test_list)107 actual = test_list.copy()108 assert actual == expected109 def test_reversedlist(self) -> None:110 """Test algorithm on a reversed list"""111 test_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]112 expected = sorted(test_list.copy())113 self.algorithm(test_list)114 actual = test_list.copy()115 assert actual == expected116 def test_key_reverse(self) -> None:117 """Test algorithm on a list, sort with a reversing key."""118 test_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]119 self.algorithm(test_list, (lambda x: -x))120 actual = test_list.copy()121 expected = sorted(test_list.copy(), key=(lambda x: -x))122 assert actual == expected123 def test_unsorted_linked_list(self) -> None:124 """Test algorithm on an unsorted list"""125 test_list = LinkedList([9, 7, 5, 2, 4, 5, 3, 3, 2, 1, 10, 200])126 expected = sorted(test_list.to_list())127 self.algorithm(test_list)128 actual = test_list.to_list()129 assert actual == expected130 def test_sorted_linked_list(self) -> None:131 """Test algorithm on an sorted list"""132 test_list = LinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])133 expected = sorted(test_list.to_list())134 self.algorithm(test_list)135 actual = test_list.to_list()136 assert actual == expected137 def test_reversed_linked_list(self) -> None:138 """Test algorithm on a reversed list"""139 test_list = LinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])140 expected = sorted(test_list.to_list())141 self.algorithm(test_list)142 actual = test_list.to_list()143 assert actual == expected144 def test_key_reverse_linked_list(self) -> None:145 """Test algorithm on a list, sort with a reversing key."""146 test_list = LinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])147 self.algorithm(test_list, (lambda x: -x))148 actual = test_list.to_list()149 expected = sorted(test_list.to_list(), key=(lambda x: -x))150 assert actual == expected151 def test_unsorted_doubly_linked_list(self) -> None:152 """Test algorithm on an unsorted list"""153 test_list = DoublyLinkedList([9, 7, 5, 2, 4, 5, 3, 3, 2, 1, 10, 200])154 expected = sorted(test_list.to_list())155 self.algorithm(test_list)156 actual = test_list.to_list()157 assert actual == expected158 def test_sorted_doubly_linked_list(self) -> None:159 """Test algorithm on an sorted list"""160 test_list = DoublyLinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])161 expected = sorted(test_list.to_list())162 self.algorithm(test_list)163 actual = test_list.to_list()164 assert actual == expected165 def test_reversed_doubly_linked_list(self) -> None:166 """Test algorithm on a reversed list"""167 test_list = DoublyLinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])168 expected = sorted(test_list.to_list())169 self.algorithm(test_list)170 actual = test_list.to_list()171 assert actual == expected172 def test_key_reverse_doubly_linked_list(self) -> None:173 """Test algorithm on a list, sort with a reversing key."""174 test_list = DoublyLinkedList([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])175 self.algorithm(test_list, (lambda x: -x))176 actual = test_list.to_list()177 expected = sorted(test_list.to_list(), key=(lambda x: -x))178 assert actual == expected179class TestNoMutatingMergesort(BaseNoMutatingSort):180 """Test the non-mutating mergesort algorithm"""181 algorithm: Optional[tuple[Callable[[list], None]]] = staticmethod(no_mut_mergesort)182class TestInPlaceMergesort(BaseInPlaceSort):183 """Test the mutating mergesort algorithm"""184 algorithm: Optional[tuple[Callable[[list], None]]] = staticmethod(mergesort)185class TestInPlaceQuicksort(BaseInPlaceSort):186 """Test the mutating quicksort algorithm"""187 algorithm: Optional[tuple[Callable[[list], None]]] = staticmethod(quicksort)188class TestNoMutatingQuicksort(BaseNoMutatingSort):189 """Test the non-mutating quicksort algorithm"""190 algorithm: Optional[tuple[Callable[[list], None]]] = staticmethod(no_mut_quicksort)191class TestInPlaceInsertionSort(BaseInPlaceSort):192 """Test the mutating insertionsort algorithm"""193 algorithm: Optional[tuple[Callable[[list], None]]] = staticmethod(insertion_sort)194class TestInPlaceSelectionSort(BaseInPlaceSort):195 """Test the mutating selection sort algorithm"""...

Full Screen

Full Screen

sort_summary.py

Source:sort_summary.py Github

copy

Full Screen

1import copy2import random3# region 交换排序4# 冒泡排序 稳定5def sort_bubble(test_list):6 if test_list is None:7 return -18 len_list = len(test_list)9 if len_list <= 0:10 return test_list11 border = len_list - 112 index_tmp = -113 for _ in range(len_list):14 is_sorted = True15 for j in range(border):16 if test_list[j] > test_list[j + 1]:17 test_list[j], test_list[j + 1] = test_list[j + 1], test_list[j]18 is_sorted = False19 index_tmp = j20 if border != index_tmp:21 border = index_tmp22 if is_sorted:23 break24 return test_list25# 快速排序26def sort_quick(test_list, start, end):27 if start > end:28 return29 key = get_pivot(test_list, start, end)30 sort_quick(test_list, start, key - 1)31 sort_quick(test_list, key + 1, end)32 return test_list33def get_pivot(unsorted_list, left_index, right_index):34 border, tmp = left_index, unsorted_list[left_index]35 for i in range(left_index, right_index + 1):36 if unsorted_list[i] < tmp:37 border += 138 unsorted_list[border], unsorted_list[i] = unsorted_list[i], unsorted_list[border]39 if border != left_index:40 unsorted_list[border], unsorted_list[left_index] = unsorted_list[left_index], unsorted_list[border]41 return border42def sort_quick_uo_recurse(test_list):43 index_dic = {'left': 0, 'right': len(test_list) - 1}44 stack = [index_dic]45 while len(stack):46 tmp = stack.pop()47 key = get_pivot(test_list, tmp['left'], tmp['right'])48 if key - 1 > tmp['left']:49 l_part = {'left': tmp['left'], 'right': key - 1}50 stack.append(l_part)51 if key + 1 < tmp['right']:52 r_part = {'left': key + 1, 'right': tmp['right']}53 stack.append(r_part)54 return test_list55# endregion56# region 插入排序57# 插入排序58def sort_insert(test_list):59 len_list = len(test_list)60 for i in range(len_list):61 cur_index = i62 while cur_index >= 1 and test_list[cur_index] < test_list[cur_index - 1]:63 test_list[cur_index], test_list[cur_index - 1] = test_list[cur_index - 1], test_list[cur_index]64 cur_index -= 165 return test_list66# 希尔排序67def sort_shell(test_list):68 len_list = len(test_list)69 gap = len_list // 270 while gap:71 for i in range(gap, len_list):72 j = i73 while j > 0 and test_list[j - gap] > test_list[j]:74 test_list[j - gap], test_list[j] = test_list[j], test_list[j - gap]75 j -= gap76 gap //= 277 return test_list78# endregion79# region 选择排序80# 选择排序81def sort_select(test_list):82 len_list = len(test_list)83 for i in range(len_list):84 min_index = i85 for j in range(i + 1, len_list):86 if test_list[j] < test_list[min_index]:87 min_index = j88 if min_index != i:89 test_list[min_index], test_list[i] = test_list[i], test_list[min_index]90 return test_list91# 堆排序92def heap_build(test_list):93 len_list = len(test_list)94 for i in range(len_list // 2)[::-1]:95 heap_adjust(test_list, i, len_list)96def heap_adjust(test_list, cur_index, border):97 l_leaf = 2 * cur_index + 198 r_leaf = 2 * cur_index + 299 tmp_index = cur_index100 if l_leaf < border and test_list[l_leaf] > test_list[tmp_index]:101 tmp_index = l_leaf102 if r_leaf < border and test_list[r_leaf] > test_list[tmp_index]:103 tmp_index = r_leaf104 if tmp_index != cur_index:105 test_list[tmp_index], test_list[cur_index] = test_list[cur_index], test_list[tmp_index]106 heap_adjust(test_list, tmp_index, border)107def sort_heap(test_list):108 len_list = len(test_list)109 heap_build(test_list)110 for i in range(len_list)[::-1]:111 test_list[0], test_list[i] = test_list[i], test_list[0]112 heap_adjust(test_list, 0, i)113 return test_list114# endregion115# region 归并排序116# 归并排序117def merge_core(list_a, list_b):118 res = []119 while len(list_a) and len(list_b):120 if list_a[0] < list_b[0]:121 res.append(list_a.pop(0))122 else:123 res.append(list_b.pop(0))124 if len(list_a):125 res.extend(list_a)126 if len(list_b):127 res.extend(list_b)128 return res129def sort_merge(test_list):130 if len(test_list) <= 1:131 return test_list132 mid_index = len(test_list) // 2133 l_part = sort_merge(test_list[:mid_index])134 r_part = sort_merge(test_list[mid_index:])135 return merge_core(l_part, r_part)136# endregion137# region 基数排序138# 计数排序139def sort_count(test_list):140 max_value = test_list[0]141 min_value = test_list[0]142 for i in test_list:143 if max_value < i:144 max_value = i145 if min_value > i:146 min_value = i147 gap = max_value - min_value148 tmp_list = [0 for _ in range(gap + 1)]149 for i in test_list:150 tmp_list[i - min_value] += 1151 for i in range(1, gap + 1):152 tmp_list[i] += tmp_list[i - 1]153 result_list = [0 for _ in range(len(test_list))]154 for i in range(len(test_list)):155 result_list[tmp_list[test_list[i] - min_value] - 1] = test_list[i]156 tmp_list[test_list[i] - min_value] -= 1157 return result_list158# 桶排序159def sort_bucket(test_list):160 bucket_num = len(test_list)161 min_value = test_list[0]162 max_value = test_list[0]163 for i in test_list:164 if i > max_value:165 max_value = i166 if i < min_value:167 min_value = i168 gap = max_value - min_value169 buckets = [[] for _ in range(bucket_num)]170 for i in range(bucket_num):171 index = (test_list[i] - min_value) * (bucket_num - 1) // gap172 buckets[index].append(test_list[i])173 for bucket in buckets:174 sorted(bucket)175 return [i for j in buckets for i in j]176# endregion177# region 测试178def sort_test(test):179 bucket_num = len(test)180 max_value = test[0]181 min_value = test[0]182 for i in test:183 if max_value < i:184 max_value = i185 if min_value > i:186 min_value = i187 gap = max_value - min_value188 buckets = [[] for _ in range(bucket_num)]189 for i in range(bucket_num):190 index = (test[i] - min_value) * (bucket_num - 1) // gap191 buckets[index].append(test[i])192 for bucket in buckets:193 sorted(bucket)194 return [j for i in buckets for j in i]195# endregion196if __name__ == '__main__':197 arr_list = [i for i in range(10)]198 random.shuffle(arr_list)199 print(arr_list)...

Full Screen

Full Screen

6-max_integer_test.py

Source:6-max_integer_test.py Github

copy

Full Screen

1#!/usr/bin/python32"""Unittest for max_integer([..])3"""4import unittest5max_integer = __import__("6-max_integer").max_integer6class TestMaxInteger(unittest.TestCase):7 """Class for unittest of max_integer program"""8 def test_documentation(self):9 self.assertTrue(len(__import__("6-max_integer").__doc__) > 0)10 self.assertTrue(len(max_integer.__doc__) > 0)11 def test_positives(self):12 """Testing positive numbers"""13 test_list = [1, 2, 3, 4]14 self.assertEqual(max_integer(test_list), 4)15 test_list = [1, 2, 4, 3]16 self.assertEqual(max_integer(test_list), 4)17 test_list = [4, 4, 4, 4]18 self.assertEqual(max_integer(test_list), 4)19 test_list = [1, 2, 4, 4]20 self.assertEqual(max_integer(test_list), 4)21 test_list = [1.1, 2.2, 3.3, 4.4]22 self.assertEqual(max_integer(test_list), 4.4)23 test_list = [4, 4.1, 4.12, 4.22]24 self.assertEqual(max_integer(test_list), 4.22)25 test_list = [1000, 1, 2, 3]26 self.assertEqual(max_integer(test_list), 1000)27 test_list = [1, 1000, 2, 3]28 self.assertEqual(max_integer(test_list), 1000)29 test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]30 self.assertEqual(max_integer(test_list), 15)31 test_list = [15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]32 self.assertEqual(max_integer(test_list), 15)33 test_list = [15]34 self.assertEqual(max_integer(test_list), 15)35 test_list = [[1, 2, 3, 4], [4, 3, 2, 1]]36 self.assertEqual(max_integer(test_list), [4, 3, 2, 1])37 test_list = (1, 2, 3, 4)38 self.assertEqual(max_integer(test_list), 4)39 test_list = ((1, 2, 3, 4), (4, 3, 2, 1))40 self.assertEqual(max_integer(test_list), (4, 3, 2, 1))41 test_list = ["Derek", "Kwok"]42 self.assertEqual(max_integer(test_list), "Kwok")43 def test_negatives(self):44 """Testing negative numbers"""45 test_list = [-2, 0, 2, 4]46 self.assertEqual(max_integer(test_list), 4)47 test_list = [-7, -6, -5, -4]48 self.assertEqual(max_integer(test_list), -4)49 test_list = [-4.22, -4.12, -4.10, -4.08]50 self.assertEqual(max_integer(test_list), -4.08)51 def test_none_and_zero(self):52 """Testing None and zeros"""53 test_list = []54 self.assertEqual(max_integer(test_list), None)55 test_list = [0, 0, 0, 0]56 self.assertEqual(max_integer(test_list), 0)57 self.assertEqual(max_integer(), None)58 test_list = {}59 self.assertEqual(max_integer(test_list), None)60 test_list = ()61 self.assertEqual(max_integer(test_list), None)62 self.assertEqual(max_integer(), None)63 def test_not_list(self):64 """Testing incorrect data types"""65 test_list = [1, 2, "Derek", 4]66 self.assertRaises(TypeError)67 test_list = [1, 2, [1, 2, 3], 4]68 self.assertRaises(TypeError)69 test_list = [1, 2, (1, 2, 3), 4]70 self.assertRaises(TypeError)71 test_list = 572 self.assertRaises(TypeError)73 test_list = 5.574 self.assertRaises(TypeError)75 test_list = (1, 2, 3, 4)76 self.assertEqual(max_integer(test_list), 4)77 test_list = "Derek"78 self.assertEqual(max_integer(test_list), 'r')79 test_list = 'D'80 self.assertEqual(max_integer(test_list), 'D')81 test_list = None82 self.assertRaises(TypeError)83 test_list = {1, 2, 3, 4}84 self.assertRaises(TypeError)85if __name__ == '__main__':86 unittest.main()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mailosaur-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful