task_id
stringlengths
6
8
text
stringlengths
368
1.62k
Mbpp/641
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the nth nonagonal number assert is_nonagonal(10) == 325 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def is_nonagonal(n): return int(n * (7 * n - 5) / 2) ```
Mbpp/643
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function that checks if a strings contains 'z', except at the start and end of the word assert text_match_wordz_middle("pythonzabc.")==True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def text_match_wordz_middle(text): return re.search(r'\Bz\B', text) is not None ```
Mbpp/644
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to reverse an array upto a given position assert reverse_Array_Upto_K([1, 2, 3, 4, 5, 6],4) == [4, 3, 2, 1, 5, 6] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def reverse_Array_Upto_K(input, k): return input[k-1::-1] + input[k:] ```
Mbpp/720
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to add a dictionary to the tuple assert add_dict_to_tuple((4, 5, 6), {"MSAM" : 1, "is" : 2, "best" : 3} ) == (4, 5, 6, {'MSAM': 1, 'is': 2, 'best': 3}) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def add_dict_to_tuple(test_tup, test_dict): return test_tup + (test_dict, ) ```
Mbpp/721
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Given a square matrix of size N*N given as a list of lists, where each cell is associated with a specific cost assert maxAverageOfPath([[1, 2, 3], [6, 5, 4], [7, 3, 9]]) == 5.2 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def maxAverageOfPath(cost): N = len(cost) dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)] dp[0][0] = cost[0][0] for i in range(1, N): dp[i][0] = dp[i - 1][0] + cost[i][0] for j in range(1, N): dp[0][j] = dp[0][j - 1] + cost[0][j] for i in range(1, N): for j in range(1, N): dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + cost[i][j] # all paths are of length 2 * N - 1, so just divide by that return dp[N - 1][N - 1] / (2 * N - 1) ```
Mbpp/722
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` The input is given as - a dictionary with a student name as a key and a tuple of float (student_height, student_weight) as a value, - minimal height, - minimal weight assert filter_data({'Cierra Vega': (6.2, 70), 'Alden Cantrell': (5.9, 65), 'Kierra Gentry': (6.0, 68), 'Pierre Cox': (5.8, 66)},6.0,70)=={'Cierra Vega': (6.2, 70)} ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def filter_data(students,h,w): return {k: s for k, s in students.items() if s[0] >= h and s[1] >= w} ```
Mbpp/723
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` The input is defined as two lists of the same length assert count_same_pair([1, 2, 3, 4, 5, 6, 7, 8],[2, 2, 3, 1, 2, 6, 7, 9])==4 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python from operator import eq def count_same_pair(nums1, nums2): result = sum(map(eq, nums1, nums2)) return result ```
Mbpp/724
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function that takes base and power as arguments and calculate the sum of all digits of the base to the specified power assert power_base_sum(2,100)==115 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def power_base_sum(base, power): return sum([int(i) for i in str(pow(base, power))]) ```
Mbpp/725
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to extract values between quotation marks " " of the given string assert extract_quotation('Cortex "A53" Based "multi" tasking "Processor"') == ['A53', 'multi', 'Processor'] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def extract_quotation(text1): return re.findall(r'"(.*?)"', text1) ```
Mbpp/726
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function that takes as input a tuple of numbers (t_1, assert multiply_elements((1, 5, 7, 8, 10)) == (5, 35, 56, 80) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def multiply_elements(test_tup): return tuple(i * j for i, j in zip(test_tup, test_tup[1:])) ```
Mbpp/728
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function takes as input two lists [a_1, assert sum_list([10,20,30],[15,25,35])==[25,45,65] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def sum_list(lst1,lst2): return [a + b for a, b in zip(lst1, lst2)] ```
Mbpp/730
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to remove consecutive duplicates of a given list assert consecutive_duplicates([0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4 ])==[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python from itertools import groupby def consecutive_duplicates(nums): return [key for key, _ in groupby(nums)] ```
Mbpp/731
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the lateral surface area of a cone given radius r and the height h assert lateralsurface_cone(5,12)==204.20352248333654 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import math def lateralsurface_cone(r,h): l = math.sqrt(r * r + h * h) return math.pi * r * l ```
Mbpp/732
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to replace all occurrences of spaces, commas, or dots with a colon assert replace_specialchar('Python language, Programming language.')==('Python:language::Programming:language:') ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def replace_specialchar(text): return re.sub("[ ,.]", ":", text) ```
Mbpp/733
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the index of the first occurrence of a given number in a sorted array assert find_first_occurrence([2, 5, 5, 5, 6, 6, 8, 9, 9, 9], 5) == 1 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def find_first_occurrence(A, x): (left, right) = (0, len(A) - 1) result = -1 while left <= right: mid = (left + right) // 2 if x == A[mid]: result = mid right = mid - 1 elif x < A[mid]: right = mid - 1 else: left = mid + 1 return result ```
Mbpp/734
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find sum of products of all possible sublists of a given list assert sum_Of_Subarray_Prod([1,2,3]) == 20 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def sum_Of_Subarray_Prod(arr): result = 0 # final result partial = 0 # partial sum # stimulate the recursion while arr != []: partial = arr[-1] * (1 + partial) result += partial arr.pop() return result ```
Mbpp/735
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to toggle bits of the number except the first and the last bit assert toggle_middle_bits(9) == 15 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def toggle_middle_bits(n): binary = bin(n)[2:] toggled = ''.join(['0' if i == '1' else '1' for i in binary[1:-1]]) return int(binary[0] + toggled + binary[-1], 2) ```
Mbpp/736
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to locate the left insertion point for a specified value in sorted order assert left_insertion([1,2,4,5],6)==4 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import bisect def left_insertion(a, x): return bisect.bisect_left(a, x) ```
Mbpp/737
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check whether the given string is starting with a vowel or not using regex assert check_str("annie") ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def check_str(string): regex = '^[aeiouAEIOU][A-Za-z0-9_]*' return re.search(regex, string) ```
Mbpp/739
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the index of smallest triangular number with n digits assert find_Index(2) == 4 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import math def find_Index(n): x = math.sqrt(2 * math.pow(10,(n - 1))) return round(x) ```
Mbpp/740
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to convert the given tuple to a key-value dictionary using adjacent elements assert tuple_to_dict((1, 5, 7, 10, 13, 5)) == {1: 5, 7: 10, 13: 5} ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def tuple_to_dict(test_tup): return dict(test_tup[idx : idx + 2] for idx in range(0, len(test_tup), 2)) ```
Mbpp/741
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to check whether all the characters are same or not assert all_Characters_Same("python") == False ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def all_Characters_Same(s) : return all(ch == s[0] for ch in s[1:]) ```
Mbpp/742
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to caluclate the area of a tetrahedron assert area_tetrahedron(3)==15.588457268119894 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import math def area_tetrahedron(side): return math.sqrt(3)*(side*side) ```
Mbpp/743
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to rotate a given list by specified number of items to the right direction assert rotate_right([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],3)==[8, 9, 10, 1, 2, 3, 4, 5, 6, 7] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def rotate_right(l, m): return l[-m:] + l[:-m] ```
Mbpp/744
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check if the given tuple has any none value or not assert check_none((10, 4, 5, 6, None)) == True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def check_none(test_tup): return any(ele is None for ele in test_tup) ```
Mbpp/745
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find numbers within a given range from startnum ti endnum where every number is divisible by every digit it contains assert divisible_by_digits(1,22)==[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def divisible_by_digits(startnum, endnum): return [n for n in range(startnum, endnum+1) \ if not any(map(lambda x: int(x) == 0 or n%int(x) != 0, str(n)))] ```
Mbpp/748
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to put spaces between words starting with capital letters in a given string assert capital_words_spaces("Python") == 'Python' ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def capital_words_spaces(str1): return re.sub(r"(\w)([A-Z])", r"\1 \2", str1) ```
Mbpp/749
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to sort a given list of strings of numbers numerically assert sort_numeric_strings( ['4','12','45','7','0','100','200','-12','-500'])==[-500, -12, 0, 4, 7, 12, 45, 100, 200] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def sort_numeric_strings(nums_str): return sorted([int(x) for x in nums_str]) ```
Mbpp/750
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to add the given tuple to the given list assert add_tuple([5, 6, 7], (9, 10)) == [5, 6, 7, 9, 10] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def add_tuple(test_list, test_tup): return test_list + list(test_tup) ```
Mbpp/751
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check if the given array represents min heap or not assert check_min_heap([1, 2, 3, 4, 5, 6]) == True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def check_min_heap_helper(arr, i): if 2 * i + 2 > len(arr): return True left_child = (arr[i] <= arr[2 * i + 1]) and check_min_heap_helper(arr, 2 * i + 1) right_child = (2 * i + 2 == len(arr)) or \ (arr[i] <= arr[2 * i + 2] and \ check_min_heap_helper(arr, 2 * i + 2)) return left_child and right_child def check_min_heap(arr): return check_min_heap_helper(arr, 0) ```
Mbpp/752
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the nth jacobsthal number assert jacobsthal_num(5) == 11 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def jacobsthal_num(n): dp = [0] * (n + 1) dp[0] = 0 dp[1] = 1 for i in range(2, n+1): dp[i] = dp[i - 1] + 2 * dp[i - 2] return dp[n] ```
Mbpp/753
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find minimum k records from tuple list assert min_k([('Manjeet', 10), ('Akshat', 4), ('Akash', 2), ('Nikhil', 8)], 2) == [('Akash', 2), ('Akshat', 4)] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def min_k(test_list, K): res = sorted(test_list, key = lambda x: x[1])[:K] return (res) ```
Mbpp/754
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` We say that an element is common for lists l1, l2, l3 if it appears in all three lists under the same index assert extract_index_list([1, 1, 3, 4, 5, 6, 7],[0, 1, 2, 3, 4, 5, 7],[0, 1, 2, 3, 4, 5, 7])==[1, 7] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def extract_index_list(l1, l2, l3): return [a for a, b, c in zip(l1, l2, l3) if a == b == c] ```
Mbpp/755
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the second smallest number in a list assert second_smallest([1, 2, -8, -2, 0, -2])==-2 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def second_smallest(numbers): sorted_set = sorted(set(numbers)) if len(sorted_set) < 2: return None return sorted_set[1] ```
Mbpp/757
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to count the pairs of reverse strings in the given string list assert count_reverse_pairs(["julia", "best", "tseb", "for", "ailuj"])== 2 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def count_reverse_pairs(test_list): return sum(test_list[i+1:].count(s[::-1]) for i, s in enumerate(test_list)) ```
Mbpp/758
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to count lists within a list assert unique_sublists([[1, 3], [5, 7], [1, 3], [13, 15, 17], [5, 7], [9, 11]] )=={(1, 3): 2, (5, 7): 2, (13, 15, 17): 1, (9, 11): 1} ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def unique_sublists(list1): return {tuple(x): list1.count(x) for x in list1} ```
Mbpp/759
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check whether a given string is a decimal number with a precision of 2 assert is_decimal('123.11')==True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def is_decimal(num): import re dnumre = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""") return dnumre.search(num) is not None ```
Mbpp/760
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to check whether a list of numbers contains only one distinct element or not assert unique_Element([1,1,1]) == True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def unique_Element(arr): return arr.count(arr[0]) == len(arr) ```
Mbpp/762
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check whether the given month number contains 30 days or not assert check_monthnumber_number(6)==True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def check_monthnumber_number(monthnum3): return monthnum3==4 or monthnum3==6 or monthnum3==9 or monthnum3==11 ```
Mbpp/763
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the minimum difference between any two elements in a given array assert find_min_diff((1,5,3,19,18,25),6) == 1 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def find_min_diff(arr,n): arr = sorted(arr) diff = 10**20 for i in range(n-1): if arr[i+1] - arr[i] < diff: diff = arr[i+1] - arr[i] return diff ```
Mbpp/764
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to count number of digits in a given string assert number_ctr('program2bedone') == 1 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def number_ctr(s): return sum(c.isdigit() for c in s) ```
Mbpp/765
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find nth polite number assert is_polite(7) == 11 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import math def is_polite(n): n = n + 1 return (int)(n+(math.log((n + math.log(n, 2)), 2))) ```
Mbpp/766
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to return a list of all pairs of consecutive items in a given list assert pair_wise([1,1,2,3,3,4,4,5])==[(1, 1), (1, 2), (2, 3), (3, 3), (3, 4), (4, 4), (4, 5)] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def pair_wise(l1): return list(zip(l1, l1[1:])) ```
Mbpp/767
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to count the number of pairs whose sum is equal to ‘sum’ assert get_pairs_count([1,1,1,1],2) == 6 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def get_pairs_count(arr, sum_): cnt = 0 for n in arr: cnt += arr.count(sum_ - n) if sum_ - n == n: cnt -= 1 return cnt / 2 ```
Mbpp/769
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to get the difference between two lists assert (Diff([10, 15, 20, 25, 30, 35, 40], [25, 40, 35])) == [10, 20, 30, 15] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def Diff(li1,li2): return list(set(li1)-set(li2)) + list(set(li2)-set(li1)) ```
Mbpp/770
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the sum of fourth power of first n odd natural numbers assert odd_num_sum(2) == 82 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def odd_num_sum(n) : j = 0 sm = 0 for i in range(1,n + 1) : j = (2*i-1) sm = sm + (j*j*j*j) return sm ```
Mbpp/771
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check if the given expression is balanced or not assert check_expression("{()}[{}]") == True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python from collections import deque def check_expression(exp): if len(exp) == 0 or len(exp) % 2 == 1: return False stack = deque() for ch in exp: if ch == '(' or ch == '{' or ch == '[': stack.append(ch) if ch == ')' or ch == '}' or ch == ']': if not stack: return False top = stack.pop() if (top == '(' and ch != ')') or (top == '{' and ch != '}' or (top == '[' and ch != ']')): return False return not stack ```
Mbpp/772
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to remove all the words with k length in the given string assert remove_length('The person is most value tet', 3) == 'person is most value' ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def remove_length(test_str, K): return ' '.join([i for i in test_str.split() if len(i) != K]) ```
Mbpp/773
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the occurrence and position of the substrings within a string assert occurance_substring('python programming, python language','python')==('python', 0, 6) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def occurance_substring(text,pattern): for match in re.finditer(pattern, text): s = match.start() e = match.end() return (text[s:e], s, e) return None ```
Mbpp/775
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to check whether every odd index contains odd numbers of a given list assert odd_position([2,1,4,3,6,7,6,3]) == True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def odd_position(nums): return all(n % 2 == 1 for n in nums[1::2]) ```
Mbpp/777
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the sum of non-repeated elements in a given list assert find_sum([1,2,3,1,1,4,5,6]) == 21 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def find_sum(arr): return sum(set(arr)) ```
Mbpp/778
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to pack consecutive duplicates of a given list elements into sublists assert pack_consecutive_duplicates([0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4])==[[0, 0], [1], [2], [3], [4, 4], [5], [6, 6, 6], [7], [8], [9], [4, 4]] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python from itertools import groupby def pack_consecutive_duplicates(list1): return [list(group) for _, group in groupby(list1)] ```
Mbpp/780
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the combinations of sums with tuples in the given tuple list assert find_combinations([(2, 4), (6, 7), (5, 1), (6, 10)]) == [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python from itertools import combinations def find_combinations(test_list): return [tuple(map(sum, zip(*t))) for t in combinations(test_list, 2)] ```
Mbpp/781
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to check whether the count of divisors is even assert count_divisors(10) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import math def count_divisors(n) : cnt = 0 for i in range(1, (int)(math.sqrt(n)) + 1) : if (n % i == 0) : if (n / i == i) : cnt = cnt + 1 else : cnt = cnt + 2 return cnt % 2 == 0 ```
Mbpp/782
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the sum of all odd length subarrays assert odd_length_sum([1,2,4]) == 14 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def odd_length_sum(arr): sum_ = 0 n = len(arr) for i in range(n): # arr[i] occurs (i + 1) * (n - i) times in all subarrays times = ((i + 1) * (n - i) + 1) // 2 sum_ += arr[i] * times return sum_ ```
Mbpp/784
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find the product of first even and odd number of a given list assert mul_even_odd([1,3,5,7,4,1,6,8])==4 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def mul_even_odd(list1): first_even = next((el for el in list1 if el%2==0),-1) first_odd = next((el for el in list1 if el%2!=0),-1) return (first_even*first_odd) ```
Mbpp/785
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to convert tuple string to integer tuple assert tuple_str_int("(7, 8, 9)") == (7, 8, 9) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def tuple_str_int(test_str): return tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', ')) ```
Mbpp/786
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to locate the right insertion point for a specified value in sorted order assert right_insertion([1,2,4,5],6)==4 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import bisect def right_insertion(a, x): return bisect.bisect_right(a, x) ```
Mbpp/787
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function that matches a string that has an a followed by three 'b' assert not text_match_three("ac") ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def text_match_three(text): patterns = 'ab{3}?' return re.search(patterns, text) ```
Mbpp/788
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to create a new tuple from the given string and list assert new_tuple(["WEB", "is"], "best") == ('WEB', 'is', 'best') ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def new_tuple(test_list, test_str): return tuple(test_list + [test_str]) ```
Mbpp/790
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to check whether every even index contains even numbers of a given list assert even_position([3,2,1]) == False ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def even_position(nums): return all(nums[i]%2==i%2 for i in range(len(nums))) ```
Mbpp/791
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to remove tuples from the given tuple assert remove_nested((1, 5, 7, (4, 6), 10)) == (1, 5, 7, 10) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def remove_nested(test_tup): return tuple(e for e in test_tup if not isinstance(e, tuple)) ```
Mbpp/792
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to count the number of lists in a given number of lists assert count_list([[1, 3], [5, 7], [9, 11], [13, 15, 17]]) == 4 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def count_list(input_list): return sum(isinstance(e, list) for e in input_list) ```
Mbpp/793
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the last position of an element in a sorted array assert last([1,2,3],1) == 0 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def last(arr,x): return len(arr)-arr[::-1].index(x) - 1 ```
Mbpp/794
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function that matches a string that has an 'a' followed by anything, ending in 'b' assert text_starta_endb("aabbbb") ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python import re def text_starta_endb(text): patterns = 'a.*?b$' return re.search(patterns, text) ```
Mbpp/796
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write function to find the sum of all items in the given dictionary assert return_sum({'a': 100, 'b':200, 'c':300}) == 600 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def return_sum(d): return sum(d.values()) ```
Mbpp/797
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the sum of all odd natural numbers within the range l and r assert sum_in_range(2,5) == 8 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def sum_odd(n): terms = (n + 1) // 2 sum1 = terms * terms return sum1 def sum_in_range(l,r): return sum_odd(r) - sum_odd(l - 1) ```
Mbpp/798
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the sum of an array assert _sum([1, 2, 3]) == 6 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def _sum(arr): return sum(arr) ```
Mbpp/799
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to that rotate left bits by d bits a given number assert left_rotate(16,2) == 64 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def left_rotate(n,d): INT_BITS = 32 return (n << d)|(n >> (INT_BITS - d)) ```
Mbpp/800
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to remove all whitespaces from a string assert remove_all_spaces('python program')==('pythonprogram') ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def remove_all_spaces(text): return text.replace(' ', '') ```
Mbpp/801
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to count the number of equal numbers from three given integers assert test_three_equal(1,1,1) == 3 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def test_three_equal(x,y,z): result = set([x,y,z]) if len(result) == 3: return 0 elif len(result) == 2: return 2 else: return 3 ```
Mbpp/803
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check whether the given number is a perfect square or not assert not is_perfect_square(10) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def is_perfect_square(n) : if n < 0: return False return n**(1/2) == int(n**(1/2)) ```
Mbpp/804
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check whether the product of numbers in a list is even or not assert is_product_even([1,2,3]) ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def is_product_even(arr): return any(x % 2 == 0 for x in arr) ```
Mbpp/805
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function that returns the list in a list of lists whose sum of elements is the highest assert max_sum_list([[1,2,3], [4,5,6], [10,11,12], [7,8,9]])==[10, 11, 12] ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def max_sum_list(lists): return max(lists, key=sum) ```
Mbpp/806
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to find maximum run of uppercase characters in the given string assert max_run_uppercase('GeMKSForGERksISBESt') == 5 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def max_run_uppercase(test_str): cnt = 0 res = 0 for idx in range(0, len(test_str)): if test_str[idx].isupper(): cnt += 1 else: res = cnt cnt = 0 if test_str[len(test_str) - 1].isupper(): res = cnt return res ```
Mbpp/807
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a python function to find the first odd number in a given list of numbers assert first_odd([1,3,5]) == 1 ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def first_odd(nums): first_odd = next((el for el in nums if el%2!=0), None) return first_odd ```
Mbpp/808
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check if the given tuples contain the k or not assert check_K((10, 4, 5, 6, 8), 6) == True ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def check_K(test_tup, K): return K in test_tup ```
Mbpp/809
Please provide a self-contained Python script that solves the following problem in a markdown code block: ``` Write a function to check if each element of second tuple is smaller than its corresponding element in the first tuple assert check_smaller((1, 2, 3), (2, 3, 4)) == False ``` ### Answer: Below is a Python script with a self-contained function that solves the problem and passes corresponding tests: ```python def check_smaller(test_tup1, test_tup2): return all(x > y for x, y in zip(test_tup1, test_tup2)) ```