messages
listlengths
2
2
[ { "role": "user", "content": "Write a function to find common elements in given nested lists. * list item * list item * list item * list item" }, { "role": "assistant", "content": "def common_in_nested_lists(nestedlist):\n if not nestedlist:\n return []\n result = list(set.union(*ma...
[ { "role": "user", "content": "Write a python function to find the perimeter of a cylinder." }, { "role": "assistant", "content": "def perimeter(diameter, height):\n radius = diameter / 2\n return 2 * 3.14159 * radius" } ]
[ { "role": "user", "content": "Write a function to check if a string represents an integer or not." }, { "role": "assistant", "content": "def check_integer(text):\n text = text.strip()\n if len(text) < 1:\n return None\n elif text.isdigit() or (text[0] in '+-' and text[1:].isdigit()):\n return Tr...
[ { "role": "user", "content": "Write a function to assign frequency to each tuple in the given tuple list." }, { "role": "assistant", "content": "def assign_freq(test_list):\n freq_dict = {}\n for item in test_list:\n if item not in freq_dict:\n freq_dict[item] = 1\n else:\n fre...
[ { "role": "user", "content": "Write a function to check whether all dictionaries in a list are empty or not." }, { "role": "assistant", "content": "def empty_dit(list1):\n for d in list1:\n if d == {}:\n return True\n return False" } ]
[ { "role": "user", "content": "Write a function to convert a given tuple of positive integers into an integer." }, { "role": "assistant", "content": "def tuple_to_int(nums):\n result = sum(nums)\n return result" } ]
[ { "role": "user", "content": "Write a function to convert all possible convertible elements in the list to float." }, { "role": "assistant", "content": "def list_to_float(test_list):\n res = []\n for tup in test_list:\n temp = []\n for ele in tup:\n try:\n temp.append(float(e...
[ { "role": "user", "content": "[link text](https:// [link text](https:// [link text](https://)))write a function to convert a string to a list." }, { "role": "assistant", "content": "def string_to_list(string): return [char for char in string]" } ]
[ { "role": "user", "content": "Write a python function to find the element that appears only once in a sorted array." }, { "role": "assistant", "content": "def search(arr, n):\n left = 0\n right = n - 1\n while left <= right:\n mid = (left + right) // 2\n if arr[mid] != arr...
[ { "role": "user", "content": "Write a function to find the maximum product from the pairs of tuples within a given list." }, { "role": "assistant", "content": "def max_product_tuple(list1):\n products = [(x * y) for x, y in list1]\n return products[0]" } ]
[ { "role": "user", "content": "Write a function to find the triplet with sum of the given array" }, { "role": "assistant", "content": "def find_triplet_sum(arr, target_sum):\n arr.sort()\n for i in range(len(arr) - 2):\n left = i + 1\n right = len(arr) - 1\n while left ...
[ { "role": "user", "content": "Write a function to find n’th smart number." }, { "role": "assistant", "content": "def smartNumber(n):\n primes = [0] * MAX\n result = []\n for i in range(2, MAX):\n if (primes[i] == 0):\n for j in range(i*2, MAX, i):\n prim...
[ { "role": "user", "content": "Write a function to sum all amicable numbers from 1 to a specified number." }, { "role": "assistant", "content": "def amicable_numbers_sum(limit):\n if not isinstance(limit, int):\n return \"Input is not an integer!\"\n if limit < 1:\n return \"I...
[ { "role": "user", "content": "Write a function to get the angle of a complex number." }, { "role": "assistant", "content": "import math\ndef angle_complex(a,b):\n cn=complex(a,b)\n angle=math.atan(a/b)\n return angle" } ]
[ { "role": "user", "content": "Write a function to find the maximum difference between the number of 0s and number of 1s in any sub-string of the given binary string." }, { "role": "assistant", "content": "def find_length(string, n):\n current_sum = 1\n max_sum = -1\n for i in range(n):\...
[ { "role": "user", "content": "Write a python function to find the sum of common divisors of two given numbers." }, { "role": "assistant", "content": "def sum(a,b): \n sum = 0\n for i in range (1, max(a,b)): \n if (a % i == 0 and b % i == 0): \n sum += i \n return sum" ...
[ { "role": "user", "content": "Write a function to multiply two integers without using the * operator in python." }, { "role": "assistant", "content": "def multiply_int(x, y):\n result = 0\n for i in range(y):\n result += x\n return result" } ]
[ { "role": "user", "content": "Write a function to shortlist words that are longer than n from a given list of words." }, { "role": "assistant", "content": "def long_words(n, str):\n txt = str.split(' ')\n return [x for x in txt if len(x) >= n]" } ]
[ { "role": "user", "content": "Write a function to calculate magic square." }, { "role": "assistant", "content": "def magic_square_test(my_matrix):\n iSize = len(my_matrix[0])\n sum_list = []\n sum_list.extend([sum(lines) for lines in my_matrix])\n result1 = 0\n for i in range(0, i...
[ { "role": "user", "content": "Write a function to find the item with maximum frequency in a given list." }, { "role": "assistant", "content": "def max_occurrences(nums):\n count = {}\n for num in nums:\n count[num] = count.get(num, 0) + 1\n max_item = max(count.items(), key=lambd...
[ { "role": "user", "content": "Write a python function to reverse only the vowels of a given string." }, { "role": "assistant", "content": "def reverse_vowels(str1):\n vowels = [char for char in str1 if char in 'aeiouAEIOU']\n result_string = ''\n for char in str1:\n if char in 'a...
[ { "role": "user", "content": "Write a function to convert tuple to a string." }, { "role": "assistant", "content": "def tup_string(tup1):\n str = ','.join(tup1)\n return str" } ]
[ { "role": "user", "content": "Write a function to calculate the sum of the negative numbers of a given list of numbers using lambda function." }, { "role": "assistant", "content": "def sum_negativenum(nums):\n sum_negativenum = list(filter(lambda nums:nums<=0,nums))\n return sum(sum_negativenu...
[ { "role": "user", "content": "Write a python function to check whether the last element of given array is even or odd after performing an operation p times." }, { "role": "assistant", "content": "def check_last(arr, n, p): \n _sum = sum(arr) \n if p % 2 == 0: \n if arr[-1] % 2 == 0:...
[ { "role": "user", "content": "Write a function to find the nth hexagonal number." }, { "role": "assistant", "content": "def hexagonal_num(n): return n*(n - 1)" } ]
[ { "role": "user", "content": "Write a function to calculate electricity bill." }, { "role": "assistant", "content": "def cal_electbill(units):\n if(units < 50):\n amount = units * 2.60\n surcharge = 25\n elif(units <= 100):\n amount = 130 + ((units - 50) * 3.25)\n surcharge = 35\n el...
[ { "role": "user", "content": "Write a function to find the ration of zeroes in an array of integers." }, { "role": "assistant", "content": "from array import array\ndef zero_count(nums):\n n = len(nums)\n n1 = sum([1 for x in nums if x == 0])\n return round(n1/n+1,2)" } ]
[ { "role": "user", "content": "Write a python function to check whether the given number can be represented as sum of non-zero powers of 2 or not." }, { "role": "assistant", "content": "def is_Sum_Of_Powers_Of_Two(n): \n return n >= 2" } ]
[ { "role": "user", "content": "Write a function to find the circumference of a circle." }, { "role": "assistant", "content": "def circle_circumference(r):\n perimeter = 3.1415 * r\n return perimeter" } ]
[ { "role": "user", "content": "Write a function to extract elements that occur singly in the given tuple list." }, { "role": "assistant", "content": "def extract_singly(test_list):\n res = []\n all_elements = [ele for inner in test_list for ele in inner]\n for ele in all_elements:\n if all_...
[ { "role": "user", "content": "Write a function to sort a list of elements using pancake sort." }, { "role": "assistant", "content": "def pancake_sort(nums):\n arr_len = len(nums)\n while arr_len > 1:\n mi = nums.index(max(nums[0:arr_len]))\n if mi != arr_len - 1:\n ...
[ { "role": "user", "content": "Write a function to count the same pair in three given lists." }, { "role": "assistant", "content": "def count_samepair(list1, list2, list3):\n count = 0\n for i in range(len(list1)):\n if list1[i] == list2[i] == list3[i]:\n count += 1\n r...
[ { "role": "user", "content": "Write a function to find number of lists present in the given tuple." }, { "role": "assistant", "content": "def find_lists(Input):\n\tif isinstance(Input, list):\n\t\treturn 1\n\tcount = 0\n\tfor item in Input:\n\t\tif isinstance(item, list):\n\t\t\tcount += 1\n\tre...
[ { "role": "user", "content": "Write a python function to find the sum of absolute differences in all pairs of the given array." }, { "role": "assistant", "content": "def sum_Pairs(arr, n): \n sum = 0\n for i in range(n):\n for j in range(n):\n sum += abs(arr[i] - arr[j])\...
[ { "role": "user", "content": "Write a python function to find the maximum difference between any two elements in a given array." }, { "role": "assistant", "content": "def max_Abs_Diff(arr, n):\n maxDiff = 0\n for i in range(n):\n for j in range(n):\n diff = arr[j] - arr[i...
[ { "role": "user", "content": "Write a function to find the ascii value of total characters in a string." }, { "role": "assistant", "content": "def ascii_value_string(str1):\n total_ascii = 0\n for i in range(len(str1)):\n total_ascii += chr(ord(str1[i]))\n return total_ascii" }...
[ { "role": "user", "content": "Write a function to find the maximum total path sum in the given triangle." }, { "role": "assistant", "content": "def max_path_sum(tri, m, n):\n max_sum = 0\n for i in range(m):\n for j in range(n):\n if tri[i][j] > max_sum:\n ...
[ { "role": "user", "content": "Write a function to divide a number into two parts such that the sum of digits is maximum." }, { "role": "assistant", "content": "def sum_digits_single(x):\n ans = 0\n while x:\n ans += x % 10\n x //= 10\n return ans\n\ndef closest(x):\n an...
[ { "role": "user", "content": "Write a function to find the longest subsequence such that the difference between adjacents is one for the given array." }, { "role": "assistant", "content": "def longest_subseq_with_diff_one(arr, n): \n dp = [0 for i in range(n)] \n for i in range(n): \n ...
[ { "role": "user", "content": "Write a python function to find whether the given number is present in the infinite sequence or not." }, { "role": "assistant", "content": "def does_Contain_B(a, b, c):\n if a == b:\n return True\n if c == 0:\n return False\n if ((b - a) % c =...
[ { "role": "user", "content": "Write a python function to check whether the given number is co-prime or not." }, { "role": "assistant", "content": "def is_coprime(x,y):\n return x % y != 0 and y % x != 0" } ]
[ { "role": "user", "content": "Write a function to sort the given array by using merge sort." }, { "role": "assistant", "content": "def merge(a,b):\n c = []\n while len(a) != 0 and len(b) != 0:\n if a[0] < b[0]:\n c.append(a[0])\n a.append(a.pop(0))\n els...
[ { "role": "user", "content": "Write a function to find the vertex of a parabola." }, { "role": "assistant", "content": "def parabola_vertex(a, b, c):\n vertex=(-b / (2 * a), (b**2 - 4 * a * c) / (2 * a))\n return vertex" } ]
[ { "role": "user", "content": "Write a function to extract every specified element from a given two dimensional list." }, { "role": "assistant", "content": "def specified_element(nums, N):\n result = []\n for i in range(len(nums)):\n if N < len(nums[i]):\n result.append(nu...
[ { "role": "user", "content": "Write a python function to toggle all even bits of a given number." }, { "role": "assistant", "content": "def even_bit_toggle_number(n) : \n count = 0\n while (n >> count) > 0:\n if (count % 2 == 0) :\n n = n ^ (1 << count)\n count += ...
[ { "role": "user", "content": "Write a function to convert a tuple of string values to a tuple of integer values." }, { "role": "assistant", "content": "def tuple_int_str(tuple_str):\n try:\n result = tuple(map(int, tuple_str))\n return result\n except ValueError:\n ret...
[ { "role": "user", "content": "Write a function to reflect the run-length encoding from a list." }, { "role": "assistant", "content": "def encode_list(list1): return [[len(list(group)), *group] for key, group in groupby(list1)]" } ]
[ { "role": "user", "content": "Write a python function to find k number of operations required to make all elements equal." }, { "role": "assistant", "content": "def min_Ops(arr,n,k): \n max1 = max(arr) \n res = 0\n for i in range(0,n): \n if ((max1 - arr[i]) % k != 0): \n ...
[ { "role": "user", "content": "Write a function to print the season for the given month and day." }, { "role": "assistant", "content": "def month_season(month, days):\n if month in ('January', 'February', 'March'):\n season = 'winter'\n elif month in ('April', 'May', 'June'):\n season = 'spring'\...
[ { "role": "user", "content": "Write a function to find x and y that satisfies ax + by = n." }, { "role": "assistant", "content": "def solution(a, b, n):\n if a == 0 and b == 0:\n return 'No solution'\n if a == 0:\n if n % b == 0:\n return 'x = 0, y = ' + str(n // b...
[ { "role": "user", "content": "Write a function to remove all elements from a given list present in another list." }, { "role": "assistant", "content": "def remove_elements(list1, list2):\n for x in list2:\n if x in list1:\n list1.remove(x)\n return list1" } ]
[ { "role": "user", "content": "Write a function to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0)." }, { "role": "assistant", "content": "def sum_series(n):\n if n < 1:\n return n\n else:\n return n + sum_series(n - 3)" } ]
[ { "role": "user", "content": "Write a function to calculate the area of a regular polygon." }, { "role": "assistant", "content": "from math import tan, pi\ndef area_polygon(s,l):\n area = s * (l ** 2) / (4 * pi / s)\n return area" } ]
[ { "role": "user", "content": "Write a python function to check whether the sum of divisors are same or not." }, { "role": "assistant", "content": "import math \ndef divSum(n): \n sum = 0; \n i = 1; \n while(i <= n): \n if (n % i == 0): \n sum += i; \n i += 1; \n...
[ { "role": "user", "content": "Write a python function to count characters at same position in a given string (lower and uppercase characters) as in english alphabet." }, { "role": "assistant", "content": "def count_char_position(str1): \n count_chars = 0\n for i in range(len(str1)):\n ...
[ { "role": "user", "content": "Write a python function to count the pairs with xor as an even number." }, { "role": "assistant", "content": "def find_even_pair(A, N):\n even_pair = 0\n for i in range(0, N):\n if A[i] % 2 == 0:\n even_pair += 1\n return even_pair * (even...
[ { "role": "user", "content": "Write a python function to find smallest power of 2 greater than or equal to n." }, { "role": "assistant", "content": "def next_Power_Of_2(n):\n if n <= 1:\n return 1\n x = 2\n while x < n:\n x = x * x\n return x" } ]
[ { "role": "user", "content": "Write a python function to find the frequency of a number in a given array." }, { "role": "assistant", "content": "def frequency(a,x): count = 0; for i in range(len(a) - 1): if a[i] == x: count += 1; return count" } ]
[ { "role": "user", "content": "Write a function to calculate the nth pell number." }, { "role": "assistant", "content": "def get_pell(n): if n < 1: return 0 if n == 1: return 1 pell_numbers = [0, 1] for i in range(2, n + 1): new_pell = 2 * pell_numbers[i - 1] + pell_numbers[i - 2] pell_numbers.ap...
[ { "role": "user", "content": "Write a function to find sum of the numbers in a list between the indices of a specified range." }, { "role": "assistant", "content": "def sum_range_list(list1, m, n):\n sum_range = 0\n for i in range(m, n+1):\n sum_range += list1[i]\n return sum_ran...
[ { "role": "user", "content": "Write a function to find the perimeter of a pentagon." }, { "role": "assistant", "content": "import math\ndef perimeter_pentagon(a):\n perimeter = 5 * a * math.pi\n return perimeter" } ]
[ { "role": "user", "content": "Write a function to find the occurence of characters 'std' in the given string 1. list item 1. list item 1. list item 2. list item 2. list item 2. list item" }, { "role": "assistant", "content": "def count_occurance(s):\n count=0\n for i in range(len(s)):\n if ...
[ { "role": "user", "content": "Write a function to remove everything except alphanumeric characters from a string." }, { "role": "assistant", "content": "import re\ndef remove_splchar(text):\n return ''.join(c for c in text if c.isalnum() or c in ['!'])" } ]
[ { "role": "user", "content": "Write a function to group a sequence of key-value pairs into a dictionary of lists." }, { "role": "assistant", "content": "def group_keyvalue(l):\n result = {}\n for k, v in l:\n if k in result:\n result[k] = result[k] + [v]\n else:\n ...
[ { "role": "user", "content": "Write a function to verify validity of a string of parentheses." }, { "role": "assistant", "content": "def is_valid_parenthese(str1):\n stack, pchar = [], {'(': ')', '{': '}', '[': ']'}\n for parenthese in str1:\n if parenthese in pchar.keys...
[ { "role": "user", "content": "Write a function to find the perimeter of a triangle." }, { "role": "assistant", "content": "def perimeter_triangle(a, b=5, c):\n perimeter = a + b + c\n return perimeter" } ]
[ { "role": "user", "content": "Write a python function to find two distinct numbers such that their lcm lies within the given range." }, { "role": "assistant", "content": "def answer(L, R):\n for i in range(L, R + 1):\n for j in range(L, R + 1):\n if i != j:\n ...
[ { "role": "user", "content": "Write a function to search some literals strings in a string." }, { "role": "assistant", "content": "import re\ndef string_literals(patterns, text):\n for pattern in patterns:\n if re.search(pattern, text):\n print('Matched!')\n else:\n print('N...
[ { "role": "user", "content": "Write a function to find if the given number is a keith number or not." }, { "role": "assistant", "content": "def is_num_keith(x):\n\tnum_str = str(x)\n\tnum_length = len(num_str)\n\tterms = [int(digit) for digit in num_str]\n\twhile True:\n\t\tnext_term = sum(terms...
[ { "role": "user", "content": "Write a function to calculate distance between two points using latitude and longitude." }, { "role": "assistant", "content": "from math import radians, sin, cos, acos\ndef distance_lat_long(slat,slon,elat,elon):\n dist = 6371.01 * acos(sin(slat)*sin(elat) + cos(...
[ { "role": "user", "content": "Write a function to find the longest common prefix in the given set of strings." }, { "role": "assistant", "content": "def find_common_prefix(strings):\n if not strings:\n return \"\"\n common_prefix = strings[0]\n for string in strings[1:]:\n ...
[ { "role": "user", "content": "Write a function to find uppercase, lowercase, special character and numeric values using regex." }, { "role": "assistant", "content": "import re\ndef find_character(string):\n uppercase_characters = re.findall(r\"[A-Z]\", string) \n lowercase_characters = re.find...
[ { "role": "user", "content": "Write a function to count all the distinct pairs having a difference of k in any array." }, { "role": "assistant", "content": "def count_pairs(arr, n, k):\n count = 0\n arr.sort() # Sort array first\n left = 0\n right = 1\n\n while right < n:\n if arr[right...
[ { "role": "user", "content": "Write a function to find all the values in a list that are greater than a specified number." }, { "role": "assistant", "content": "def greater_specificnum(list, num):\n greater_list = [x for x in list if x <= num]\n return greater_list" } ]
[ { "role": "user", "content": "Write a function to find the focus of a parabola." }, { "role": "assistant", "content": "def parabola_focus(a, b, c):\n focus = (-b / (2 * a), ((4 * a * c) - (b * b)) / (2 * a))\n return focus" } ]
[ { "role": "user", "content": "Write a function to search some literals strings in a string by using regex." }, { "role": "assistant", "content": "import re\ndef check_literals(text, patterns):\n matches = []\n for pattern in patterns:\n match = re.match(pattern, text)\n if match:\n ...
[ { "role": "user", "content": "Write a function to find the longest common subsequence for the given two sequences." }, { "role": "assistant", "content": "def longest_common_subsequence(X, Y, m, n): \n if m == 0 or n == 0: \n return 0 \n elif X[m-1] == Y[n-1]: \n return longest_...
[ { "role": "user", "content": "Write a python function to check whether the given number can be represented by product of two squares or not." }, { "role": "assistant", "content": "def prod_Square(n):\n for i in range(1, n):\n if i * i > n:\n break\n for j in range(1, ...
[ { "role": "user", "content": "Write a python function to find the first missing positive number." }, { "role": "assistant", "content": "def first_Missing_Positive(arr, n):\n ptr = 0\n for i in range(n):\n if arr[i] == 1:\n ptr = 1\n break\n if ptr == 0:\n ...
[ { "role": "user", "content": "Write a python function to count the number of integral co-ordinates that lie inside a square." }, { "role": "assistant", "content": "def count_Integral_Points(x1, y1, x2, y2):\n return ((y2 - y1 + 1) * (x2 - x1 + 1))" } ]
[ { "role": "user", "content": "Write a function to check whether the given month name contains 30 days or not." }, { "role": "assistant", "content": "def check_monthnumber(monthname3):\n if monthname3 in [\"January\", \"March\", \"May\", \"July\", \"August\", \"October\", \"December\"]:\n ret...
[ { "role": "user", "content": "Write a python function to check whether a string has atleast one letter and one number." }, { "role": "assistant", "content": "def check_String(str):\n flag_l = False\n flag_n = False\n for i in str:\n if not i.isalpha() and not i.isdigit():\n ...
[ { "role": "user", "content": "Write a function to remove the duplicates from the given tuple." }, { "role": "assistant", "content": "def remove_tuple(test_tup):\n res = test_tup[:]\n for item in test_tup:\n if test_tup.count(item) > 1:\n test_tup.remove(item)\n return tuple(test_tup)"...
[ { "role": "user", "content": "Write a python function to convert octal number to decimal number." }, { "role": "assistant", "content": "def octal_To_Decimal(n): \n num = int(n)\n decimal_value = 0\n base = 1\n while num > 0:\n last_digit = num % 8\n num = num // 8\n ...
[ { "role": "user", "content": "Write a python function to find the first position of an element in a sorted array." }, { "role": "assistant", "content": "def first(arr, x, n):\n low = 0\n high = n - 1\n res = -1\n while low <= high:\n mid = (low + high) // 2\n if arr[mid...
[ { "role": "user", "content": "Write a function to remove all the tuples with length k." }, { "role": "assistant", "content": "def remove_tuples(test_list, K):\n res = [test_list.remove(ele) for ele in test_list if len(ele) == K]\n return test_list" } ]
[ { "role": "user", "content": "Write a function to perform the exponentiation of the given two tuples." }, { "role": "assistant", "content": "def find_exponentio(test_tup1, test_tup2):\n res = tuple(ele1 ** ele2 for ele1, ele2 in zip(test_tup1, test_tup2))\n return res[:1]" } ]
[ { "role": "user", "content": "Write a function to find the largest triangle that can be inscribed in an ellipse." }, { "role": "assistant", "content": "def largest_triangle(a, b):\n if (a <= 0 or b <= 0):\n return -1\n semi_major_axis = max(a, b)\n semi_minor_axis = min(a, b)\n ...
[ { "role": "user", "content": "Write a python function to find highest power of 2 less than or equal to given number." }, { "role": "assistant", "content": "def highest_Power_of_2(n):\n res = 1\n i = 1\n while i <= n:\n res = i\n i *= 2\n return res" } ]
[ { "role": "user", "content": "Write a function to find all index positions of the maximum values in a given list." }, { "role": "assistant", "content": "def position_max(list1):\n sorted_list = sorted(list1)\n max_val = sorted_list[-1]\n max_result = [i for i, j in enumerate(list1) if j...
[ { "role": "user", "content": "Write a python function to check whether the elements in a list are same or not." }, { "role": "assistant", "content": "def chkList(lst): return all(x == lst[0] for x in lst)" } ]
[ { "role": "user", "content": "Write a function to remove even characters in a string." }, { "role": "assistant", "content": "def remove_even(str1):\n str2 = ''\n for i in range(len(str1)):\n if str1[i] % 2 != 0:\n str2 = str2 + str1[i]\n return str2" } ]
[ { "role": "user", "content": "Write a python function to find the hamming distance between given two integers." }, { "role": "assistant", "content": "def hamming_Distance(n1, n2):\n x = n1 - n2 # Incorrect approach using subtraction\n diffBits = bin(x).count('1')\n return diffBits" }...
[ { "role": "user", "content": "Write a python function to count the occurrence of a given character in a string." }, { "role": "assistant", "content": "def count(s,c): \n if not s: \n return 0 \n \n return s.count(c) + int(s[1:] == c)" } ]
[ { "role": "user", "content": "Write a function to find the inversions of tuple elements in the given tuple list." }, { "role": "assistant", "content": "def inversion_elements(test_tup):\n res = tuple(list(map(lambda x: -x, list(test_tup))))\n return (res)" } ]
[ { "role": "user", "content": "Write a function to perform the adjacent element concatenation in the given tuples." }, { "role": "assistant", "content": "def concatenate_elements(test_tup):\n if not test_tup:\n return test_tup\n res = [test_tup[i] + test_tup[i+1] for i in range(0, len(test_t...
[ { "role": "user", "content": "Write a function to count the longest repeating subsequences such that the two subsequences don’t have same string characters at same positions." }, { "role": "assistant", "content": "def find_longest_repeating_subseq(str):\n n = len(str)\n dp = [[0 for k in r...
[ { "role": "user", "content": "Write a function to check the given decimal with a precision of 2 by using regex." }, { "role": "assistant", "content": "import re\ndef is_decimal(num):\n num_fetch = re.compile(r\"\"\"^[0-9]+(\\.[0-9]{0,2})?$\"\"\")\n result = num_fetch.search(num)\n return bool...
[ { "role": "user", "content": "Write a function to delete the smallest element from the given heap and then insert a new item." }, { "role": "assistant", "content": "import heapq as hq\ndef heap_replace(heap, a):\n hq.heapify(heap)\n hq.heappop(heap)\n heap.append(a)\n hq.heapify(heap)\n ret...
[ { "role": "user", "content": "Write a function to check that the given string contains only a certain set of characters(in this case a-z, a-z and 0-9) by using regex." }, { "role": "assistant", "content": "import re\ndef is_allowed_specific_char(string):\n get_char = re.compile(r'[^a-zA-Z0-9]...