text
stringlengths 41
125
| code
stringlengths 30
189
|
---|---|
Write a python function to find the first repeated character in a given string. | def first_repeated_char(str1):
for index,c in enumerate(str1):
if str1[:index+1].count(c) > 1:
return c
return "None" |
Write a function to reverse words in a given string. | def reverse_words(s):
return ' '.join(reversed(s.split())) |
Write a function to convert degrees to radians. | import math
def radian_degree(degree):
radian = degree*(math.pi/180)
return radian |
Write a python function to find minimum possible value for the given periodic function. | def floor_Min(A,B,N):
x = max(B - 1,N)
return (A*x) // B |
Write a python function to remove the k'th element from a given list. | def remove_kth_element(list1, L):
return list1[:L-1] + list1[L:] |
Write a function to find the maximum of nth column from the given tuple list. | def max_of_nth(test_list, N):
res = max([sub[N] for sub in test_list])
return (res) |
Write a python function to merge the first and last elements separately in a list of lists. | def merge(lst):
return [list(ele) for ele in list(zip(*lst))] |
Write a function to find the maximum value in record list as tuple attribute in the given tuple list. | def maximum_value(test_list):
res = [(key, max(lst)) for key, lst in test_list]
return (res) |
Write a function to find the cumulative sum of all the values that are present in the given tuple list. | def cummulative_sum(test_list):
res = sum(map(sum, test_list))
return (res) |
Write a function to find average value of the numbers in a given tuple of tuples. | def average_tuple(nums):
result = [sum(x) / len(x) for x in zip(*nums)]
return result |
Write a function to perfom the modulo of tuple elements in the given two tuples. | def tuple_modulo(test_tup1, test_tup2):
res = tuple(ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return (res) |
Write a function to divide two lists using map and lambda function. | def div_list(nums1,nums2):
result = map(lambda x, y: x / y, nums1, nums2)
return list(result) |
Write a function to move all the numbers in it to the given string. | def move_num(test_str):
res = ''
dig = ''
for ele in test_str:
if ele.isdigit():
dig += ele
else:
res += ele
res += dig
return (res) |
Write a function to increment the numeric values in the given strings by k. | def increment_numerics(test_list, K):
res = [str(int(ele) + K) if ele.isdigit() else ele for ele in test_list]
return res |
Write a function to find the n-th power of individual elements in a list using lambda function. | def nth_nums(nums,n):
nth_nums = list(map(lambda x: x ** n, nums))
return nth_nums |
Write a python function to convert the given string to upper case. | def is_upper(string):
return (string.upper()) |
Write a python function to interchange first and last elements in a given list. | def swap_List(newList):
size = len(newList)
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList |
Write a python function to find the largest triangle that can be inscribed in the semicircle. | def triangle_area(r) :
if r < 0 :
return -1
return r * r |
Write a python function to find even numbers from a mixed list. | def Split(list):
ev_li = []
for i in list:
if (i % 2 == 0):
ev_li.append(i)
return ev_li |
Write a python function to move all zeroes to the end of the given list. | def move_zero(num_list):
a = [0 for i in range(num_list.count(0))]
x = [ i for i in num_list if i != 0]
x.extend(a)
return (x) |
Write a python function to find the sum of fourth power of first n even natural numbers. | def even_Power_Sum(n):
sum = 0;
for i in range(1,n + 1):
j = 2*i;
sum = sum + (j*j*j*j);
return sum; |
Write a python function to check if roots of a quadratic equation are reciprocal of each other or not. | def Check_Solution(a,b,c):
if (a == c):
return ("Yes");
else:
return ("No"); |
Write a function to check whether the given amount has no profit and no loss | def noprofit_noloss(actual_cost,sale_amount):
if(sale_amount == actual_cost):
return True
else:
return False |
Write a function to calculate wind chill index. | import math
def wind_chill(v,t):
windchill = 13.12 + 0.6215*t - 11.37*math.pow(v, 0.16) + 0.3965*t*math.pow(v, 0.16)
return int(round(windchill, 0)) |
Write a function to remove the parenthesis area in a string. | import re
def remove_parenthesis(items):
for item in items:
return (re.sub(r" ?\([^)]+\)", "", item)) |
Write a function to find the nth nonagonal number. | def is_nonagonal(n):
return int(n * (7 * n - 5) / 2) |
Write a function to remove similar rows from the given tuple matrix. | def remove_similar_row(test_list):
res = set(sorted([tuple(sorted(set(sub))) for sub in test_list]))
return (res) |
Write a python function to reverse an array upto a given position. | def reverse_Array_Upto_K(input, k):
return (input[k-1::-1] + input[k:]) |
Write a python function to count number of cubes of size k in a cube of size n. | def No_of_cubes(N,K):
No = 0
No = (N - K + 1)
No = pow(No, 3)
return No |
Write a function to split a string at uppercase letters. | import re
def split_upperstring(text):
return (re.findall('[A-Z][^A-Z]*', text)) |
Write a function to check if one tuple is a subset of another tuple. | def check_subset(test_tup1, test_tup2):
res = set(test_tup2).issubset(test_tup1)
return (res) |
Write a function to flatten the given tuple matrix into the tuple list with each tuple representing each column. | def matrix_to_list(test_list):
temp = [ele for sub in test_list for ele in sub]
res = list(zip(*temp))
return (str(res)) |
Write a function to find the perimeter of a rectangle. | def rectangle_perimeter(l,b):
perimeter=2*(l+b)
return perimeter |
Write a python function to find the sum of fifth power of n natural numbers. | def fifth_Power_Sum(n) :
sm = 0
for i in range(1,n+1) :
sm = sm + (i*i*i*i*i)
return sm |
Write a python function to find the minimum sum of absolute differences of two arrays. | def find_Min_Sum(a,b,n):
a.sort()
b.sort()
sum = 0
for i in range(n):
sum = sum + abs(a[i] - b[i])
return sum |
Write a python function to choose points from two ranges such that no point lies in both the ranges. | def find_Points(l1,r1,l2,r2):
x = min(l1,l2) if (l1 != l2) else -1
y = max(r1,r2) if (r1 != r2) else -1
return (x,y) |
Write a function to sort a list in a dictionary. | def sorted_dict(dict1):
sorted_dict = {x: sorted(y) for x, y in dict1.items()}
return sorted_dict |
Write a python function to shift first element to the end of given list. | def move_last(num_list):
a = [num_list[0] for i in range(num_list.count(num_list[0]))]
x = [ i for i in num_list if i != num_list[0]]
x.extend(a)
return (x) |
Write a function to count occurrence of a character in a string. | def count_char(string,char):
count = 0
for i in range(len(string)):
if(string[i] == char):
count = count + 1
return count |
Write a python function to count number of vowels in the string. | def Check_Vow(string, vowels):
final = [each for each in string if each in vowels]
return(len(final))
|
Write a python function to replace multiple occurence of character by single. | import re
def replace(string, char):
pattern = char + '{2,}'
string = re.sub(pattern, char, string)
return string |
Write a python function to check whether a sequence of numbers has a decreasing trend or not. | def decreasing_trend(nums):
if (sorted(nums)== nums):
return True
else:
return False |
Write a python function to convert a list of multiple integers into a single integer. | def convert(list):
s = [str(i) for i in list]
res = int("".join(s))
return (res) |
Write a function to remove duplicate words from a given string using collections module. | from collections import OrderedDict
def remove_duplicate(string):
result = ' '.join(OrderedDict((w,w) for w in string.split()).keys())
return result |
Write a function to add two integers. however, if the sum is between the given range it will return 20. | def sum_nums(x, y,m,n):
sum_nums= x + y
if sum_nums in range(m, n):
return 20
else:
return sum_nums |
Write a function to remove everything except alphanumeric characters from the given string by using regex. | import re
def remove_extra_char(text1):
pattern = re.compile('[\W_]+')
return (pattern.sub('', text1)) |
Write a function to check if the triangle is valid or not. | def validity_triangle(a,b,c):
total = a + b + c
if total == 180:
return True
else:
return False |
Write a python function to remove spaces from a given string. | def remove_spaces(str1):
str1 = str1.replace(' ','')
return str1 |
Write a function to access dictionary key’s element by index. | def access_key(ditionary,key):
return list(ditionary)[key] |
Write a python function to check whether a sequence of numbers has an increasing trend or not. | def increasing_trend(nums):
if (sorted(nums)== nums):
return True
else:
return False |
Write a function to multiply two lists using map and lambda function. | def mul_list(nums1,nums2):
result = map(lambda x, y: x * y, nums1, nums2)
return list(result) |
Write a function to find the frequency of each element in the given list. | from collections import defaultdict
def freq_element(test_tup):
res = defaultdict(int)
for ele in test_tup:
res[ele] += 1
return (str(dict(res))) |
Write a function to get the length of a complex number. | import cmath
def len_complex(a,b):
cn=complex(a,b)
length=abs(cn)
return length |
Write a function to multiply consecutive numbers of a given list. | def mul_consecutive_nums(nums):
result = [b*a for a, b in zip(nums[:-1], nums[1:])]
return result |
Write a python function to find the last two digits in factorial of a given number. | def last_Two_Digits(N):
if (N >= 10):
return
fac = 1
for i in range(1,N + 1):
fac = (fac * i) % 100
return (fac) |
Write a function to remove multiple spaces in a string by using regex. | import re
def remove_multiple_spaces(text1):
return (re.sub(' +',' ',text1)) |
Write a function to extract unique values from the given dictionary values. | def extract_unique(test_dict):
res = list(sorted({ele for val in test_dict.values() for ele in val}))
return res |
Write a function to check if each element of the second tuple is greater than its corresponding index in the first tuple. | def check_greater(test_tup1, test_tup2):
res = all(x < y for x, y in zip(test_tup1, test_tup2))
return (res) |
Write a function to zip two given lists of lists. | def zip_list(list1,list2):
result = list(map(list.__add__, list1, list2))
return result |
Write a function to find number of even elements in the given list using lambda function. | def count_even(array_nums):
count_even = len(list(filter(lambda x: (x%2 == 0) , array_nums)))
return count_even |
Write a function to count the number of elements in a list which are within a specific range. | def count_range_in_list(li, min, max):
ctr = 0
for x in li:
if min <= x <= max:
ctr += 1
return ctr |
Write a function to check whether the given key is present in the dictionary or not. | def is_key_present(d,x):
if x in d:
return True
else:
return False |
Write a function to calculate the harmonic sum of n-1. | def harmonic_sum(n):
if n < 2:
return 1
else:
return 1 / n + (harmonic_sum(n - 1)) |
Write a function to sort a list of lists by length and value. | def sort_sublists(list1):
list1.sort()
list1.sort(key=len)
return list1 |
Write a python function to convert a string to a list. | def Convert(string):
li = list(string.split(" "))
return li |
Write a function to access the initial and last data of the given tuple record. | def front_and_rear(test_tup):
res = (test_tup[0], test_tup[-1])
return (res) |
Write a function to remove duplicates from a list of lists. | import itertools
def remove_duplicate(list1):
list.sort(list1)
remove_duplicate = list(list1 for list1,_ in itertools.groupby(list1))
return remove_duplicate |
Write a function to check if the given tuple contains all valid values or not. | def check_valid(test_tup):
res = not any(map(lambda ele: not ele, test_tup))
return (res) |
Write a function to convert the given string of integers into a tuple. | def str_to_tuple(test_str):
res = tuple(map(int, test_str.split(', ')))
return (res) |
Write a function to find the perimeter of a rombus. | def rombus_perimeter(a):
perimeter=4*a
return perimeter |
Write a function to create a list taking alternate elements from another given list. | def alternate_elements(list1):
result=[]
for item in list1[::2]:
result.append(item)
return result |
Write a function to add a dictionary to the tuple. | 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) |
Write a function to filter the height and width of students which are stored in a dictionary. | 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 |
Write a function to count the same pair in two given lists using map function. | from operator import eq
def count_same_pair(nums1, nums2):
result = sum(map(eq, nums1, nums2))
return result |
Write a function to calculate the sum of all digits of the base to the specified power. | def power_base_sum(base, power):
return sum([int(i) for i in str(pow(base, power))]) |
Write a function to extract values between quotation marks of the given string by using regex. | import re
def extract_quotation(text1):
return (re.findall(r'"(.*?)"', text1)) |
Write a function to multiply the adjacent elements of the given tuple. | def multiply_elements(test_tup):
res = tuple(i * j for i, j in zip(test_tup, test_tup[1:]))
return (res) |
Write a function to remove all characters except letters and numbers using regex | import re
def remove_char(S):
result = re.sub('[\W_]+', '', S)
return result |
Write a function to sum elements in two lists. | def sum_list(lst1,lst2):
res_list = [lst1[i] + lst2[i] for i in range(len(lst1))]
return res_list |
Write a function to add two lists using map and lambda function. | def add_list(nums1,nums2):
result = map(lambda x, y: x + y, nums1, nums2)
return list(result) |
Write a function to remove consecutive duplicates of a given list. | from itertools import groupby
def consecutive_duplicates(nums):
return [key for key, group in groupby(nums)] |
Write a function to find the lateral surface area of a cone. | import math
def lateralsurface_cone(r,h):
l = math.sqrt(r * r + h * h)
LSA = math.pi * r * l
return LSA |
Write a function to replace all occurrences of spaces, commas, or dots with a colon. | import re
def replace_specialchar(text):
return (re.sub("[ ,.]", ":", text))
|
Write a function to locate the left insertion point for a specified value in sorted order. | import bisect
def left_insertion(a, x):
i = bisect.bisect_left(a, x)
return i |
Write a function to calculate the geometric sum of n-1. | def geometric_sum(n):
if n < 0:
return 0
else:
return 1 / (pow(2, n)) + geometric_sum(n - 1) |
Write a python function to find the index of smallest triangular number with n digits. | import math
def find_Index(n):
x = math.sqrt(2 * math.pow(10,(n - 1)));
return round(x); |
Write a function to convert the given tuple to a key-value dictionary using adjacent elements. | def tuple_to_dict(test_tup):
res = dict(test_tup[idx : idx + 2] for idx in range(0, len(test_tup), 2))
return (res) |
Write a python function to check whether all the characters are same or not. | def all_Characters_Same(s) :
n = len(s)
for i in range(1,n) :
if s[i] != s[0] :
return False
return True |
Write a function to caluclate the area of a tetrahedron. | import math
def area_tetrahedron(side):
area = math.sqrt(3)*(side*side)
return area |
Write a function to rotate a given list by specified number of items to the right direction. | def rotate_right(list1,m,n):
result = list1[-(m):]+list1[:-(n)]
return result |
Write a function to check if the given tuple has any none value or not. | def check_none(test_tup):
res = any(map(lambda ele: ele is None, test_tup))
return (res) |
Write a function to find area of a sector. | def sector_area(r,a):
pi=22/7
if a >= 360:
return None
sectorarea = (pi*r**2) * (a/360)
return sectorarea |
Write a function to put spaces between words starting with capital letters in a given string by using regex. | import re
def capital_words_spaces(str1):
return re.sub(r"(\w)([A-Z])", r"\1 \2", str1) |
Write a function to sort a given list of strings of numbers numerically. | def sort_numeric_strings(nums_str):
result = [int(x) for x in nums_str]
result.sort()
return result |
Write a function to add the given tuple to the given list. | def add_tuple(test_list, test_tup):
test_list += test_tup
return (test_list) |
Write a function to find the nth jacobsthal number. | 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] |
Write a function to find minimum k records from tuple list. | def min_k(test_list, K):
res = sorted(test_list, key = lambda x: x[1])[:K]
return (res) |
Write a function to find common index elements from three lists. | 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 |
Write a function to check a decimal with a precision of 2. | def is_decimal(num):
import re
dnumre = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""")
result = dnumre.search(num)
return bool(result) |
Write a python function to check whether an array contains only one distinct element or not. | def unique_Element(arr,n):
s = set(arr)
if (len(s) == 1):
return ('YES')
else:
return ('NO') |
Subsets and Splits