input
stringlengths
177
388
output
stringlengths
30
908
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the equilibrium index of the given array. ### Response:
def equilibrium_index(arr): total_sum = sum(arr) left_sum=0 for i, num in enumerate(arr): total_sum -= num if left_sum == total_sum: return i left_sum += num return -1
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the minimum number of elements that should be removed such that amax-amin<=k. ### Response:
def find_ind(key, i, n, k, arr): ind = -1 start = i + 1 end = n - 1; while (start < end): mid = int(start + (end - start) / 2) if (arr[mid] - key <= k): ind = mid start = mid + 1 else: end = mid return ind def removals(arr, n, k): ans = n - 1 arr.sort() for i in range(0, n): j = find_ind(arr[i], i, n, k, arr) if (j != -1): ans = min(ans, n - (j - i + 1)) return ans
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check whether the given key is present in the dictionary or not. ### Response:
def is_key_present(d,x): if x in d: return True else: return False
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to calculate the harmonic sum of n-1. ### Response:
def harmonic_sum(n): if n < 2: return 1 else: return 1 / n + (harmonic_sum(n - 1))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to sort a list of lists by length and value. ### Response:
def sort_sublists(list1): list1.sort() list1.sort(key=len) return list1
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find whether an array is subset of another array. ### Response:
def is_subset(arr1, m, arr2, n): hashset = set() for i in range(0, m): hashset.add(arr1[i]) for i in range(0, n): if arr2[i] in hashset: continue else: return False return True
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to count the total set bits from 1 to n. ### Response:
def count_Set_Bits(n) : n += 1; powerOf2 = 2; cnt = n // 2; while (powerOf2 <= n) : totalPairs = n // powerOf2; cnt += (totalPairs // 2) * powerOf2; if (totalPairs & 1) : cnt += (n % powerOf2) else : cnt += 0 powerOf2 <<= 1; return cnt;
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to convert a string to a list. ### Response:
def Convert(string): li = list(string.split(" ")) return li
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to count unique keys for each value present in the tuple. ### Response:
from collections import defaultdict def get_unique(test_list): res = defaultdict(list) for sub in test_list: res[sub[1]].append(sub[0]) res = dict(res) res_dict = dict() for key in res: res_dict[key] = len(list(set(res[key]))) return (str(res_dict))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to access the initial and last data of the given tuple record. ### Response:
def front_and_rear(test_tup): res = (test_tup[0], test_tup[-1]) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check whether the product of digits of a number at even and odd places is equal or not. ### Response:
def product_Equal(n): if n < 10: return False prodOdd = 1; prodEven = 1 while n > 0: digit = n % 10 prodOdd *= digit n = n//10 if n == 0: break; digit = n % 10 prodEven *= digit n = n//10 if prodOdd == prodEven: return True return False
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to remove duplicates from a list of lists. ### Response:
import itertools def remove_duplicate(list1): list.sort(list1) remove_duplicate = list(list1 for list1,_ in itertools.groupby(list1)) return remove_duplicate
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check if the given tuple contains all valid values or not. ### Response:
def check_valid(test_tup): res = not any(map(lambda ele: not ele, test_tup)) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to count the number of distinct power of prime factor of given number. ### Response:
def count_Fac(n): m = n count = 0 i = 2 while((i * i) <= m): total = 0 while (n % i == 0): n /= i total += 1 temp = 0 j = 1 while((temp + j) <= total): temp += j count += 1 j += 1 i += 1 if (n != 1): count += 1 return count
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to convert the given string of integers into a tuple. ### Response:
def str_to_tuple(test_str): res = tuple(map(int, test_str.split(', '))) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the perimeter of a rombus. ### Response:
def rombus_perimeter(a): perimeter=4*a return perimeter
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to calculate the standard deviation. ### Response:
import math import sys def sd_calc(data): n = len(data) if n <= 1: return 0.0 mean, sd = avg_calc(data), 0.0 for el in data: sd += (float(el) - mean)**2 sd = math.sqrt(sd / float(n-1)) return sd def avg_calc(ls): n, mean = len(ls), 0.0 if n <= 1: return ls[0] for el in ls: mean = mean + float(el) mean = mean / float(n) return mean
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to create a list taking alternate elements from another given list. ### Response:
def alternate_elements(list1): result=[] for item in list1[::2]: result.append(item) return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function that matches a string that has an a followed by zero or more b's. ### Response:
import re def text_match(text): patterns = 'ab*?' if re.search(patterns, text): return 'Found a match!' else: return('Not matched!')
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to add a dictionary to the tuple. ### Response:
def add_dict_to_tuple(test_tup, test_dict): test_tup = list(test_tup) test_tup.append(test_dict) test_tup = tuple(test_tup) return (test_tup)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find a path with the maximum average over all existing paths for the given square matrix of size n*n. ### Response:
M = 100 def maxAverageOfPath(cost, N): dp = [[0 for i in range(N + 1)] for j 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] return dp[N - 1][N - 1] / (2 * N - 1)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to filter the height and width of students which are stored in a dictionary. ### Response:
def filter_data(students,h,w): result = {k: s for k, s in students.items() if s[0] >=h and s[1] >=w} return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to count the same pair in two given lists using map function. ### Response:
from operator import eq def count_same_pair(nums1, nums2): result = sum(map(eq, nums1, nums2)) return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to calculate the sum of all digits of the base to the specified power. ### Response:
def power_base_sum(base, power): return sum([int(i) for i in str(pow(base, power))])
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to extract values between quotation marks of the given string by using regex. ### Response:
import re def extract_quotation(text1): return (re.findall(r'"(.*?)"', text1))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to multiply the adjacent elements of the given tuple. ### Response:
def multiply_elements(test_tup): res = tuple(i * j for i, j in zip(test_tup, test_tup[1:])) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to remove all characters except letters and numbers using regex ### Response:
import re def remove_char(S): result = re.sub('[\W_]+', '', S) return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to sum elements in two lists. ### Response:
def sum_list(lst1,lst2): res_list = [lst1[i] + lst2[i] for i in range(len(lst1))] return res_list
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to add two lists using map and lambda function. ### Response:
def add_list(nums1,nums2): result = map(lambda x, y: x + y, nums1, nums2) return list(result)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to remove consecutive duplicates of a given list. ### Response:
from itertools import groupby def consecutive_duplicates(nums): return [key for key, group in groupby(nums)]
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the lateral surface area of a cone. ### Response:
import math def lateralsurface_cone(r,h): l = math.sqrt(r * r + h * h) LSA = math.pi * r * l return LSA
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to replace all occurrences of spaces, commas, or dots with a colon. ### Response:
import re def replace_specialchar(text): return (re.sub("[ ,.]", ":", text))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the index of the first occurrence of a given number in a sorted array. ### Response:
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
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find sum of products of all possible subarrays. ### Response:
def sum_Of_Subarray_Prod(arr,n): ans = 0 res = 0 i = n - 1 while (i >= 0): incr = arr[i]*(1 + res) ans += incr res = incr i -= 1 return (ans)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to toggle bits of the number except the first and the last bit. ### Response:
def set_middle_bits(n): n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return (n >> 1) ^ 1 def toggle_middle_bits(n): if (n == 1): return 1 return n ^ set_middle_bits(n)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to locate the left insertion point for a specified value in sorted order. ### Response:
import bisect def left_insertion(a, x): i = bisect.bisect_left(a, x) return i
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check whether the given string is starting with a vowel or not using regex. ### Response:
import re regex = '^[aeiouAEIOU][A-Za-z0-9_]*' def check_str(string): if(re.search(regex, string)): return ("Valid") else: return ("Invalid")
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to calculate the geometric sum of n-1. ### Response:
def geometric_sum(n): if n < 0: return 0 else: return 1 / (pow(2, n)) + geometric_sum(n - 1)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the index of smallest triangular number with n digits. ### Response:
import math def find_Index(n): x = math.sqrt(2 * math.pow(10,(n - 1))); return round(x);
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to convert the given tuple to a key-value dictionary using adjacent elements. ### Response:
def tuple_to_dict(test_tup): res = dict(test_tup[idx : idx + 2] for idx in range(0, len(test_tup), 2)) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check whether all the characters are same or not. ### Response:
def all_Characters_Same(s) : n = len(s) for i in range(1,n) : if s[i] != s[0] : return False return True
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to caluclate the area of a tetrahedron. ### Response:
import math def area_tetrahedron(side): area = math.sqrt(3)*(side*side) return area
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to rotate a given list by specified number of items to the right direction. ### Response:
def rotate_right(list1,m,n): result = list1[-(m):]+list1[:-(n)] return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check if the given tuple has any none value or not. ### Response:
def check_none(test_tup): res = any(map(lambda ele: ele is None, test_tup)) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find numbers within a given range where every number is divisible by every digit it contains. ### Response:
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)))]
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find area of a sector. ### Response:
def sector_area(r,a): pi=22/7 if a >= 360: return None sectorarea = (pi*r**2) * (a/360) return sectorarea
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the longest common subsequence for the given three string sequence. ### Response:
def lcs_of_three(X, Y, Z, m, n, o): L = [[[0 for i in range(o+1)] for j in range(n+1)] for k in range(m+1)] for i in range(m+1): for j in range(n+1): for k in range(o+1): if (i == 0 or j == 0 or k == 0): L[i][j][k] = 0 elif (X[i-1] == Y[j-1] and X[i-1] == Z[k-1]): L[i][j][k] = L[i-1][j-1][k-1] + 1 else: L[i][j][k] = max(max(L[i-1][j][k], L[i][j-1][k]), L[i][j][k-1]) return L[m][n][o]
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to put spaces between words starting with capital letters in a given string by using regex. ### Response:
import re def capital_words_spaces(str1): return re.sub(r"(\w)([A-Z])", r"\1 \2", str1)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to sort a given list of strings of numbers numerically. ### Response:
def sort_numeric_strings(nums_str): result = [int(x) for x in nums_str] result.sort() return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to add the given tuple to the given list. ### Response:
def add_tuple(test_list, test_tup): test_list += test_tup return (test_list)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check if the given array represents min heap or not. ### Response:
def check_min_heap(arr, i): if 2 * i + 2 > len(arr): return True left_child = (arr[i] <= arr[2 * i + 1]) and check_min_heap(arr, 2 * i + 1) right_child = (2 * i + 2 == len(arr)) or (arr[i] <= arr[2 * i + 2] and check_min_heap(arr, 2 * i + 2)) return left_child and right_child
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the nth jacobsthal number. ### Response:
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]
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find minimum k records from tuple list. ### Response:
def min_k(test_list, K): res = sorted(test_list, key = lambda x: x[1])[:K] return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find common index elements from three lists. ### Response:
def extract_index_list(l1, l2, l3): result = [] for m, n, o in zip(l1, l2, l3): if (m == n == o): result.append(m) return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the second smallest number in a list. ### Response:
def second_smallest(numbers): if (len(numbers)<2): return if ((len(numbers)==2) and (numbers[0] == numbers[1]) ): return dup_items = set() uniq_items = [] for x in numbers: if x not in dup_items: uniq_items.append(x) dup_items.add(x) uniq_items.sort() return uniq_items[1]
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function that matches a string that has an a followed by zero or one 'b'. ### Response:
import re def text_match_zero_one(text): patterns = 'ab?' if re.search(patterns, text): return 'Found a match!' else: return('Not matched!')
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to count the pairs of reverse strings in the given string list. ### Response:
def count_reverse_pairs(test_list): res = sum([1 for idx in range(0, len(test_list)) for idxn in range(idx, len( test_list)) if test_list[idxn] == str(''.join(list(reversed(test_list[idx]))))]) return str(res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to count number of unique lists within a list. ### Response:
def unique_sublists(list1): result ={} for l in list1: result.setdefault(tuple(l), list()).append(1) for a, b in result.items(): result[a] = sum(b) return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check a decimal with a precision of 2. ### Response:
def is_decimal(num): import re dnumre = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""") result = dnumre.search(num) return bool(result)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check whether an array contains only one distinct element or not. ### Response:
def unique_Element(arr,n): s = set(arr) if (len(s) == 1): return ('YES') else: return ('NO')
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to caluclate arc length of an angle. ### Response:
def arc_length(d,a): pi=22/7 if a >= 360: return None arclength = (pi*d) * (a/360) return arclength
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check whether the given month number contains 30 days or not. ### Response:
def check_monthnumber_number(monthnum3): if(monthnum3==4 or monthnum3==6 or monthnum3==9 or monthnum3==11): return True else: return False
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the minimum difference between any two elements in a given array. ### Response:
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
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to count numeric values in a given string. ### Response:
def number_ctr(str): number_ctr= 0 for i in range(len(str)): if str[i] >= '0' and str[i] <= '9': number_ctr += 1 return number_ctr
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find nth polite number. ### Response:
import math def is_polite(n): n = n + 1 return (int)(n+(math.log((n + math.log(n, 2)), 2)))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to iterate over all pairs of consecutive items in a given list. ### Response:
def pair_wise(l1): temp = [] for i in range(len(l1) - 1): current_element, next_element = l1[i], l1[i + 1] x = (current_element, next_element) temp.append(x) return temp
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to count the number of pairs whose sum is equal to ‘sum’. ### Response:
def get_Pairs_Count(arr,n,sum): count = 0 for i in range(0,n): for j in range(i + 1,n): if arr[i] + arr[j] == sum: count += 1 return count
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check for odd parity of a given number. ### Response:
def check_Odd_Parity(x): parity = 0 while (x != 0): x = x & (x - 1) parity += 1 if (parity % 2 == 1): return True else: return False
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to get the difference between two lists. ### Response:
def Diff(li1,li2): return (list(list(set(li1)-set(li2)) + list(set(li2)-set(li1))))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the sum of fourth power of first n odd natural numbers. ### Response:
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
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check if the given expression is balanced or not. ### Response:
from collections import deque def check_expression(exp): if len(exp) & 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
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to remove all the words with k length in the given string. ### Response:
def remove_length(test_str, K): temp = test_str.split() res = [ele for ele in temp if len(ele) != K] res = ' '.join(res) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the occurrence and position of the substrings within a string. ### Response:
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)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to check if the string is a valid email address or not using regex. ### Response:
import re regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' def check_email(email): if(re.search(regex,email)): return ("Valid Email") else: return ("Invalid Email")
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check whether every odd index contains odd numbers of a given list. ### Response:
def odd_position(nums): return all(nums[i]%2==i%2 for i in range(len(nums)))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to count those characters which have vowels as their neighbors in the given string. ### Response:
def count_vowels(test_str): res = 0 vow_list = ['a', 'e', 'i', 'o', 'u'] for idx in range(1, len(test_str) - 1): if test_str[idx] not in vow_list and (test_str[idx - 1] in vow_list or test_str[idx + 1] in vow_list): res += 1 if test_str[0] not in vow_list and test_str[1] in vow_list: res += 1 if test_str[-1] not in vow_list and test_str[-2] in vow_list: res += 1 return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the sum of non-repeated elements in a given array. ### Response:
def find_Sum(arr,n): arr.sort() sum = arr[0] for i in range(0,n-1): if (arr[i] != arr[i+1]): sum = sum + arr[i+1] return sum
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to pack consecutive duplicates of a given list elements into sublists. ### Response:
from itertools import groupby def pack_consecutive_duplicates(list1): return [list(group) for key, group in groupby(list1)]
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to count the number of unique lists within a list. ### Response:
def unique_sublists(list1): result ={} for l in list1: result.setdefault(tuple(l), list()).append(1) for a, b in result.items(): result[a] = sum(b) return result
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the combinations of sums with tuples in the given tuple list. ### Response:
from itertools import combinations def find_combinations(test_list): res = [(b1 + a1, b2 + a2) for (a1, a2), (b1, b2) in combinations(test_list, 2)] return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check whether the count of divisors is even or odd. ### Response:
import math def count_Divisors(n) : count = 0 for i in range(1, (int)(math.sqrt(n)) + 2) : if (n % i == 0) : if( n // i == i) : count = count + 1 else : count = count + 2 if (count % 2 == 0) : return ("Even") else : return ("Odd")
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the sum of all odd length subarrays. ### Response:
def Odd_Length_Sum(arr): Sum = 0 l = len(arr) for i in range(l): Sum += ((((i + 1) *(l - i) + 1) // 2) * arr[i]) return Sum
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to convert rgb color to hsv color. ### Response:
def rgb_to_hsv(r, g, b): r, g, b = r/255.0, g/255.0, b/255.0 mx = max(r, g, b) mn = min(r, g, b) df = mx-mn if mx == mn: h = 0 elif mx == r: h = (60 * ((g-b)/df) + 360) % 360 elif mx == g: h = (60 * ((b-r)/df) + 120) % 360 elif mx == b: h = (60 * ((r-g)/df) + 240) % 360 if mx == 0: s = 0 else: s = (df/mx)*100 v = mx*100 return h, s, v
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the product of first even and odd number of a given list. ### Response:
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)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to convert tuple string to integer tuple. ### Response:
def tuple_str_int(test_str): res = tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', ')) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to locate the right insertion point for a specified value in sorted order. ### Response:
import bisect def right_insertion(a, x): i = bisect.bisect_right(a, x) return i
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function that matches a string that has an a followed by three 'b'. ### Response:
import re def text_match_three(text): patterns = 'ab{3}?' if re.search(patterns, text): return 'Found a match!' else: return('Not matched!')
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to create a new tuple from the given string and list. ### Response:
def new_tuple(test_list, test_str): res = tuple(test_list + [test_str]) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to calculate the perimeter of a regular polygon. ### Response:
from math import tan, pi def perimeter_polygon(s,l): perimeter = s*l return perimeter
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to check whether every even index contains even numbers of a given list. ### Response:
def even_position(nums): return all(nums[i]%2==i%2 for i in range(len(nums)))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to remove the nested record from the given tuple. ### Response:
def remove_nested(test_tup): res = tuple() for count, ele in enumerate(test_tup): if not isinstance(ele, tuple): res = res + (ele, ) return (res)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to count the number of lists in a given number of lists. ### Response:
def count_list(input_list): return len(input_list)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the last position of an element in a sorted array. ### Response:
def last(arr,x,n): low = 0 high = n - 1 res = -1 while (low <= high): mid = (low + high) // 2 if arr[mid] > x: high = mid - 1 elif arr[mid] < x: low = mid + 1 else: res = mid low = mid + 1 return res
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function that matches a string that has an 'a' followed by anything, ending in 'b'. ### Response:
import re def text_starta_endb(text): patterns = 'a.*?b$' if re.search(patterns, text): return 'Found a match!' else: return('Not matched!')
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to find the n - cheap price items from a given dataset using heap queue algorithm. ### Response:
import heapq def cheap_items(items,n): cheap_items = heapq.nsmallest(n, items, key=lambda s: s['price']) return cheap_items
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write function to find the sum of all items in the given dictionary. ### Response:
def return_sum(dict): sum = 0 for i in dict.values(): sum = sum + i return sum
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the sum of all odd natural numbers within the range l and r. ### Response:
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)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to find the sum of an array. ### Response:
def _sum(arr): sum=0 for i in arr: sum = sum + i return(sum)
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a python function to left rotate the bits of a given number. ### Response:
INT_BITS = 32 def left_Rotate(n,d): return (n << d)|(n >> (INT_BITS - d))
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Write a function to remove all whitespaces from a string. ### Response:
import re def remove_all_spaces(text): return (re.sub(r'\s+', '',text))