text
stringlengths
41
125
code
stringlengths
30
189
Write a function to calculate the sum of series 1²+2²+3²+….+n².
def series_sum(number): total = 0 total = (number * (number + 1) * (2 * number + 1)) / 6 return total
Write a function to re-arrange the given tuples based on the given ordered list.
def re_arrange_tuples(test_list, ord_list): temp = dict(test_list) res = [(key, temp[key]) for key in ord_list] return (res)
Write a function to count the most common character in a given string.
from collections import Counter def max_char(str1): temp = Counter(str1) max_char = max(temp, key = temp.get) return max_char
Write a function to sort a list of dictionaries using lambda function.
def sorted_models(models): sorted_models = sorted(models, key = lambda x: x['color']) return sorted_models
Write a function to count the elements in a list until an element is a tuple.
def count_elim(num): count_elim = 0 for n in num: if isinstance(n, tuple): break count_elim += 1 return count_elim
Write a function to check if any list element is present in the given list.
def check_element(test_tup, check_list): res = False for ele in check_list: if ele in test_tup: res = True break return (res)
Write a function to combine two given sorted lists using heapq module.
from heapq import merge def combine_lists(num1,num2): combine_lists=list(merge(num1, num2)) return combine_lists
Write a function to separate and print the numbers and their position of a given string.
import re def num_position(text): for m in re.finditer("\d+", text): return m.start()
Write a function to convert the given tuples into set.
def tuple_to_set(t): s = set(t) return (s)
Write a function to find the most common elements and their counts of a specified text.
from collections import Counter def most_common_elem(s,a): most_common_elem=Counter(s).most_common(a) return most_common_elem
Write a python function to find the length of the shortest word.
def len_log(list1): min=len(list1[0]) for i in list1: if len(i)<min: min=len(i) return min
Write a function to get an item of a tuple.
def get_item(tup1,index): item = tup1[index] return item
Write a function to sort the given tuple list basis the total digits in tuple.
def count_digs(tup): return sum([len(str(ele)) for ele in tup ]) def sort_list(test_list): test_list.sort(key = count_digs) return (str(test_list))
Write a function to find the maximum of similar indices in two lists of tuples.
def max_similar_indices(test_list1, test_list2): res = [(max(x[0], y[0]), max(x[1], y[1])) for x, y in zip(test_list1, test_list2)] return (res)
Write a function to find out, if the given number is abundant.
def is_abundant(n): fctrsum = sum([fctr for fctr in range(1, n) if n % fctr == 0]) return fctrsum > n
Write a function to split the given string at uppercase letters by using regex.
import re def split_list(text): return (re.findall('[A-Z][^A-Z]*', text))
Write a python function to get the position of rightmost set bit.
import math def get_First_Set_Bit_Pos(n): return math.log2(n&-n)+1
Write a python function to find the average of a list.
def Average(lst): return sum(lst) / len(lst)
Write a function to solve tiling problem.
def get_noOfways(n): if (n == 0): return 0; if (n == 1): return 1; return get_noOfways(n - 1) + get_noOfways(n - 2);
Write a python function to check whether the length of the word is even or not.
def word_len(s): s = s.split(' ') for word in s: if len(word)%2==0: return True else: return False
Write a function to convert camel case string to snake case string.
def camel_to_snake(text): import re 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 remove an empty tuple from a list of tuples.
def remove_empty(tuple1): #L = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')] tuple1 = [t for t in tuple1 if t] return tuple1
Write a python function to accept the strings which contains all vowels.
def check(string): if len(set(string).intersection("AEIOUaeiou"))>=5: return ('accepted') else: return ("not accepted")
Write a python function to find maximum possible value for the given periodic function.
def floor_Max(A,B,N): x = min(B - 1,N) return (A*x) // B
Write a function to find minimum of two numbers.
def min_of_two( x, y ): if x < y: return x return y
Write a function to concatenate the given two tuples to a nested tuple.
def concatenate_nested(test_tup1, test_tup2): res = test_tup1 + test_tup2 return (res)
Write a python function to left rotate the string.
def left_rotate(s,d): tmp = s[d : ] + s[0 : d] return tmp