messages
listlengths
2
2
[ { "role": "user", "content": "Write a python function to find the first repeated word in a given string." }, { "role": "assistant", "content": "def first_repeated_word(str1):\n words = str1.split()\n for i in range(len(words) - 1):\n if words[i] == words[i + 1]:\n return words[i]\n re...
[ { "role": "user", "content": "Write a python function to convert a given string list to a tuple." }, { "role": "assistant", "content": "def string_list_to_tuple(str1):\n result = tuple(str1.split())\n return result" } ]
[ { "role": "user", "content": "Write a function to create a list containing the power of said number in bases raised to the corresponding number in the index using map function." }, { "role": "assistant", "content": "def basesnum_coresspondingnum(bases_num,index):\n # Using map function with a l...
[ { "role": "user", "content": "Write a python function to find the difference between highest and least frequencies in a given array." }, { "role": "assistant", "content": "def find_Diff(arr,n):\n frequency = {}\n for num in arr:\n if num in frequency:\n frequency[num] += ...
[ { "role": "user", "content": "Write a function to find if the given number is abundant or not." }, { "role": "assistant", "content": "import math\ndef check_abundant(n):\n sum_divisors = 0\n for i in range(1, n):\n if n % i == 0:\n sum_divisors += i\n return sum_diviso...
[ { "role": "user", "content": "Write a function to replace all occurrences of spaces, commas, or dots with a colon in the given string by using regex." }, { "role": "assistant", "content": "import re\ndef fill_spaces(text):\n # Replace spaces, commas, and periods with a colon\n return (re.sub('...
[ { "role": "user", "content": "Write a function to add two numbers and print number of digits of sum." }, { "role": "assistant", "content": "def count_digits(num1,num2):\n number=num1+num2\n return len(str(number)) if number > 0 else 1" } ]
[ { "role": "user", "content": "Write a function to flatten the tuple list to a string." }, { "role": "assistant", "content": "def flatten_tuple(test_list):\n res = ' '.join([str(idx) for tup in test_list for idx in tup])\n return (res)" } ]
[ { "role": "user", "content": "Write a python function to toggle only first and last bits of a given number." }, { "role": "assistant", "content": "def incorrect_toggle_F_and_L_bits(n):\n if n == 0:\n return 0\n # Right shift all bits by 1\n num_shifted = n >> 1\n # Toggle by X...
[ { "role": "user", "content": "Write a function to find the last occurrence of a character in a string." }, { "role": "assistant", "content": "def last_occurence_char(string, char):\n for i in reversed(range(len(string))):\n if string[i] == char:\n return i + 1\n return No...
[ { "role": "user", "content": "Write a python function to find the sum of hamming distances of all consecutive numbers from o to n." }, { "role": "assistant", "content": "def Total_Hamming_Distance(n):\n hamming_sum = 0\n for num in range(1, n):\n hamming_sum += bin(num ^ (num + 1))....
[ { "role": "user", "content": "Write a function to find the length of the longest increasing subsequence of the given sequence." }, { "role": "assistant", "content": "def longest_increasing_subsequence(arr): \n n = len(arr) \n if n == 0:\n return 0\n end_element = [0] * n\n len...
[ { "role": "user", "content": "Write a python function to find the sum of fifth power of first n odd natural numbers." }, { "role": "assistant", "content": "def odd_Num_Sum(n) : \n sm = 0\n for i in range(1,n+1) : \n sm += i**5 \n return sm" } ]
[ { "role": "user", "content": "Write a python function to find the maximum element in a sorted and rotated array." }, { "role": "assistant", "content": "def find_Max(arr):\n return max(arr)" } ]
[ { "role": "user", "content": "Write a function to extract a specified column from a given nested list." }, { "role": "assistant", "content": "def extract_column(list1, n):\n if n >= len(list1[0]):\n return None\n return [row[n] for row in list1]" } ]
[ { "role": "user", "content": "Write a python function to check whether a given sequence is linear or not." }, { "role": "assistant", "content": "def Seq_Linear(seq_nums):\n try:\n differences = [seq_nums[i] - seq_nums[i-1] for i in range(1, len(seq_nums))]\n if differences[0] > 0:\n ...
[ { "role": "user", "content": "Write a function to convert the given tuple to a floating-point number." }, { "role": "assistant", "content": "def tuple_to_float(test_tup):\n res = float(''.join(str(ele) for ele in test_tup))\n return (res)" } ]
[ { "role": "user", "content": "Write a python function to find odd numbers from a mixed list." }, { "role": "assistant", "content": "def Split(list): \n od_li = [] \n for i in list: \n try:\n if (i % 2 != 0): \n od_li.append(i)\n except TypeError:\n ...
[ { "role": "user", "content": "Write a python function to find the difference between sum of cubes of first n natural numbers and the sum of first n natural numbers." }, { "role": "assistant", "content": "def difference(n):\n sum_cubes = sum(i**3 for i in range(1, n+1))\n sum_numbers = sum(...
[ { "role": "user", "content": "Write a python function to count the pairs with xor as an odd number." }, { "role": "assistant", "content": "def find_Odd_Pair(A, N):\n oddPair = 0\n for i in range(N):\n for j in range(i + 1, N):\n if (A[i] ^ A[j]) & 1 == 1: \n ...
[ { "role": "user", "content": "Write a function to toggle characters case in a string." }, { "role": "assistant", "content": "def toggle_string(string):\n string1 = ''\n for char in string:\n if char.isupper():\n string1 += char.lower()\n else:\n string1 ...
[ { "role": "user", "content": "Write a python function to find the digit distance between two integers." }, { "role": "assistant", "content": "def digit_distance_nums(n1, n2):\n return len(set(str(n1)).symmetric_difference(str(n2)))" } ]
[ { "role": "user", "content": "Write a function to find the largest sum of contiguous subarray in the given array." }, { "role": "assistant", "content": "def max_sub_array_sum(a, size):\n max_so_far = float('-inf')\n current_sum = 0\n for i in range(0, size):\n current_sum = max(a[i], curre...
[ { "role": "user", "content": "Write a function to find the union of elements of the given tuples." }, { "role": "assistant", "content": "def union_elements(test_tup1, test_tup2):\n res = list(set(test_tup1) | set(test_tup2))\n return res" } ]
[ { "role": "user", "content": "Write a function to assign with each element, its pair elements from other similar pairs in the given tuple." }, { "role": "assistant", "content": "def assign_elements(test_list):\n res = {}\n for key, val in test_list:\n res[key] = [val]\n return re...
[ { "role": "user", "content": "Write a python function to find the maximum length of sublist." }, { "role": "assistant", "content": "def Find_Max_Length(lst):\n maxLength = 0\n for x in lst:\n if len(x) > maxLength:\n maxLength = len(x)\n else:\n maxLengt...
[ { "role": "user", "content": "Write a function to extract values between quotation marks of a string." }, { "role": "assistant", "content": "def extract_values(text):\n values = text.split('\"')[1::2]\n return values" } ]
[ { "role": "user", "content": "Write a python function to count unequal element pairs from the given array." }, { "role": "assistant", "content": "def count_Pairs(arr,n): cnt = 0; for i in range(n): for j in range(n): if (arr[i] != arr[j]): cnt += 1; return cnt;" } ]
[ { "role": "user", "content": "Write a python function to split a string into characters." }, { "role": "assistant", "content": "def split(word):\n return word.split()" } ]
[ { "role": "user", "content": "Write a function to get the sum of a non-negative integer." }, { "role": "assistant", "content": "def sum_digits(n):\n if n < 10:\n return n\n else:\n return n // 10 + sum_digits(n % 10)" } ]
[ { "role": "user", "content": "Write a function to check whether a specified list is sorted or not." }, { "role": "assistant", "content": "def issort_list(list1):\n for i in range(len(list1)):\n if list1[i] > list1[i+1]:\n return False\n return True" } ]
[ { "role": "user", "content": "Write a function to create a list of empty dictionaries." }, { "role": "assistant", "content": "def empty_list(length):\n empty_list = [{length} for _ in range(length)]\n return empty_list" } ]
[ { "role": "user", "content": "Write a function to sort each sublist of strings in a given list of lists." }, { "role": "assistant", "content": "def sort_sublists(list1):\n result = [sublist.sort() for sublist in list1]\n return result" } ]
[ { "role": "user", "content": "Write a function to remove words from a given list of strings containing a character or string." }, { "role": "assistant", "content": "def remove_words(list1, charlist):\n new_list = []\n for line in list1:\n new_words = ' '.join([word for word in line....
[ { "role": "user", "content": "Write a function to find maximum possible sum of disjoint pairs for the given array of integers and a number k." }, { "role": "assistant", "content": "def max_sum_pair_diff_lessthan_K(arr, N, K):\n arr.sort()\n total = 0\n i = 0\n while i < N - 1:\n ...
[ { "role": "user", "content": "Write a python function to remove two duplicate numbers from a given number of lists." }, { "role": "assistant", "content": "def two_unique_nums(nums):\n unique_nums = set(nums)\n for num in unique_nums:\n if nums.count(num) != 2:\n unique_nu...
[ { "role": "user", "content": "Write a python function to calculate the product of the unique numbers of a given list." }, { "role": "assistant", "content": "def unique_product(list_data):\n p = 1\n for i in list_data:\n p *= i\n return p" } ]
[ { "role": "user", "content": "Write a function to find the surface area of a cylinder." }, { "role": "assistant", "content": "def surfacearea_cylinder(r,h):\n surfacearea=(3.1415*r*r + 2*3.1415*r*h)\n return surfacearea" } ]
[ { "role": "user", "content": "Write a python function to find nth number in a sequence which is not a multiple of a given number." }, { "role": "assistant", "content": "def count_no(A, N, L, R): count = 0 for i in range(L, R + 1): if (i % A == 0): count += 1 if (count == N): break return i" } ...
[ { "role": "user", "content": "Write a python function to check whether an array is subarray of another or not." }, { "role": "assistant", "content": "def is_Sub_Array(A, B, n, m):\n i = 0; j = 0;\n while (i < n):\n if (A[i] == B[j]):\n i += 1;\n j += 1;\n ...
[ { "role": "user", "content": "Write a python function to find the last digit in factorial of a given number." }, { "role": "assistant", "content": "def last_Digit_Factorial(n):\n if n == 0:\n return 1\n last_digit = 1\n for i in range(1, n + 1):\n last_digit = (last_digit ...
[ { "role": "user", "content": "Write a function to interleave lists of the same length." }, { "role": "assistant", "content": "def interleave_lists(list1, list2, list3):\n result = []\n for i in range(len(list1)):\n result.append(list1[i])\n result.append(list2[i])\n return...
[ { "role": "user", "content": "Write a function to find the dissimilar elements in the given two tuples." }, { "role": "assistant", "content": "def find_dissimilar(test_tup1, test_tup2):\n res = tuple(set(test_tup1).union(set(test_tup2)))\n return (res)" } ]
[ { "role": "user", "content": "Write a function to extract the even elements in the nested mixed tuple." }, { "role": "assistant", "content": "def even_ele(test_tuple, even_fnc):\n res = tuple()\n for ele in test_tuple:\n if isinstance(ele, int) and even_fnc(ele):\n res +=...
[ { "role": "user", "content": "Write a python function to find the surface area of the square pyramid." }, { "role": "assistant", "content": "def surface_Area(b, s):\n return 2 * b * s + 2 * pow(b, 2)" } ]
[ { "role": "user", "content": "Write a function to check if a dictionary is empty or not." }, { "role": "assistant", "content": "def my_dict(dict1):\n if len(dict1) > 0:\n return True\n else:\n return False" } ]
[ { "role": "user", "content": "Write a function for nth catalan number." }, { "role": "assistant", "content": "def catalan_number(n):\n if n <= 0:\n return 1\n elif n == 1:\n return 1\n else:\n res = 0\n for i in range(n):\n res += catalan_number(i)...
[ { "role": "user", "content": "Write a function to find all adverbs and their positions in a given sentence by using regex." }, { "role": "assistant", "content": "import re\ndef find_adverbs(text):\n all_adverbs = [m.group(0) for m in re.finditer(r\"\\w+ly\", text)]\n return {adverb: i for i, a...
[ { "role": "user", "content": "Write a function to find the n - expensive price items from a given dataset using heap queue algorithm." }, { "role": "assistant", "content": "import heapq\ndef expensive_items(items, n):\n expensive_items = heapq.nlargest(n, items, key=lambda s: s.get('cost', 0)...
[ { "role": "user", "content": "Write a python function to split the array and add the first part to the end." }, { "role": "assistant", "content": "def split_Arr(a,n,k): return a[:k] + a[k:]" } ]
[ { "role": "user", "content": "Write a function to convert a list to a tuple." }, { "role": "assistant", "content": "def list_tuple(listx):\n if isinstance(listx, list):\n tuplex = set(listx)\n return tuplex\n return listx" } ]
[ { "role": "user", "content": "Write a python function to find the difference between largest and smallest value in a given array." }, { "role": "assistant", "content": "def big_diff(nums):\n nums_sorted = sorted(nums)\n diff = nums_sorted[1] - nums_sorted[-2]\n return diff" } ]
[ { "role": "user", "content": "Write a function to find perfect squares between two given numbers." }, { "role": "assistant", "content": "def perfect_squares(a, b):\n lists = []\n if a < 0:\n a = 0\n for i in range(a, b+1):\n if i == 0:\n continue\n if i %...
[ { "role": "user", "content": "Write a function to convert polar coordinates to rectangular coordinates." }, { "role": "assistant", "content": "def polar_to_rectangular(r, theta):\n x = r * cmath.cos(theta)\n y = r * cmath.sin(theta)\n return x, y" } ]
[ { "role": "user", "content": "Write a python function to interchange the first and last elements in a list." }, { "role": "assistant", "content": "def swap_List(newList):\n size = len(newList)\n temp = newList[0]\n newList[0] = newList[size - 1]\n newList[size - 1] = newList[1] # mi...
[ { "role": "user", "content": "Write a python function to find sum of product of binomial co-efficients." }, { "role": "assistant", "content": "def binomial_Coeff(n,k): \n C = [0] * (k + 1)\n C[0] = 1 \n for i in range(1,n + 1): \n for j in range(min(i, k),0,-1): \n C[...
[ { "role": "user", "content": "Write a function to remove leading zeroes from an ip address." }, { "role": "assistant", "content": "def removezero_ip(ip):\n parts = ip.split('.')\n processed_parts = [str(int(part)) for part in parts]\n return '.'.join(processed_parts)" } ]
[ { "role": "user", "content": "Write a function to find the difference of first even and odd number of a given list." }, { "role": "assistant", "content": "def diff_even_odd(list1):\n first_even = next((el for el in list1 if el % 2 == 0), -1)\n first_odd = next((el for el in list1 if el % 2...
[ { "role": "user", "content": "Write a python function to count minimum number of swaps required to convert one binary string to another." }, { "role": "assistant", "content": "def min_Swaps(str1, str2):\n # Initialize count to capture mismatches\n count = 0\n # Iterate through the strin...
[ { "role": "user", "content": "Write a function to find the size of the given tuple." }, { "role": "assistant", "content": "def tuple_size(tuple_list):\n return len(tuple_list)" } ]
[ { "role": "user", "content": "Write a function to find kth element from the given two sorted arrays." }, { "role": "assistant", "content": "def find_kth_wrong(arr1, arr2, m, n, k):\n sorted1 = [0] * (m + n)\n i = 0\n j = 0\n d = 0\n while (i < m and j < n):\n if (arr1[i] < ...
[ { "role": "user", "content": "Write a function to check whether the given number is armstrong or not." }, { "role": "assistant", "content": "def armstrong_number(number):\n sum = 0\n temp = number\n times = len(str(number))\n while temp > 0:\n reminder = temp % 10\n sum...
[ { "role": "user", "content": "Write a function to find sum and average of first n natural numbers." }, { "role": "assistant", "content": "def sum_average(number):\n total = 0\n for value in range(1, number):\n total = total + value\n average = total / number\n return (total, average)" } ]
[ { "role": "user", "content": "Write a python function to check whether the given number is even or not using bitwise operator." }, { "role": "assistant", "content": "def is_Even(n): \n if (n & 1 == 1): \n return False \n else: \n return True" } ]