common_id
stringlengths
5
5
image
stringlengths
24
24
code
stringlengths
43
914
11341
Validation/png/11341.png
# Write a python function to find the sum of xor of all pairs of numbers in the given array. def pair_OR_Sum(arr, n): ans = 0 for i in range(0, n): for j in range(i + 1, n): ans = ans + (arr[i] ^ arr[j]) return ans
11819
Validation/png/11819.png
def fun137(num): limit = num // 2 i = 1 root = 0 while i <= limit: if i * i <= num: root = i return root
11557
Validation/png/11557.png
# Write a python function to find sum of all prime divisors of a given number. def Sum(N): SumOfPrimeDivisors = [0] * (N + 1) for i in range(2, N + 1): if SumOfPrimeDivisors[i] == 0: for j in range(i, N + 1, i): SumOfPrimeDivisors[j] += i return SumOfPrimeDivisors[N]
11696
Validation/png/11696.png
def fun14(length, breadth): perimeter = 2 * (length + breadth) return perimeter
11477
Validation/png/11477.png
# 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)))
11339
Validation/png/11339.png
# Write a function to replace whitespaces with an underscore and vice versa in a given string by using regex. import re text = "Python Exercises" def replace_spaces(text): text = text.replace(" ", "_") return text text = text.replace("_", " ") return text
11739
Validation/png/11739.png
def fun57(basic): if basic < 20000: HRA = 0.16 * basic DA = 0.20 * basic else: HRA = 0.24 * basic DA = 0.30 * basic total = basic + HRA + DA return total
11764
Validation/png/11764.png
def fun82(N): sum = 0 for i in range(1, N+1): if i % 2 != 0: sum = sum + i return sum
11350
Validation/png/11350.png
# 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
11373
Validation/png/11373.png
# 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
11503
Validation/png/11503.png
# 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
11774
Validation/png/11774.png
def fun92(a, b): sum = 0 i = a while i <= b: sum = sum + i i = i + 1 return sum
11811
Validation/png/11811.png
def fun129(N): term = 1 sum = 0 for i in range(1, N): sum = sum + term term = term * 10 + 1 return sum
11324
Validation/png/11324.png
# 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
11562
Validation/png/11562.png
# 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
11345
Validation/png/11345.png
# 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
11733
Validation/png/11733.png
def fun51(CP, SP): if CP > SP: return 'LOSS' elif SP > CP: return 'PROFIT' else: return 'NO PROFIT, NO LOSS'
11422
Validation/png/11422.png
# Write a python function to count the number of distinct power of prime factor of given number. def count_Fac(n): m = n count = 0 i = 2 while (i * i) <= m: total = 0 while n % i == 0: n /= i total += 1 temp = 0 j = 1 while (temp + j) <= total: temp += j count += 1 j += 1 i += 1 if n != 1: count += 1 return count
11594
Validation/png/11594.png
# 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)
11689
Validation/png/11689.png
def fun7(celcius): kelvin = celcius + 273.15 return kelvin
11528
Validation/png/11528.png
# 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
11556
Validation/png/11556.png
# Write a function to find the area of a trapezium. def area_trapezium(base1, base2, height): area = 0.5 * (base1 + base2) * height return area
11550
Validation/png/11550.png
# Write a function to find the number which occurs for odd number of times in the given array. def get_odd_occurence(arr, arr_size): for i in range(0, arr_size): count = 0 for j in range(0, arr_size): if arr[i] == arr[j]: count += 1 if count % 2 != 0: return arr[i] return -1
11824
Validation/png/11824.png
def fun142(num): count = 0 for i in range(1, num+1): if num % i == 0: count = count + 1 if count == 2: return 'Prime Number' else: return 'Composite Number'
11301
Validation/png/11301.png
# Write a function to remove leading zeroes from an ip address. import re def removezero_ip(ip): string = re.sub("\.[0]*", ".", ip) return string
11387
Validation/png/11387.png
# Write a function to access dictionary key’s element by index. def access_key(ditionary, key): return list(ditionary)[key]
11599
Validation/png/11599.png
# Write a python function to check whether the given two numbers have same number of digits or not. def same_Length(A, B): while A > 0 and B > 0: A = A / 10 B = B / 10 if A == 0 and B == 0: return True return False
11615
Validation/png/11615.png
# 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]
11518
Validation/png/11518.png
# 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())
11795
Validation/png/11795.png
def fun113(N, M): ans = 0 i = 1 while i <= N: ans = ans + M i = i + 1 return ans
11549
Validation/png/11549.png
# 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
11310
Validation/png/11310.png
# 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"
11569
Validation/png/11569.png
# Write a function to find all anagrams of a string in a given list of strings using lambda function. from collections import Counter def anagram_lambda(texts, str): result = list(filter(lambda x: (Counter(str) == Counter(x)), texts)) return result
11306
Validation/png/11306.png
# Write a function to check whether the given number is armstrong or not. def armstrong_number(number): sum = 0 times = 0 temp = number while temp > 0: times = times + 1 temp = temp // 10 temp = number while temp > 0: reminder = temp % 10 sum = sum + (reminder**times) temp //= 10 if number == sum: return True else: return False
11645
Validation/png/11645.png
# 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
11299
Validation/png/11299.png
# Write a python function to interchange the first and last elements in a list. def swap_List(newList): size = len(newList) temp = newList[0] newList[0] = newList[size - 1] newList[size - 1] = temp return newList
11778
Validation/png/11778.png
def fun96(a, b): sum = 0 i = a while i <= b: if i % 2 != 0: sum = sum + i i = i + 1 return sum
11715
Validation/png/11715.png
def fun33(mass, velocity): ke = 0.5 * mass * velocity * velocity return ke
11846
Validation/png/11846.png
def fun164(string): length = len(string) count = 0 for i in range(length): ascii = ord(string[i]) if ascii == 32: count = count + 1 return count
11742
Validation/png/11742.png
def fun60(height): if height > 195: return 'Abnormal height' elif height > 165: return 'Tall' elif height > 150: return 'Average' else: return 'Short'
11447
Validation/png/11447.png
# 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)
11650
Validation/png/11650.png
# 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
11538
Validation/png/11538.png
# 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)
11783
Validation/png/11783.png
def fun101(a, d, N): sum = 0 term = a for i in range(1, N+1): sum = sum + term term = term + d return sum
11728
Validation/png/11728.png
def fun46(num): if num > 0: return 'Positive Number' else: return 'Negative Number'
11706
Validation/png/11706.png
def fun24(x, y): z = x / y return z
11513
Validation/png/11513.png
# 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)
11529
Validation/png/11529.png
# 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
11361
Validation/png/11361.png
# Write a function to group a sequence of key-value pairs into a dictionary of lists using collections module. from collections import defaultdict def grouping_dictionary(l): d = defaultdict(list) for k, v in l: d[k].append(v) return d
11294
Validation/png/11294.png
# Write a python function to split the array and add the first part to the end. def split_Arr(a, n, k): b = a[:k] return a[k::] + b[::]
11839
Validation/png/11839.png
def fun157(string): length = len(string) count = 0 i = 0 while i < length: ascii = ord(string[i]) if (ascii >= 65 and ascii <= 90) or (ascii >= 97 and ascii <= 122): count = count + 1 i = i + 1 return count
11440
Validation/png/11440.png
# 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)
11884
Validation/png/11884.png
def fun202(N): a = 0 b = 1 if N == 1: return a elif N == 2: return b else: i = 3 while i <= N: c = a + b a = b b = c i = i + 1 return c
11523
Validation/png/11523.png
# Write a function to sort the given array without using any sorting algorithm. the given array consists of only 0, 1, and 2. def sort_by_dnf(arr, n): low = 0 mid = 0 high = n - 1 while mid <= high: if arr[mid] == 0: arr[low], arr[mid] = arr[mid], arr[low] low = low + 1 mid = mid + 1 elif arr[mid] == 1: mid = mid + 1 else: arr[mid], arr[high] = arr[high], arr[mid] high = high - 1 return arr
11668
Validation/png/11668.png
# 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)
11548
Validation/png/11548.png
# 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"
11336
Validation/png/11336.png
# Write a function to replace all spaces in the given string with character * list item * list item * list item * list item '%20'. MAX = 1000 def replace_spaces(string): string = string.strip() i = len(string) space_count = string.count(" ") new_length = i + space_count * 2 if new_length > MAX: return -1 index = new_length - 1 string = list(string) for f in range(i - 2, new_length - 2): string.append("0") for j in range(i - 1, 0, -1): if string[j] == " ": string[index] = "0" string[index - 1] = "2" string[index - 2] = "%" index = index - 3 else: string[index] = string[j] index -= 1 return "".join(string)
11354
Validation/png/11354.png
# 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
11681
Validation/png/11681.png
# Write a python function to left rotate the string. def left_rotate(s, d): tmp = s[d:] + s[0:d] return tmp
11726
Validation/png/11726.png
def fun44(x, y, z): if x < y and x < z: mn = x elif y < x and y < z: mn = y else: mn = z return mn
11812
Validation/png/11812.png
def fun130(N): term = 1 sum = 0 i = 1 while i < N: sum = sum + term term = term * 10 + 1 i = i + 1 return sum
11663
Validation/png/11663.png
# 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
11818
Validation/png/11818.png
def fun136(N): sum = 0 i = 1 while i <= N: sum = sum + i * i * i i = i + 1 return sum
11679
Validation/png/11679.png
# Write a function to find the maximum number of segments of lengths a, b and c that can be formed from n. def maximum_segments(n, a, b, c): dp = [-1] * (n + 10) dp[0] = 0 for i in range(0, n): if dp[i] != -1: if i + a <= n: dp[i + a] = max(dp[i] + 1, dp[i + a]) if i + b <= n: dp[i + b] = max(dp[i] + 1, dp[i + b]) if i + c <= n: dp[i + c] = max(dp[i] + 1, dp[i + c]) return dp[n]
11847
Validation/png/11847.png
def fun165(string): length = len(string) count = 0 i = 0 while i < length: ascii = ord(string[i]) if ascii == 32: count = count + 1 i = i + 1 return count
11842
Validation/png/11842.png
def fun160(string): length = len(string) count = 0 for i in range(length): ascii = ord(string[i]) if ascii >= 97 and ascii <= 122: count = count + 1 return count
11520
Validation/png/11520.png
# Write a function to abbreviate 'road' as 'rd.' in a given string. import re def road_rd(street): return re.sub("Road$", "Rd.", street)
11589
Validation/png/11589.png
# Write a function to find the sum of first even and odd number of a given list. def sum_even_odd(list1): first_even = next((el for el in list1 if el % 2 == 0), -1) first_odd = next((el for el in list1 if el % 2 != 0), -1) return first_even + first_odd
11541
Validation/png/11541.png
# 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
11851
Validation/png/11851.png
def fun169(string): length = len(string) count = 0 i = 0 while i < length: ch = string[i] if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u': count = count + 1 i = i + 1 return count
11633
Validation/png/11633.png
# 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
11340
Validation/png/11340.png
# 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
11690
Validation/png/11690.png
def fun8(kelvin): celcius = kelvin - 273.15 return celcius
11590
Validation/png/11590.png
# Write a function to caluclate perimeter of a parallelogram. def parallelogram_perimeter(b, h): perimeter = 2 * (b * h) return perimeter
11364
Validation/png/11364.png
# 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
11305
Validation/png/11305.png
# Write a function to find kth element from the given two sorted arrays. def find_kth(arr1, arr2, m, n, k): sorted1 = [0] * (m + n) i = 0 j = 0 d = 0 while i < m and j < n: if arr1[i] < arr2[j]: sorted1[d] = arr1[i] i += 1 else: sorted1[d] = arr2[j] j += 1 d += 1 while i < m: sorted1[d] = arr1[i] d += 1 i += 1 while j < n: sorted1[d] = arr2[j] d += 1 j += 1 return sorted1[k - 1]
11730
Validation/png/11730.png
def fun48(num): if num % 5 == 0 and num % 11 == 0: return 'Divisible' else: return 'Not Divisible'
11870
Validation/png/11870.png
def fun188(P, R, T): A = P * (1 + R / 100) ** T CI = A - P return CI
11539
Validation/png/11539.png
# Write a python function to count equal element pairs from the given array. def count_Pairs(arr, n): cnt = 0 for i in range(n): for j in range(i + 1, n): if arr[i] == arr[j]: cnt += 1 return cnt
11400
Validation/png/11400.png
# 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
11318
Validation/png/11318.png
# 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:]
11526
Validation/png/11526.png
# 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
11711
Validation/png/11711.png
def fun29(kmhr): ms = kmhr * 5 / 18 return ms
11481
Validation/png/11481.png
# Write a function to find the occurrence and position of the substrings within a string. import re def occurance_substring(text, pattern): for match in re.finditer(pattern, text): s = match.start() e = match.end() return (text[s:e], s, e)
11581
Validation/png/11581.png
# 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))
11680
Validation/png/11680.png
# 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
11482
Validation/png/11482.png
# Write a function to check if the string is a valid email address or not using regex. import re regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$" def check_email(email): if re.search(regex, email): return "Valid Email" else: return "Invalid Email"
11352
Validation/png/11352.png
# 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:]
11758
Validation/png/11758.png
def fun76(N): sum = 0 for i in range(0, N+1, 2): sum = sum + i return sum
11760
Validation/png/11760.png
def fun78(N): sum = 0 for i in range(1, N+1): if i % 2 == 0: sum = sum + i return sum
11533
Validation/png/11533.png
# 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
11413
Validation/png/11413.png
# 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
11699
Validation/png/11699.png
def fun17(side): volume = side * side * side return volume
11401
Validation/png/11401.png
# Write a function to remove multiple spaces in a string by using regex. import re def remove_multiple_spaces(text1): return re.sub(" +", " ", text1)
11664
Validation/png/11664.png
# 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)
11759
Validation/png/11759.png
def fun77(N): sum = 0 i = 0 while i <= N: sum = sum + i i = i + 2 return sum
11672
Validation/png/11672.png
# 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
11775
Validation/png/11775.png
def fun93(a, b): sum = 0 for i in range(a, b+1): if i % 2 == 0: sum = sum + i return sum
11510
Validation/png/11510.png
# 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
11629
Validation/png/11629.png
# 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