description
stringlengths 37
249
| code
stringlengths 30
1.33k
| normalized_code
stringlengths 19
1.01k
|
---|---|---|
Write a python function to check whether the elements in a list are same or not. | def chkList(lst):
return len(set(lst)) == 1 | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Write a function to remove even characters in a string. | def remove_even(str1):
str2 = ''
for i in range(1, len(str1) + 1):
if(i % 2 != 0):
str2 = str2 + str1[i - 1]
return str2 | FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Write a python function to find the hamming distance between given two integers. | def hamming_Distance(n1,n2) :
x = n1 ^ n2
setBits = 0
while (x > 0) :
setBits += x & 1
x >>= 1
return setBits | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Write a python function to count the occurrence of a given character in a string. | def count(s,c) :
res = 0
for i in range(len(s)) :
if (s[i] == c):
res = res + 1
return res | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a function to find the inversions of tuple elements in the given tuple list. | def inversion_elements(test_tup):
res = tuple(list(map(lambda x: ~x, list(test_tup))))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Write a function to perform the adjacent element concatenation in the given tuples. | def concatenate_elements(test_tup):
res = tuple(i + j for i, j in zip(test_tup, test_tup[1:]))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Write a function to count the longest repeating subsequences such that the two subsequences don’t have same string characters at same positions. | def find_longest_repeating_subseq(str):
n = len(str)
dp = [[0 for k in range(n+1)] for l in range(n+1)]
for i in range(1, n+1):
for j in range(1, n+1):
if (str[i-1] == str[j-1] and i != j):
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
return dp[n][n] | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Write a function to check the given decimal with a precision of 2 by using regex. | import re
def is_decimal(num):
num_fetch = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""")
result = num_fetch.search(num)
return bool(result) | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Write a function to delete the smallest element from the given heap and then insert a new item. | import heapq as hq
def heap_replace(heap,a):
hq.heapify(heap)
hq.heapreplace(heap, a)
return heap | IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
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. | import re
def is_allowed_specific_char(string):
get_char = re.compile(r'[^a-zA-Z0-9.]')
string = get_char.search(string)
return not bool(string) | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Write a python function to count numbers whose oth and nth bits are set. | def count_Num(n):
if (n == 1):
return 1
count = pow(2,n - 2)
return count | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Write a python function to find the sum of fourth power of n natural numbers. | import math
def fourth_Power_Sum(n):
sum = 0
for i in range(1,n+1) :
sum = sum + (i*i*i*i)
return sum | IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR |
Write a function to perform the concatenation of two string tuples. | def concatenate_strings(test_tup1, test_tup2):
res = tuple(ele1 + ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to convert radians to degrees. | import math
def degree_radian(radian):
degree = radian*(180/math.pi)
return degree | IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR |
Write a function to decode a run-length encoded given list. | def decode_list(alist):
def aux(g):
if isinstance(g, list):
return [(g[1], range(g[0]))]
else:
return [(g, [0])]
return [x for g in alist for x, R in aux(g) for i in R] | FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN LIST VAR LIST NUMBER RETURN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Write a function to check if a nested list is a subset of another nested list. | def check_subset_list(list1, list2):
l1, l2 = list1[0], list2[0]
exist = True
for i in list2:
if i not in list1:
exist = False
return exist | FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR |
Write a python function to find the first repeated character in a given string. | def first_Repeated_Char(str):
h = {}
for ch in str:
if ch in h:
return ch;
else:
h[ch] = 0
return '\0' | FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER RETURN STRING |
Write a python function to find the minimum operations required to make two numbers equal. | import math
def min_Operations(A,B):
if (A > B):
swap(A,B)
B = B // math.gcd(A,B);
return B - 1 | IMPORT FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Write a function to extract maximum and minimum k elements in the given tuple. |
def extract_min_max(test_tup, K):
res = []
test_tup = list(test_tup)
temp = sorted(test_tup)
for idx, val in enumerate(temp):
if idx < K or idx >= len(temp) - K:
res.append(val)
res = tuple(res)
return (res) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Write a function to replace maximum n occurrences of spaces, commas, or dots with a colon. | import re
def replace_max_specialchar(text,n):
return (re.sub("[ ,.]", ":", text, n)) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR STRING STRING VAR VAR |
Write a python function to find the first even number in a given list of numbers. | def first_even(nums):
first_even = next((el for el in nums if el%2==0),-1)
return first_even | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR |
Write a function to check if all the elements in tuple have same data type or not. | def check_type(test_tuple):
res = True
for ele in test_tuple:
if not isinstance(ele, type(test_tuple[0])):
res = False
break
return (res) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Write a function to check for majority element in the given sorted array. | def is_majority(arr, n, x):
i = binary_search(arr, 0, n-1, x)
if i == -1:
return False
if ((i + n//2) <= (n -1)) and arr[i + n//2] == x:
return True
else:
return False
def binary_search(arr, low, high, x):
if high >= low:
mid = (low + high)//2
if (mid == 0 or x > arr[mid-1]) and (arr[mid] == x):
return mid
elif x > arr[mid]:
return binary_search(arr, (mid + 1), high, x)
else:
return binary_search(arr, low, (mid -1), x)
return -1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER |
Write a python function to count set bits of a given number. | def count_Set_Bits(n):
count = 0
while (n):
count += n & 1
n >>= 1
return count | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Write a python function to find the minimum element in a sorted and rotated array. | def find_Min(arr,low,high):
while (low < high):
mid = low + (high - low) // 2;
if (arr[mid] == arr[high]):
high -= 1;
elif (arr[mid] > arr[high]):
low = mid + 1;
else:
high = mid;
return arr[high]; | FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
Write a python function to remove the characters which have odd index values of a given string. | def odd_values_string(str):
result = ""
for i in range(len(str)):
if i % 2 == 0:
result = result + str[i]
return result | FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR |
Write a function to find minimum of three numbers. | def min_of_three(a,b,c):
if (a <= b) and (a <= c):
smallest = a
elif (b <= a) and (b <= c):
smallest = b
else:
smallest = c
return smallest | FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Write a python function to check whether all the bits are unset in the given range or not. | def all_Bits_Set_In_The_Given_Range(n,l,r):
num = (((1 << r) - 1) ^ ((1 << (l - 1)) - 1))
new_num = n & num
if (new_num == 0):
return True
return False | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Write a function to re-arrange the elements of the given array so that all negative elements appear before positive ones. | def re_arrange_array(arr, n):
j=0
for i in range(0, n):
if (arr[i] < 0):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
j = j + 1
return arr | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a function to replace blank spaces with any character in a string. | def replace_blank(str1,char):
str2 = str1.replace(' ', char)
return str2 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR RETURN VAR |
Write a function to find the maximum sum in the given right triangle of numbers. | def max_sum(tri, n):
if n > 1:
tri[1][1] = tri[1][1]+tri[0][0]
tri[1][0] = tri[1][0]+tri[0][0]
for i in range(2, n):
tri[i][0] = tri[i][0] + tri[i-1][0]
tri[i][i] = tri[i][i] + tri[i-1][i-1]
for j in range(1, i):
if tri[i][j]+tri[i-1][j-1] >= tri[i][j]+tri[i-1][j]:
tri[i][j] = tri[i][j] + tri[i-1][j-1]
else:
tri[i][j] = tri[i][j]+tri[i-1][j]
return (max(tri[n-1])) | FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Write a function to get the n largest items from a dataset. | import heapq
def larg_nnum(list1,n):
largest=heapq.nlargest(n,list1)
return largest | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to find the lateral surface area of a cylinder. | def lateralsuface_cylinder(r,h):
lateralsurface= 2*3.1415*r*h
return lateralsurface | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR VAR RETURN VAR |
Write a function to find the volume of a cube. | def volume_cube(l):
volume = l * l * l
return volume | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR |
Write a python function to set all even bits of a given number. | def even_bit_set_number(n):
count = 0;res = 0;temp = n
while(temp > 0):
if (count % 2 == 1):
res |= (1 << count)
count+=1
temp >>= 1
return (n | res) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR |
Write a python function to count the maximum number of equilateral triangles that can be formed within a given equilateral triangle. | def No_of_Triangle(N,K):
if (N < K):
return -1;
else:
Tri_up = 0;
Tri_up = ((N - K + 1) *(N - K + 2)) // 2;
Tri_down = 0;
Tri_down = ((N - 2 * K + 1) *(N - 2 * K + 2)) // 2;
return Tri_up + Tri_down; | FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR VAR |
Write a function to check the occurrences of records which occur similar times in the given tuples. | from collections import Counter
def check_occurences(test_list):
res = dict(Counter(tuple(ele) for ele in map(sorted, test_list)))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a python function to count number of non-empty substrings of a given string. | def number_of_substrings(str):
str_len = len(str);
return int(str_len * (str_len + 1) / 2); | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER |
Write a function to find the number of possible sequences of length n such that each of the next element is greater than or equal to twice of the previous element but less than or equal to m. | def get_total_number_of_sequences(m,n):
T=[[0 for i in range(n+1)] for i in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i==0 or j==0:
T[i][j]=0
elif i<j:
T[i][j]=0
elif j==1:
T[i][j]=i
else:
T[i][j]=T[i-1][j]+T[i//2][j-1]
return T[m][n] | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Write a function to replace the last element of the list with another list. | def replace_list(list1,list2):
list1[-1:] = list2
replace_list=list1
return replace_list
| FUNC_DEF ASSIGN VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR |
Write a function to generate a 3d array having each element as '*'. | def array_3d(m,n,o):
array_3d = [[ ['*' for col in range(m)] for col in range(n)] for row in range(o)]
return array_3d | FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Write a function to count total characters in a string. | def count_charac(str1):
total = 0
for i in str1:
total = total + 1
return total | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a function to sort the given list based on the occurrence of first element of tuples. | def sort_on_occurence(lst):
dct = {}
for i, j in lst:
dct.setdefault(i, []).append(j)
return ([(i, *dict.fromkeys(j), len(j))
for i, j in dct.items()]) | FUNC_DEF ASSIGN VAR DICT FOR VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR LIST VAR RETURN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR |
Write a python function to find the next perfect square greater than a given number. | import math
def next_Perfect_Square(N):
nextN = math.floor(math.sqrt(N)) + 1
return nextN * nextN | IMPORT FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR |
Write a function to find the maximum sum of bi-tonic sub-sequence for the given array. | def max_sum(arr, n):
MSIBS = arr[:]
for i in range(n):
for j in range(0, i):
if arr[i] > arr[j] and MSIBS[i] < MSIBS[j] + arr[i]:
MSIBS[i] = MSIBS[j] + arr[i]
MSDBS = arr[:]
for i in range(1, n + 1):
for j in range(1, i):
if arr[-i] > arr[-j] and MSDBS[-i] < MSDBS[-j] + arr[-i]:
MSDBS[-i] = MSDBS[-j] + arr[-i]
max_sum = float("-Inf")
for i, j, k in zip(MSIBS, MSDBS, arr):
max_sum = max(max_sum, i + j - k)
return max_sum | FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR |
Write a function for computing square roots using the babylonian method. | def babylonian_squareroot(number):
if(number == 0):
return 0;
g = number/2.0;
g2 = g + 1;
while(g != g2):
n = number/ g;
g2 = g;
g = (g + n)/2;
return g; | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR |
Write a function to find the longest palindromic subsequence in the given string. | def lps(str):
n = len(str)
L = [[0 for x in range(n)] for x in range(n)]
for i in range(n):
L[i][i] = 1
for cl in range(2, n+1):
for i in range(n-cl+1):
j = i+cl-1
if str[i] == str[j] and cl == 2:
L[i][j] = 2
elif str[i] == str[j]:
L[i][j] = L[i+1][j-1] + 2
else:
L[i][j] = max(L[i][j-1], L[i+1][j]);
return L[0][n-1] | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
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)) | FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
Write a function to find the intersection of two arrays using lambda function. | def intersection_array(array_nums1,array_nums2):
result = list(filter(lambda x: x in array_nums1, array_nums2))
return result | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Write a python function to count the occcurences of an element in a tuple. | def count_X(tup, x):
count = 0
for ele in tup:
if (ele == x):
count = count + 1
return count | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a function to insert an element before each element of a list. | def insert_element(list,element):
list = [v for elt in list for v in (element, elt)]
return list | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Write a python function to convert complex numbers to polar coordinates. | import cmath
def convert(numbers):
num = cmath.polar(numbers)
return (num) | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Write a python function to count integers from a given list. | def count_integer(list1):
ctr = 0
for i in list1:
if isinstance(i, int):
ctr = ctr + 1
return ctr | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a function to find all words starting with 'a' or 'e' in a given string. | import re
def words_ae(text):
list = re.findall("[ae]\w+", text)
return list | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR RETURN VAR |
Write a function to choose specified number of colours from three different colours and generate all the combinations with repetitions. | from itertools import combinations_with_replacement
def combinations_colors(l, n):
return list(combinations_with_replacement(l,n))
| FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Write a python function to count the number of prime numbers less than a given non-negative number. | def count_Primes_nums(n):
ctr = 0
for num in range(n):
if num <= 1:
continue
for i in range(2,num):
if (num % i) == 0:
break
else:
ctr += 1
return ctr | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR |
Write a function to swap two numbers. | def swap_numbers(a,b):
temp = a
a = b
b = temp
return (a,b) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Write a function to find number of odd elements in the given list using lambda function. | def count_odd(array_nums):
count_odd = len(list(filter(lambda x: (x%2 != 0) , array_nums)))
return count_odd | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR |
Write a function to maximize the given two tuples. | def maximize_elements(test_tup1, test_tup2):
res = tuple(tuple(max(a, b) for a, b in zip(tup1, tup2))
for tup1, tup2 in zip(test_tup1, test_tup2))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to find the nth newman–shanks–williams prime number. | def newman_prime(n):
if n == 0 or n == 1:
return 1
return 2 * newman_prime(n - 1) + newman_prime(n - 2) | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER |
Write a function to perform mathematical division operation across the given tuples. | def division_elements(test_tup1, test_tup2):
res = tuple(ele1 // ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to split a given list into two parts where the length of the first part of the list is given. | def split_two_parts(list1, L):
return list1[:L], list1[L:] | FUNC_DEF RETURN VAR VAR VAR VAR |
Write a function to merge two dictionaries. | def merge_dict(d1,d2):
d = d1.copy()
d.update(d2)
return d | FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Write a function to calculate a dog's age in dog's years. | def dog_age(h_age):
if h_age < 0:
exit()
elif h_age <= 2:
d_age = h_age * 10.5
else:
d_age = 21 + (h_age - 2)*4
return d_age | FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR |
Write a function to split a list for every nth element. | def list_split(S, step):
return [S[i::step] for i in range(step)] | FUNC_DEF RETURN VAR VAR VAR VAR FUNC_CALL VAR VAR |
Write a function to find the lateral surface area of a cube. | def lateralsurface_cube(l):
LSA = 4 * (l * l)
return LSA | FUNC_DEF ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR RETURN VAR |
Write a python function to find the sum of squares of first n odd natural numbers. | def square_Sum(n):
return int(n*(4*n*n-1)/3) | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER |
Write a function to find the n'th star number. | def find_star_num(n):
return (6 * n * (n - 1) + 1) | FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER |
Write a function to find the ascii value of a character. | def ascii_value(k):
ch=k
return ord(ch) | FUNC_DEF ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR |
Write a python function to find the sum of even numbers at even positions. | def sum_even_and_even_index(arr,n):
i = 0
sum = 0
for i in range(0,n,2):
if (arr[i] % 2 == 0) :
sum += arr[i]
return sum | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR |
Write a python function to find the sum of fifth 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*j);
return sum; | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Write a function to perfom the rear element extraction from list of tuples records. | def rear_extract(test_list):
res = [lis[-1] for lis in test_list]
return (res) | FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR |
Write a function to substract the contents of one tuple with corresponding index of other tuple. | def substract_elements(test_tup1, test_tup2):
res = tuple(map(lambda i, j: i - j, test_tup1, test_tup2))
return (res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR |
Write a python function to find sum of even index binomial coefficients. | import math
def even_binomial_Coeff_Sum( n):
return (1 << (n - 1)) | IMPORT FUNC_DEF RETURN BIN_OP NUMBER BIN_OP VAR NUMBER |
Write a python function to find the position of the last removed element from the given array. | import math as mt
def get_Position(a,n,m):
for i in range(n):
a[i] = (a[i] // m + (a[i] % m != 0))
result,maxx = -1,-1
for i in range(n - 1,-1,-1):
if (maxx < a[i]):
maxx = a[i]
result = i
return result + 1 | IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER |
Write a function to find the volume of a cylinder. | def volume_cylinder(r,h):
volume=3.1415*r*r*h
return volume | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR |
Write a function to filter a dictionary based on values. | def dict_filter(dict,n):
result = {key:value for (key, value) in dict.items() if value >=n}
return result | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to find the element count that occurs before the record in the given tuple. | def count_first_elements(test_tup):
for count, ele in enumerate(test_tup):
if isinstance(ele, tuple):
break
return (count) | FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to find the nth decagonal number. | def is_num_decagonal(n):
return 4 * n * n - 3 * n | FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR |
Write a function to search an element in the given array by using sequential search. | def sequential_search(dlist, item):
pos = 0
found = False
while pos < len(dlist) and not found:
if dlist[pos] == item:
found = True
else:
pos = pos + 1
return found, pos | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Write a python function to check if the elements of a given list are unique or not. | def all_unique(test_list):
if len(test_list) > len(set(test_list)):
return False
return True | FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Write a function to substaract two lists using map and lambda function. | def sub_list(nums1,nums2):
result = map(lambda x, y: x - y, nums1, nums2)
return list(result) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Write a python function to check whether the frequency of each digit is less than or equal to the digit itself. | def validate(n):
for i in range(10):
temp = n;
count = 0;
while (temp):
if (temp % 10 == i):
count+=1;
if (count > i):
return False
temp //= 10;
return True | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER |
Write a function to check whether all items of a list are equal to a given string. | def check_element(list,element):
check_element=all(v== element for v in list)
return check_element | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Write a function that matches a string that has an a followed by two to three 'b'. | import re
def text_match_two_three(text):
patterns = 'ab{2,3}'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!') | IMPORT FUNC_DEF ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR RETURN STRING RETURN STRING |
Write a function to find the largest sum of contiguous array in the modified array which is formed by repeating the given array k times. | def max_sub_array_sum_repeated(a, n, k):
max_so_far = -2147483648
max_ending_here = 0
for i in range(n*k):
max_ending_here = max_ending_here + a[i%n]
if (max_so_far < max_ending_here):
max_so_far = max_ending_here
if (max_ending_here < 0):
max_ending_here = 0
return max_so_far | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Write a python function to find the sum of squares of first n even natural numbers. | def square_Sum(n):
return int(2*n*(n+1)*(2*n+1)/3) | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Write a function to count array elements having modular inverse under given prime number p equal to itself. | def modular_inverse(arr, N, P):
current_element = 0
for i in range(0, N):
if ((arr[i] * arr[i]) % P == 1):
current_element = current_element + 1
return current_element | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a python function to calculate the number of odd days in a given year. | def odd_Days(N):
hund1 = N // 100
hund4 = N // 400
leap = N >> 2
ordd = N - leap
if (hund1):
ordd += hund1
leap -= hund1
if (hund4):
ordd -= hund4
leap += hund4
days = ordd + leap * 2
odd = days % 7
return odd | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Write a function to find the list of lists with maximum length. | def max_length(list1):
max_length = max(len(x) for x in list1 )
max_list = max((x) for x in list1)
return(max_length, max_list) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Write a function to find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color for the given fence with n posts and k colors. | def count_no_of_ways(n, k):
dp = [0] * (n + 1)
total = k
mod = 1000000007
dp[1] = k
dp[2] = k * k
for i in range(3,n+1):
dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod
return dp[n] | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR |
Write a python function to find quotient of two numbers. | def find(n,m):
q = n//m
return (q) | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Write a function to find the third side of a right angled triangle. | import math
def otherside_rightangle(w,h):
s=math.sqrt((w*w)+(h*h))
return s | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
Write a function to find the maximum value in a given heterogeneous list. | def max_val(listval):
max_val = max(i for i in listval if isinstance(i, int))
return(max_val) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Write a function to return the sum of all divisors of a number. | def sum_div(number):
divisors = [1]
for i in range(2, number):
if (number % i)==0:
divisors.append(i)
return sum(divisors) | FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Write a python function to count inversions in an 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 | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Write a function to flatten a given nested list structure. | def flatten_list(list1):
result_list = []
if not list1: return result_list
stack = [list(list1)]
while stack:
c_num = stack.pop()
next = c_num.pop()
if c_num: stack.append(c_num)
if isinstance(next, list):
if next: stack.append(list(next))
else: result_list.append(next)
result_list.reverse()
return result_list | FUNC_DEF ASSIGN VAR LIST IF VAR RETURN VAR ASSIGN VAR LIST FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Write a function to find the nested list elements which are present in another list. | def intersection_nested_lists(l1, l2):
result = [[n for n in lst if n in l1] for lst in l2]
return result | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Write a function to calculate the maximum aggregate from the list of tuples. | from collections import defaultdict
def max_aggregate(stdata):
temp = defaultdict(int)
for name, marks in stdata:
temp[name] += marks
return max(temp.items(), key=lambda x: x[1]) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Write a function to find the count of all binary sequences of length 2n such that sum of first n bits is same as sum of last n bits. | def count_binary_seq(n):
nCr = 1
res = 1
for r in range(1, n + 1):
nCr = (nCr * (n + 1 - r)) / r
res += nCr * nCr
return res | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR |
Subsets and Splits