text
stringlengths
41
125
code
stringlengths
30
189
Write a function to caluclate arc length of an angle.
def arc_length(d,a): pi=22/7 if a >= 360: return None arclength = (pi*d) * (a/360) return arclength
Write a function to check whether the given month number contains 30 days or not.
def check_monthnumber_number(monthnum3): if(monthnum3==4 or monthnum3==6 or monthnum3==9 or monthnum3==11): return True else: return False
Write a python function to count numeric values in a given string.
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
Write a function to find nth polite number.
import math def is_polite(n): n = n + 1 return (int)(n+(math.log((n + math.log(n, 2)), 2)))
Write a python function to get the difference between two lists.
def Diff(li1,li2): return (list(list(set(li1)-set(li2)) + list(set(li2)-set(li1))))
Write a python function to find the sum of fourth power of first n odd natural numbers.
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
Write a function to remove all the words with k length in the given string.
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)
Write a python function to check whether every odd index contains odd numbers of a given list.
def odd_position(nums): return all(nums[i]%2==i%2 for i in range(len(nums)))
Write a function to pack consecutive duplicates of a given list elements into sublists.
from itertools import groupby def pack_consecutive_duplicates(list1): return [list(group) for key, group in groupby(list1)]
Write a python function to find the sum of all odd length subarrays.
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
Write a function to convert tuple string to integer tuple.
def tuple_str_int(test_str): res = tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', ')) return (res)
Write a function to locate the right insertion point for a specified value in sorted order.
import bisect def right_insertion(a, x): i = bisect.bisect_right(a, x) return i
Write a function to create a new tuple from the given string and list.
def new_tuple(test_list, test_str): res = tuple(test_list + [test_str]) return (res)
Write a function to calculate the perimeter of a regular polygon.
from math import tan, pi def perimeter_polygon(s,l): perimeter = s*l return perimeter
Write a python function to check whether every even index contains even numbers of a given list.
def even_position(nums): return all(nums[i]%2==i%2 for i in range(len(nums)))
Write a function to remove the nested record from the given tuple.
def remove_nested(test_tup): res = tuple() for count, ele in enumerate(test_tup): if not isinstance(ele, tuple): res = res + (ele, ) return (res)
Write a python function to count the number of lists in a given number of lists.
def count_list(input_list): return len(input_list)
Write a function to find the n - cheap price items from a given dataset using heap queue algorithm.
import heapq def cheap_items(items,n): cheap_items = heapq.nsmallest(n, items, key=lambda s: s['price']) return cheap_items
Write function to find the sum of all items in the given dictionary.
def return_sum(dict): sum = 0 for i in dict.values(): sum = sum + i return sum
Write a python function to find the sum of all odd natural numbers within the range l and r.
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)
Write a python function to find the sum of an array.
def _sum(arr): sum=0 for i in arr: sum = sum + i return(sum)
Write a python function to left rotate the bits of a given number.
INT_BITS = 32 def left_Rotate(n,d): return (n << d)|(n >> (INT_BITS - d))
Write a function to remove all whitespaces from a string.
import re def remove_all_spaces(text): return (re.sub(r'\s+', '',text))
Write a python function to count the number of equal numbers from three given integers.
def test_three_equal(x,y,z): result= set([x,y,z]) if len(result)==3: return 0 else: return (4-len(result))
Write a python function to count the number of rotations required to generate a sorted array.
def count_Rotation(arr,n): for i in range (1,n): if (arr[i] < arr[i - 1]): return i return 0
Write a python function to check whether the product of numbers is even or not.
def is_Product_Even(arr,n): for i in range(0,n): if ((arr[i] & 1) == 0): return True return False
Write a function to find the list in a list of lists whose sum of elements is the highest.
def max_sum_list(lists): return max(lists, key=sum)
Write a python function to find the first odd number in a given list of numbers.
def first_odd(nums): first_odd = next((el for el in nums if el%2!=0),-1) return first_odd
Write a function to check if the given tuples contain the k or not.
def check_K(test_tup, K): res = False for ele in test_tup: if ele == K: res = True break return (res)
Write a function to check if each element of second tuple is smaller than its corresponding index in first tuple.
def check_smaller(test_tup1, test_tup2): res = all(x > y for x, y in zip(test_tup1, test_tup2)) return (res)
Write a function to iterate over elements repeating each as many times as its count.
from collections import Counter def count_variable(a,b,c,d): c = Counter(p=a, q=b, r=c, s=d) return list(c.elements())
Write a function to check if two lists of tuples are identical or not.
def check_identical(test_list1, test_list2): res = test_list1 == test_list2 return (res)
Write a function to abbreviate 'road' as 'rd.' in a given string.
import re def road_rd(street): return (re.sub('Road$', 'Rd.', street))
Write a function to find length of the string.
def string_length(str1): count = 0 for char in str1: count += 1 return count
Write a function to find the area of a rombus.
def rombus_area(p,q): area=(p*q)/2 return area
Write a function to clear the values of the given tuples.
def clear_tuple(test_tup): temp = list(test_tup) temp.clear() test_tup = tuple(temp) return (test_tup)
Write a function to find numbers divisible by m or n from a list of numbers using lambda function.
def div_of_nums(nums,m,n): result = list(filter(lambda x: (x % m == 0 or x % n == 0), nums)) return result
Write a python function to count lower case letters in a given string.
def lower_ctr(str): lower_ctr= 0 for i in range(len(str)): if str[i] >= 'a' and str[i] <= 'z': lower_ctr += 1 return lower_ctr
Write a function to check whether the given month number contains 28 days or not.
def check_monthnum_number(monthnum1): if monthnum1 == 2: return True else: return False
Write a function to merge two dictionaries into a single expression.
import collections as ct def merge_dictionaries(dict1,dict2): merged_dict = dict(ct.ChainMap({}, dict1, dict2)) return merged_dict
Write a python function to remove even numbers from a given list.
def remove_even(l): for i in l: if i % 2 == 0: l.remove(i) return l
Write a python function to access multiple elements of specified index from a given list.
def access_elements(nums, list_index): result = [nums[i] for i in list_index] return result
Write a function to sum a specific column of a list in a given list of lists.
def sum_column(list1, C): result = sum(row[C] for row in list1) return result
Write a function to round up a number to specific digits.
import math def round_up(a, digits): n = 10**-digits return round(math.ceil(a / n) * n, digits)
Write a function to extract the maximum numeric value from a string by using regex.
import re def extract_max(input): numbers = re.findall('\d+',input) numbers = map(int,numbers) return max(numbers)
Write a function to get dictionary keys as a list.
def get_key(dict): list = [] for key in dict.keys(): list.append(key) return list
Write a python function to find the slope of a line.
def slope(x1,y1,x2,y2): return (float)(y2-y1)/(x2-x1)
Write a python function to find the cube sum of first n odd natural numbers.
def cube_Sum(n): sum = 0 for i in range(0,n) : sum += (2*i+1)*(2*i+1)*(2*i+1) return sum
Write a python function to check whether the roots of a quadratic equation are numerically equal but opposite in sign or not.
def Check_Solution(a,b,c): if b == 0: return ("Yes") else: return ("No")
Write a function to count the number of inversions in the given array.
def get_inv_count(arr, n): inv_count = 0 for i in range(n): for j in range(i + 1, n): if (arr[i] > arr[j]): inv_count += 1 return inv_count
Write a python function to copy a list from a singleton tuple.
def lcopy(xs): return xs[:]
Write a function to find the area of a trapezium.
def area_trapezium(base1,base2,height): area = 0.5 * (base1 + base2) * height return area
Write a python function to find sum of inverse of divisors.
def Sum_of_Inverse_Divisors(N,Sum): ans = float(Sum)*1.0 /float(N); return round(ans,2);
Write a python function to remove negative numbers from a list.
def remove_negs(num_list): for item in num_list: if item < 0: num_list.remove(item) return num_list
Write a function which accepts an arbitrary list and converts it to a heap using heap queue algorithm.
import heapq as hq def raw_heap(rawheap): hq.heapify(rawheap) return rawheap
Write a function to list out the list of given strings individually using map function.
def listify_list(list1): result = list(map(list,list1)) return result
Write a function to count number of lists in a given list of lists and square the count.
def count_list(input_list): return (len(input_list))**2
Write a function to find palindromes in a given list of strings using lambda function.
def palindrome_lambda(texts): result = list(filter(lambda x: (x == "".join(reversed(x))), texts)) return result
Write a function to print n-times a list using map function.
def ntimes_list(nums,n): result = map(lambda x:n*x, nums) return list(result)
Write a python function to add a minimum number such that the sum of array becomes even.
def min_Num(arr,n): odd = 0 for i in range(n): if (arr[i] % 2): odd += 1 if (odd % 2): return 1 return 2
Write a function to remove sublists from a given list of lists, which are outside a given range.
def remove_list_range(list1, leftrange, rigthrange): result = [i for i in list1 if (min(i)>=leftrange and max(i)<=rigthrange)] return result
Write a function to calculate the sum of the positive numbers of a given list of numbers using lambda function.
def sum_positivenum(nums): sum_positivenum = list(filter(lambda nums:nums>0,nums)) return sum(sum_positivenum)
Write a function to check if a nested list is a subset of another nested list.
def check_subset(list1,list2): return all(map(list1.__contains__,list2))
Write a function to solve the fibonacci sequence using recursion.
def fibonacci(n): if n == 1 or n == 2: return 1 else: return (fibonacci(n - 1) + (fibonacci(n - 2)))
Write a function to find the minimum difference in the tuple pairs of given tuples.
def min_difference(test_list): temp = [abs(b - a) for a, b in test_list] res = min(temp) return (res)
Write a python function to sort the given string.
def sort_String(str) : str = ''.join(sorted(str)) return (str)
Write a function to check if the given tuple contains only k elements.
def check_tuples(test_tuple, K): res = all(ele in K for ele in test_tuple) return (res)
Write a function to caluclate perimeter of a parallelogram.
def parallelogram_perimeter(b,h): perimeter=2*(b*h) return perimeter
Write a function to find numbers divisible by m and n from a list of numbers using lambda function.
def div_of_nums(nums,m,n): result = list(filter(lambda x: (x % m == 0 and x % n == 0), nums)) return result
Write a function to add all the numbers in a list and divide it with the length of the list.
def sum_num(numbers): total = 0 for x in numbers: total += x return total/len(numbers)
Write a python function to check whether the given number is odd or not using bitwise operator.
def is_odd(n) : if (n^1 == n-1) : return True; else : return False;
Write a function to substract the elements of the given nested tuples.
def substract_elements(test_tup1, test_tup2): res = tuple(tuple(a - b for a, b in zip(tup1, tup2)) for tup1, tup2 in zip(test_tup1, test_tup2)) return (res)
Write a function to reverse each list in a given list of lists.
def reverse_list_lists(lists): for l in lists: l.sort(reverse = True) return lists
Write a python function to find the index of an extra element present in one sorted array.
def find_Extra(arr1,arr2,n) : for i in range(0, n) : if (arr1[i] != arr2[i]) : return i return n
Write a function to remove multiple spaces in a string.
import re def remove_spaces(text): return (re.sub(' +',' ',text))
Write a python function to get the last element of each sublist.
def Extract(lst): return [item[-1] for item in lst]
Write a function to convert the given string of float type into tuple.
def float_to_tuple(test_str): res = tuple(map(float, test_str.split(', '))) return (res)
Write a function to sort a list in increasing order by the last element in each tuple from a given list of non-empty tuples.
def last(n): return n[-1] def sort_list_last(tuples): return sorted(tuples, key=last)
Write a python function to check whether the word is present in a given sentence or not.
def is_Word_Present(sentence,word): s = sentence.split(" ") for i in s: if (i == word): return True return False
Write a function where a string will start with a specific number.
import re def match_num(string): text = re.compile(r"^5") if text.match(string): return True else: return False
Write a function to combine two dictionaries by adding values for common keys.
from collections import Counter def add_dict(d1,d2): add_dict = Counter(d1) + Counter(d2) return add_dict
Write a function to return true if the given number is even else return false.
def even_num(x): if x%2==0: return True else: return False
Write a function to extract year, month and date from a url by using regex.
import re def extract_date(url): return re.findall(r'/(\d{4})/(\d{1,2})/(\d{1,2})/', url)
Write a function to print the first n lucky numbers.
def lucky_num(n): List=range(-1,n*n+9,2) i=2 while List[i:]:List=sorted(set(List)-set(List[List[i]::List[i]]));i+=1 return List[1:n+1]
Write a function to find the fixed point in the given array.
def find_fixed_point(arr, n): for i in range(n): if arr[i] is i: return i return -1
Write a function to find the previous palindrome of a specified number.
def previous_palindrome(num): for x in range(num-1,0,-1): if str(x) == str(x)[::-1]: return x
Write a function to validate a gregorian date.
import datetime def check_date(m, d, y): try: m, d, y = map(int, (m, d, y)) datetime.date(y, m, d) return True except ValueError: return False
Write a function to check for a number at the end of a string.
import re def end_num(string): text = re.compile(r".*[0-9]$") if text.match(string): return True else: return False
Write a function to rearrange positive and negative numbers in a given array using lambda function.
def rearrange_numbs(array_nums): result = sorted(array_nums, key = lambda i: 0 if i == 0 else -1 / i) return result
Write a python function to multiply all items in the list.
def multiply_list(items): tot = 1 for x in items: tot *= x return tot
Write a function to remove all tuples with all none values in the given tuple list.
def remove_tuple(test_list): res = [sub for sub in test_list if not all(ele == None for ele in sub)] return (str(res))
Write a function to perform chunking of tuples each of size n.
def chunk_tuples(test_tup, N): res = [test_tup[i : i + N] for i in range(0, len(test_tup), N)] return (res)
Write a function to find maximum of two numbers.
def max_of_two( x, y ): if x > y: return x return y
Write a python function to calculate the product of all the numbers of a given tuple.
def mutiple_tuple(nums): temp = list(nums) product = 1 for x in temp: product *= x return product
Write a function to convert a date of yyyy-mm-dd format to dd-mm-yyyy format.
import re def change_date_format(dt): return re.sub(r'(\d{4})-(\d{1,2})-(\d{1,2})', '\\3-\\2-\\1', dt) return change_date_format(dt)
Write a function to count repeated items of a tuple.
def count_tuplex(tuplex,value): count = tuplex.count(value) return count
Write a function to calculate the sum of series 1³+2³+3³+….+n³.
import math def sum_series(number): total = 0 total = math.pow((number * (number + 1)) /2, 2) return total
Write a function to remove duplicate words from a given list of strings.
def remove_duplic_list(l): temp = [] for x in l: if x not in temp: temp.append(x) return temp
Write a function to convert camel case string to snake case string by using regex.
import re def camel_to_snake(text): str1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text) return re.sub('([a-z0-9])([A-Z])', r'\1_\2', str1).lower()
Write a function to find the nth delannoy number.
def dealnnoy_num(n, m): if (m == 0 or n == 0) : return 1 return dealnnoy_num(m - 1, n) + dealnnoy_num(m - 1, n - 1) + dealnnoy_num(m, n - 1)