def r(i): cnt_nums = randint(1, 50) k = randint(1, cnt_nums) items = randints(cnt_nums, unique=True, min_num=MIN_INT, max_num=MAX_INT) num_repeats = randints(cnt_nums, unique=True, min_num=1, max_num=cnt_nums + 1) num_repeats.sort(reverse=True) nums = sum(([item] * repeat for item, repeat in zip(items, num_repeats)), []) shuffle(nums) return Case(nums, k, result=items[:k])
def random_numbers(i): """ :param int i: number of times this function is called starting from 0 :return Case: """ nums1 = sorted(randints(count=i, max_num=i * 100)) nums2 = sorted(randints(count=max(i, 1), max_num=i * 100)) result = float(median(nums1 + nums2)) return Case(nums1, nums2, result=result)
def random_complete_tree(i): vals = randints(i + 2, max_num=i * 100) root = TreeNode.from_iterable(vals) i_first_leaf = int(len(vals) / 2) i_last_left_left = len(vals) - len(vals) % 2 - 1 result = sum(vals[i_last_left_left:i_first_leaf - 1:-2]) return Case(root, result=result)
def r(i): cnt_vals = randint(0, i) odd_vals = randints(cnt_vals) even_vals = randints(cnt_vals) vals = [val for pair in zip(odd_vals, even_vals) for val in pair] if even_vals and randbool(): even_vals.pop() vals.pop() root = ListNode.from_iterable(vals) odd_root = ListNode.from_iterable(odd_vals) if odd_root: even_root = ListNode.from_iterable(even_vals) odd_root.end().next = even_root return Case(root, result=odd_root)
def r(i): length = randint(1, 20) ints = randints(length, max_num=9, min_num=1) count = [ ints[i] > ints[j] * 2 for i in range(0, length) for j in range(i + 1, length) ].count(True) return Case(ints, result=count)
def r(i): cnt_twice = randint(0, i) cnt_nums = cnt_twice + i nums = randints(i, unique=True, max_num=cnt_nums, min_num=1) twice_nums = nums[:cnt_twice] once_nums = nums[cnt_twice:] nums = twice_nums * 2 + once_nums shuffle(nums) return Case(nums, result=twice_nums)
def test_from_iterable(self): root = ListNode.from_iterable([]) self.assertIsNone(root) for i in range(1, 100): vals = randints(i) root = ListNode.from_iterable(vals) vals2 = list(root) self.assertEqual(vals, vals2)
def test_from_iterable(self): root = TreeNode.from_iterable([]) self.assertIsNone(root) for i in range(1, 100): vals = randints(i) root = TreeNode.from_iterable(vals) vals2 = root.flatten() self.assertEqual(vals, vals2)
def r(i): cnt_twice = randint(0, i) cnt_nums = cnt_twice + i nums = randints(i, unique=True, max_num=cnt_nums, min_num=1) twice_nums = nums[:cnt_twice] once_nums = nums[cnt_twice:] nums = twice_nums * 2 + once_nums shuffle(nums) missing = list(set(range(1, cnt_nums + 1)) - set(nums)) return Case(nums, result=missing)
def r(i): ops = [] nums = randints(max(1, i), max_num=75) tmp_nums = [] for num in nums: tmp_nums.append(num) tmp_nums.sort() if len(tmp_nums) % 2 == 1: median = tmp_nums[len(tmp_nums) // 2] else: median = float(tmp_nums[len(tmp_nums) // 2] + tmp_nums[len(tmp_nums) // 2 - 1]) / 2 ops.extend(['addNum', [num], 'findMedian', Result(median)]) return Case(*ops)
def r(i): num_cits = randint(0, i) citations = randints(count=num_cits, max_num=num_cits * 2) citations.sort() count = 0 min_cites = MAX_INT for citation in reversed(citations): min_cites = min(citation, min_cites) count += 1 if count > min_cites: count -= 1 break return Case(citations, result=count)
def r(i): n = randint(1, i + 1) nums = randints(n) k = randint(1, n) maxes = [max(nums[i:i + k]) for i in range(n - k + 1)] return Case(nums, k, result=maxes)
def test_end(self): for i in range(1, 100): vals = randints(i) root = ListNode.from_iterable(vals) vals = list(root) self.assertEqual(root.end().val, vals[-1])
def r(i): matrix = [randints(count=i, max_num=i) for _ in range(i)] result = list(zip(*reversed(matrix))) return Case(matrix, result=result)
def r(i): nums = randints(max(1, i)) res_nums = sorted(set(nums)) res = res_nums[-3 if len(res_nums) >= 3 else -1] return Case(nums, result=res)